From 07a01b0bc92a16ca364078b2029ddbe440dde0a8 Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Tue, 8 Apr 2025 20:39:45 +0530 Subject: [PATCH] Avoid caching resource when called via `include_relative` tag (#9784) Merge pull request 9784 --- features/include_relative_tag.feature | 19 +++++++++++++++++++ lib/jekyll/tags/include.rb | 16 ++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/features/include_relative_tag.feature b/features/include_relative_tag.feature index 9cbdcf12..9e84f30a 100644 --- a/features/include_relative_tag.feature +++ b/features/include_relative_tag.feature @@ -58,3 +58,22 @@ Feature: include_relative Tag Then I should get a zero exit status And the _site directory should exist And I should see "Welcome back Dear Reader!" in "_site/index.html" + + Scenario: Include multiple files relative to a page at root + Given I have an "apple.md" page with foo "bar" that contains "{{ page.path }}, {{ page.foo }}" + And I have an "banana.md" page with content: + """ + {% include_relative apple.md %} + {% include_relative cherry.md %} + + {{ page.path }} + """ + And I have an "cherry.md" page with foo "lipsum" that contains "{{ page.path }}, {{ page.foo }}" + When I run jekyll build + Then I should get a zero exit status + And the _site directory should exist + And I should see "

apple.md, bar

" in "_site/apple.html" + And I should see "
\n

foo: bar" in "_site/banana.html" + And I should see "


\n

foo: lipsum" in "_site/banana.html" + And I should see "

cherry.md, lipsum

" in "_site/cherry.html" + But I should not see "foo: lipsum" in "_site/cherry.html" diff --git a/lib/jekyll/tags/include.rb b/lib/jekyll/tags/include.rb index 6960952e..fa2e1f38 100644 --- a/lib/jekyll/tags/include.rb +++ b/lib/jekyll/tags/include.rb @@ -249,6 +249,11 @@ module Jekyll end class IncludeRelativeTag < IncludeTag + def load_cached_partial(path, context) + context.registers[:cached_partials] ||= {} + context.registers[:cached_partials][path] ||= parse_partial(path, context) + end + def tag_includes_dirs(context) Array(page_path(context)).freeze end @@ -267,6 +272,17 @@ module Jekyll path = File.join(site.config["collections_dir"], path) if page["collection"] path.delete_suffix("/#excerpt") end + + # Since Jekyll 4 caches convertibles based on their path within the only instance of + # `LiquidRenderer`, initialize a new LiquidRenderer instance on every render of this + # tag to bypass caching rendered output of page / document. + def parse_partial(path, context) + LiquidRenderer.new(context.registers[:site]).file(path).parse(read_file(path, context)) + rescue Liquid::Error => e + e.template_name = path + e.markup_context = "included " if e.markup_context.nil? + raise e + end end end end