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

Fix url filters, caching key was independent on baseurl #8275

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 13 additions & 6 deletions lib/jekyll/filters/url_filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ def absolute_url(input)
else
(@context.registers[:cached_absolute_url] ||= {})
end
cache[input] ||= compute_absolute_url(input)
unique_cache_key = unique_input_path(input)
cache[unique_cache_key] ||= compute_absolute_url(input)

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

# Produces a URL relative to the domain root based on site.baseurl
Expand All @@ -37,11 +38,12 @@ def relative_url(input)
else
(@context.registers[:cached_relative_url] ||= {})
end
cache[input] ||= compute_relative_url(input)
unique_cache_key = unique_input_path(input)
cache[unique_cache_key] ||= compute_relative_url(input)

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

# Strips trailing `/index.html` from URLs to create pretty permalinks
Expand Down Expand Up @@ -74,9 +76,8 @@ def compute_relative_url(input)
input = input.url if input.respond_to?(:url)
return input if Addressable::URI.parse(input.to_s).absolute?

parts = [sanitized_baseurl, input]
Addressable::URI.parse(
parts.compact.map { |part| ensure_leading_slash(part.to_s) }.join
unique_input_path(input)
).normalize.to_s
end

Expand All @@ -90,6 +91,12 @@ def ensure_leading_slash(input)

"/#{input}"
end

def unique_input_path(input)
[sanitized_baseurl, input].compact.map do |part|
ensure_leading_slash(part.to_s)
end.join
end
end
end
end
19 changes: 19 additions & 0 deletions test/test_filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,17 @@ def select; end
assert_equal "http://example.com/base/#{page_url}", filter.absolute_url(page_url)
end

should "ensure cached url depends on the baseurl" do
page_url = "cached-page-testing/"
cached_url = { page_url => "http://example.com/#{page_url}" }
filter = make_filter_mock(
"url" => "http://example.com",
"baseurl" => "en"
)
filter.context.registers[:site].filter_cache[:absolute_url] = cached_url
assert_equal "http://example.com/en/#{page_url}", filter.absolute_url(page_url)
end

should "be ok with a blank but present 'url'" do
page_url = "about/my_favorite_page/"
filter = make_filter_mock(
Expand Down Expand Up @@ -540,6 +551,14 @@ def select; end
assert_equal "/base/#{page_url}", filter.relative_url(page_url)
end

should "ensure cached url depends on the baseurl" do
page_url = "cached-page-testing/"
cached_url = { page_url => page_url }
filter = make_filter_mock("baseurl" => "en")
filter.context.registers[:site].filter_cache[:relative_url] = cached_url
assert_equal "/en/#{page_url}", filter.relative_url(page_url)
end

should "normalize international URLs" do
page_url = "错误.html"
assert_equal "/base/%E9%94%99%E8%AF%AF.html", @filter.relative_url(page_url)
Expand Down