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

renderFile of a WebC include within a draft fails on build #59

Open
darekkay opened this issue Mar 8, 2023 · 2 comments · May be fixed by #93
Open

renderFile of a WebC include within a draft fails on build #59

darekkay opened this issue Mar 8, 2023 · 2 comments · May be fixed by #93

Comments

@darekkay
Copy link

darekkay commented Mar 8, 2023

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:

rendered

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
@zachleat zachleat transferred this issue from 11ty/webc Mar 8, 2023
@darekkay darekkay changed the title renderFile of a WebC include fails on build renderFile of a WebC include within a draft fails on build Mar 14, 2023
@darekkay
Copy link
Author

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.

darekkay added a commit to darekkay/eleventy-plugin-webc that referenced this issue Jan 10, 2024
Excluded files (permalink: false) won't be rendered, hence bundling of CSS/JS resources is not required. This change not only improves the build performance, it fixes a bug where "renderFile" with WebC fails for excluded files (e.g. drafts).

Fixes: 11ty#59
@darekkay darekkay linked a pull request Jan 10, 2024 that will close this issue
@darekkay
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant