Skip to content

Commit

Permalink
Cache URLFilter results of string inputs per site (#7990)
Browse files Browse the repository at this point in the history
Merge pull request 7990
  • Loading branch information
ashmaroli committed Apr 13, 2020
1 parent b146257 commit 9c0c518
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
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

0 comments on commit 9c0c518

Please sign in to comment.