Skip to content

Commit 55fba8f

Browse files
beneschjekyllbot
authored andcommitted
Memoize absolute_url and relative_url filters (jekyll#7793)
Merge pull request 7793
1 parent b9963f3 commit 55fba8f

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

lib/jekyll/filters/url_filters.rb

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,8 @@ module URLFilters
99
#
1010
# Returns the absolute URL as a String.
1111
def absolute_url(input)
12-
return if input.nil?
13-
14-
input = input.url if input.respond_to?(:url)
15-
return input if Addressable::URI.parse(input.to_s).absolute?
16-
17-
site = @context.registers[:site]
18-
return relative_url(input) if site.config["url"].nil?
19-
20-
Addressable::URI.parse(
21-
site.config["url"].to_s + relative_url(input)
22-
).normalize.to_s
12+
cache = (@context.registers[:cached_absolute_urls] ||= {})
13+
cache[input] ||= compute_absolute_url(input)
2314
end
2415

2516
# Produces a URL relative to the domain root based on site.baseurl
@@ -29,15 +20,8 @@ def absolute_url(input)
2920
#
3021
# Returns a URL relative to the domain root as a String.
3122
def relative_url(input)
32-
return if input.nil?
33-
34-
input = input.url if input.respond_to?(:url)
35-
return input if Addressable::URI.parse(input.to_s).absolute?
36-
37-
parts = [sanitized_baseurl, input]
38-
Addressable::URI.parse(
39-
parts.compact.map { |part| ensure_leading_slash(part.to_s) }.join
40-
).normalize.to_s
23+
cache = (@context.registers[:cached_relative_urls] ||= {})
24+
cache[input] ||= compute_relative_url(input)
4125
end
4226

4327
# Strips trailing `/index.html` from URLs to create pretty permalinks
@@ -53,6 +37,32 @@ def strip_index(input)
5337

5438
private
5539

40+
def compute_absolute_url(input)
41+
return if input.nil?
42+
43+
input = input.url if input.respond_to?(:url)
44+
return input if Addressable::URI.parse(input.to_s).absolute?
45+
46+
site = @context.registers[:site]
47+
return relative_url(input) if site.config["url"].nil?
48+
49+
Addressable::URI.parse(
50+
site.config["url"].to_s + relative_url(input)
51+
).normalize.to_s
52+
end
53+
54+
def compute_relative_url(input)
55+
return if input.nil?
56+
57+
input = input.url if input.respond_to?(:url)
58+
return input if Addressable::URI.parse(input.to_s).absolute?
59+
60+
parts = [sanitized_baseurl, input]
61+
Addressable::URI.parse(
62+
parts.compact.map { |part| ensure_leading_slash(part.to_s) }.join
63+
).normalize.to_s
64+
end
65+
5666
def sanitized_baseurl
5767
site = @context.registers[:site]
5868
site.config["baseurl"].to_s.chomp("/")

0 commit comments

Comments
 (0)