renderFile of a WebC include within a draft fails on build
darekkay opened this issue · comments
Running eleventy
fails when including a WebC file with renderFile
in a file that is excluded from collections.
I have reproduced the issue within eleventy-base-blog by applying the following commit:
darekkay/eleventy-base-blog@f60f881
As the demo displays, the issue occurs when including a webc
file using renderFile
:
{% renderFile "_includes/webc/components/webc.webc" %}
It works fine when running npm run start
(i.e. the dev server), by opening fifthpost:
But it fails with npm run build
:
[11ty] Problem writing Eleventy templates: (more in DEBUG output)
[11ty] 1. Having trouble rendering njk template ./content/blog/fifthpost.md (via TemplateContentRenderError)
[11ty] 2. (./content/blog/fifthpost.md)
[11ty] EleventyShortcodeError: Error with Nunjucks shortcode `renderFile` (via Template render error)
[11ty] 3. Cannot read properties of undefined (reading 'url') (via Template render error)
[11ty]
[11ty] Original error stack trace: TypeError: Cannot read properties of undefined (reading 'url')
[11ty] at Object.addContent (C:\eleventy-base-blog\node_modules\@11ty\eleventy-plugin-bundle\eleventy.shortcodes.js:24:39)
[11ty] at Object.css (C:\eleventy-base-blog\node_modules\@11ty\eleventy\src\BenchmarkGroup.js:32:26)
[11ty] at Object.<anonymous> (C:\eleventy-base-blog\node_modules\@11ty\eleventy-plugin-webc\src\eleventyWebcTemplate.js:132:37)
[11ty] Copied 1 file / Wrote 0 files in 0.46 seconds (v2.0.0)
The issue is "fixed" when the renderFile
line is removed.
The issue does not happen when including a markdown file (as presented in the same demo).
Reproducible on:
- Windows 10 / Node 18.8.0
- macOS / Node 18.11.0
The issue is caused by the "draft" feature, or more specifically, when renderFile
is used within a file that is excluded from collections (eleventyComputedExcludeFromCollections
). That's why eleventy --serve
works (drafts are not excluded) and eleventy
doesn't (drafts excluded). Adding renderFile
to a non-draft post in the eleventy-base-blog repository doesn't cause the issue.
A workaround is to add a guard to eleventy-plugin-bundle to check if this.page
is defined. However, I feel like the proper solution should be to skip eleventy-plugin-bundle
(and eleventy-plugin-webc
) completely for excluded files, also from performance perspective.
I've had a workaround to delete draft posts on CI before building 11ty, but for unrelated reasons this workaround is no longer viable for me.
As already mentioned, a quick fix would be to check in eleventy-plugin-bundle
if this.page
is defined before trying to access this.page.url
. However, the issue begins already in eleventy-plugin-webc
: if the page should not be rendered (permalink: false
), there's no point in extracting the bundles.
I've created a pull request with a fix: #93