🍿 Fix 'Tried to use templateContent too early' error with Eleventy
Snacks (🍿) are my collection of recipes for solving problems. Often recorded (and cleaned up) from actual discussions where I'm involved in helping others with technical problems. Not all solutions are mine but I started collecting them in one place because you never know when you need one.
I rebuilt my Eleventy website based on my old setup but kept running into an annoying error during development when running npx @11ty/eleventy --serve
development server:
[11ty] Original error stack trace: TemplateContentPrematureUseError: Tried to use templateContent too early on ./weeklies/2021-52.md
It happened every time I made any change that caused a rewrite and the stack trace was pointing to either a blog post or a Weeklies entry.
After a while of googling and debugging, I figured out it happened not in those blog posts or Weeklies entries but when generating the RSS feed for both. Apparently the RSS build was happening before processing of the content pieces needed for it.
By adding
eleventyImport:
collections: ["weeklies"]
(where "weeklies"
refers to what ever collection you're building your feed from) into the front matter of files that generate the feeds, the errors went away.
This is documented in Eleventy docs but it took me long to understand where the issue was and where to make the fix.