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

Exposing rendering context to Nunjucks shortcodes fails in Eleventy 3.0.0-alpha.17 #3368

Closed
groenroos opened this issue Jul 16, 2024 · 1 comment
Labels
bug release: canary A release on the canary channel

Comments

@groenroos
Copy link

Operating system

macOS Sonoma 14.4.1

Eleventy

3.0.0-alpha.17

Describe the bug

Following the technique in this comment, I've created a shortcode that loads a partial within a post, and injects it with the rendering context. Simplified example:

post.md
---
title: Post
---

Text ...

{% promo "newsletter" %}

More text ...
.eleventy.js
import addPromo from './lib/promo.js';

export default function(eleventyConfig) {
	/* Some collection */
	eleventyConfig.addCollection('stuff', function(collection) {
		return collection.getFilteredByTag('stuff');
	});

	/* Add helper */
	addPromo(eleventyConfig);

	return {
		markdownTemplateEngine: "njk",
	};
};
lib/promo.js
import fs from 'fs';
import { EleventyRenderPlugin } from '@11ty/eleventy';

const RenderManager = EleventyRenderPlugin.RenderManager;

function addPromo(eleventyConfig) {
	const rm = new RenderManager();

	eleventyConfig.on('eleventy.config', cfg => {
		rm.templateConfig = cfg;
	});

	eleventyConfig.addAsyncShortcode('promo', async function (promoType) {
		const fn = await rm.compile(fs.readFileSync('./_includes/promo.njk').toString(), 'njk');
		return fn({ ...this.ctx.getAll(), promoType });
	});
}

export default addPromo;
_includes/promo.njk
<h1>Sign up for our {{ promoType }}!</h1>

This works perfectly in Eleventy 2.0.0, but in Eleventy 3.0.0-alpha.17, it fails with an unhelpful error:

Problem writing Eleventy templates: (more in DEBUG output)
1. Having trouble rendering njk template ./post.md (via TemplateContentRenderError)
2. (./post.md)
  EleventyShortcodeError: Error with Nunjucks shortcode `promo` (via Template render error)
3. Error in your Eleventy config file '.eleventy.js'. (via Template render error)

Original error stack trace: EleventyConfigError: Error in your Eleventy config file '.eleventy.js'.
    at TemplateConfig.requireLocalConfigFile (/node_modules/@11ty/eleventy/src/TemplateConfig.js:376:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async TemplateConfig.mergeConfig (/node_modules/@11ty/eleventy/src/TemplateConfig.js:404:41)
    at async TemplateConfig.init (/node_modules/@11ty/eleventy/src/TemplateConfig.js:199:17)
    at async RenderManager.init (/node_modules/@11ty/eleventy/src/Plugins/RenderPlugin.js:394:3)
    at async RenderManager.compile (/node_modules/@11ty/eleventy/src/Plugins/RenderPlugin.js:424:3)
    at async Object.<anonymous> (/lib/promo.js:14:14)

Changing ...this.ctx.getAll() to just ...this.ctx seems to result in the object I want; but via a try catch I can surface this additional error that seems to happen as many times as the shortcode is used in the project:

EleventyConfigError: Error in your Eleventy config file '.eleventy.js'.
    at TemplateConfig.requireLocalConfigFile (/node_modules/@11ty/eleventy/src/TemplateConfig.js:376:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async TemplateConfig.mergeConfig (/node_modules/@11ty/eleventy/src/TemplateConfig.js:404:41)
    at async TemplateConfig.init (/node_modules/@11ty/eleventy/src/TemplateConfig.js:199:17)
    at async RenderManager.init (/node_modules/@11ty/eleventy/src/Plugins/RenderPlugin.js:394:3)
    at async RenderManager.compile (/node_modules/@11ty/eleventy/src/Plugins/RenderPlugin.js:424:3)
    at async Object.<anonymous> (/lib/promo.js:15:15) {
  originalError: UserConfigError: config.addCollection(stuff) already exists. Try a different name for your collection.
      at UserConfig.addCollection (/node_modules/@11ty/eleventy/src/UserConfig.js:1051:10)
      at default (/.eleventy.js:110:17)
      at TemplateConfig.requireLocalConfigFile (/node_modules/@11ty/eleventy/src/TemplateConfig.js:368:26)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async TemplateConfig.mergeConfig (/node_modules/@11ty/eleventy/src/TemplateConfig.js:404:41)
      at async TemplateConfig.init (/node_modules/@11ty/eleventy/src/TemplateConfig.js:199:17)
      at async RenderManager.init (/node_modules/@11ty/eleventy/src/Plugins/RenderPlugin.js:394:3)
      at async RenderManager.compile (/node_modules/@11ty/eleventy/src/Plugins/RenderPlugin.js:424:3)
      at async Object.<anonymous> (/lib/promo.js:15:15)
}

It appears to (somehow) try to re-register the first collection listed in .eleventy.js, but I'm not entirely sure how or why it's doing that.

Reproduction steps

  1. Create a project as per the above example files
  2. Build it with Eleventy 3.0.0-alpha.17
  3. Build will fail

Expected behavior

Build should not fail; or at least a more useful error should be returned.

Reproduction URL

No response

Screenshots

No response

@zachleat zachleat added this to the Eleventy 3.0.0 milestone Jul 17, 2024
@zachleat zachleat added release: canary A release on the canary channel bug and removed needs-triage labels Jul 17, 2024
@zachleat
Copy link
Member

Fix shipping with 3.0.0-alpha.18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug release: canary A release on the canary channel
Projects
None yet
Development

No branches or pull requests

2 participants