Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache URLFilter results of string inputs per site #7990

Merged
merged 5 commits into from Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 22 additions & 6 deletions lib/jekyll/filters/url_filters.rb
Expand Up @@ -9,8 +9,18 @@ module URLFilters
#
# Returns the absolute URL as a String.
def absolute_url(input)
cache = (@context.registers[:cached_absolute_urls] ||= {})
return if input.nil?

cache = if input.is_a?(String)
(@context.registers[:site].filter_cache[:absolute_url] ||= {})
else
(@context.registers[:cached_absolute_url] ||= {})
end
cache[input] ||= compute_absolute_url(input)

# Duplicate cached string so that the cached value is never mutated by
# a subsequent filter.
cache[input].dup
end

# Produces a URL relative to the domain root based on site.baseurl
Expand All @@ -20,8 +30,18 @@ def absolute_url(input)
#
# Returns a URL relative to the domain root as a String.
def relative_url(input)
cache = (@context.registers[:cached_relative_urls] ||= {})
return if input.nil?

cache = if input.is_a?(String)
(@context.registers[:site].filter_cache[:relative_url] ||= {})
else
(@context.registers[:cached_relative_url] ||= {})
end
cache[input] ||= compute_relative_url(input)

# Duplicate cached string so that the cached value is never mutated by
# a subsequent filter.
cache[input].dup
end

# Strips trailing `/index.html` from URLs to create pretty permalinks
Expand All @@ -38,8 +58,6 @@ def strip_index(input)
private

def compute_absolute_url(input)
return if input.nil?

input = input.url if input.respond_to?(:url)
return input if Addressable::URI.parse(input.to_s).absolute?

Expand All @@ -53,8 +71,6 @@ def compute_absolute_url(input)
end

def compute_relative_url(input)
return if input.nil?

input = input.url if input.respond_to?(:url)
return input if Addressable::URI.parse(input.to_s).absolute?

Expand Down
3 changes: 2 additions & 1 deletion lib/jekyll/site.rb
Expand Up @@ -10,7 +10,7 @@ class Site
:gems, :plugin_manager, :theme

attr_accessor :converters, :generators, :reader
attr_reader :regenerator, :liquid_renderer, :includes_load_paths
attr_reader :regenerator, :liquid_renderer, :includes_load_paths, :filter_cache

# Public: Initialize a new Site.
#
Expand All @@ -23,6 +23,7 @@ def initialize(config)
self.config = config

@cache_dir = in_source_dir(config["cache_dir"])
@filter_cache = {}

@reader = Reader.new(self)
@regenerator = Regenerator.new(self)
Expand Down
1 change: 1 addition & 0 deletions test/test_filters.rb
Expand Up @@ -596,6 +596,7 @@ def select; end
assert_equal "/front_matter.erb", page.url
url = filter.relative_url(page.url)
url << "foo"
assert_equal "/front_matter.erb", filter.relative_url(page.url)
assert_equal "/front_matter.erb", page.url
end

Expand Down