11ty / eleventy

A simpler site generator. Transforms a directory of templates (of varying types) into HTML.
https://www.11ty.dev/
MIT License
17.13k stars 493 forks source link

Eleventy 3.0.0-alpha.13: Content date results in TemplateContentPrematureUseError: Tried to use templateContent too early on error #3322

Closed dirtystylus closed 4 months ago

dirtystylus commented 4 months ago

Operating system

macOS Ventura 13.5

Eleventy

3.0.0-alpha.13

Describe the bug

Unhandled rejection in promise: (more in DEBUG output)
[11ty] Tried to use templateContent too early on ./content/reading/skull-water/index.md (via TemplateContentPrematureUseError)
[11ty]
[11ty] Original error stack trace: TemplateContentPrematureUseError: Tried to use templateContent too early on ./content/reading/skull-water/index.md
[11ty]     at Object.get [as templateContent] (file:///Users/markllobrera/git-workspace/mllty/node_modules/@11ty/eleventy/src/Template.js:591:14)
[11ty]     at Object.memberLookup (/Users/markllobrera/git-workspace/mllty/node_modules/nunjucks/src/runtime.js:201:17)
[11ty]     at eval (eval at _compile (/Users/markllobrera/git-workspace/mllty/node_modules/nunjucks/src/environment.js:527:18), <anonymous>:93:62)
[11ty]     at Context.<anonymous> (file:///Users/markllobrera/git-workspace/mllty/node_modules/@11ty/eleventy/src/UserConfig.js:307:4)
[11ty]     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Curiously, if I set the date to be a few days in the past, say 2024-06-11T10:51:58.360-04:00 then Eleventy builds fine

Reproduction steps

  1. Clone reading/skull-water branch of my repo: https://github.com/dirtystylus/mllty/tree/reading/skull-water
  2. Update the current date for https://github.com/dirtystylus/mllty/blob/reading/skull-water/content/reading/skull-water/index.md
  3. Run DEBUG=* npx @11ty/eleventy --serve
  4. Receive the error:
Dev:Eleventy:TemplateContent './content/feed/atom.njk' compile() using engine: 'njk' +30ms
  Dev:Eleventy:TemplateContent './content/feed/atom.njk' getCompiledTemplate function created +3ms
[11ty] Unhandled rejection in promise:
[11ty] Tried to use templateContent too early on ./content/reading/skull-water/index.md (via TemplateContentPrematureUseError)
  Eleventy:EleventyErrorHandler (error stack): TemplateContentPrematureUseError: Tried to use templateContent too early on ./content/reading/skull-water/index.md
  Eleventy:EleventyErrorHandler     at Object.get [as templateContent] (file:///Users/markllobrera/git-workspace/mllty/node_modules/@11ty/eleventy/src/Template.js:591:14)
  Eleventy:EleventyErrorHandler     at Object.memberLookup (/Users/markllobrera/git-workspace/mllty/node_modules/nunjucks/src/runtime.js:201:17)
  Eleventy:EleventyErrorHandler     at eval (eval at _compile (/Users/markllobrera/git-workspace/mllty/node_modules/nunjucks/src/environment.js:527:18), <anonymous>:93:62)
  Eleventy:EleventyErrorHandler     at Context.<anonymous> (file:///Users/markllobrera/git-workspace/mllty/node_modules/@11ty/eleventy/src/UserConfig.js:307:4)
  Eleventy:EleventyErrorHandler     at process.processTicksAndRejections (node:internal/process/task_queues:95:5) +0ms

Expected behavior

No response

Reproduction URL

https://github.com/dirtystylus/mllty/tree/reading/skull-water

Screenshots

No response

uncenter commented 4 months ago

Seems like a duplicate of #3136, which now has a potential solution?

dirtystylus commented 4 months ago

@uncenter I will check—I had updated to v2.0.0 of the RSS plugin, but I haven’t read the updated docs to see if my templates need updating (and whether that resolves this issue)

dirtystylus commented 4 months ago

Updating this. The issue was that my RSS feed template was attempting to apply transforms to images stored in Markdown front matter metadata (for my Book and Film content types). So this was generating an error:

{% set book_metadata %}<p><img src="{{metadata.feed_reading.img_base_url}}{{post.data.cover_image}}" alt="Cover image for {{post.data.title}}"/></p><p>Started Reading: {{post.data.start_date | readableDate }}</p><p>Finished Reading: {{post.data.end_date | readableDate }}</p>{% endset %}
        <content type="html">{{book_metadata | transformWithHtmlBase(absolutePostUrl, post.url)}}{{ post.templateContent | transformWithHtmlBase(absolutePostUrl, post.url) }}</content>

I have simplified things to not run the book cover image through a transform:

{% set book_metadata %}<p><img src="{{metadata.feed.img_base_url}}{{post.data.cover_image}}" alt="Cover image for {{post.data.title}}"/></p><p>Started Reading: {{post.data.start_date | readableDate }}</p><p>Finished Reading: {{post.data.end_date | readableDate }}</p>{% endset %}
        <content type="html">{{book_metadata}}{{ post.content | renderTransforms(post.data.page, metadata.base) }}</content>

Have verified that with this change the content date no longer results in this error.