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

No-arg shortcode functions randomly fail #3678

Open
jmole opened this issue Mar 5, 2025 · 2 comments
Open

No-arg shortcode functions randomly fail #3678

jmole opened this issue Mar 5, 2025 · 2 comments

Comments

@jmole
Copy link

jmole commented Mar 5, 2025

Read comment 1, I have a new hypothesis for this bug.

Operating system

macOS

Eleventy

3.0.0

Describe the bug

I ran into this issue when learning 11ty while prompting Claude 3.7.

I wanted a way to include a string in a template, and claude recommended a shortcode:

  // Add a shortcode for the current year
  eleventyConfig.addShortcode("year", () => {
    return new Date().getFullYear();
  });

However, trying to use this in a njk template yielded mixed results. Sometimes it appeared to work, other times, it failed to build:

<p>&copy; {% year %} All rights reserved.</p>
[11ty] 1. Having trouble writing to "./_site/index.html" from "./src/index.njk" (via EleventyTemplateError)
[11ty] 2. (./src/includes/base.njk) [Line 20, Column 18]
[11ty]   unknown block tag: year (via Template render error)

Playing around for a while, I found that if I wrote the function like so, it would work:

  eleventyConfig.addShortcode("year", function () {
    return new Date().getFullYear();
  });

Reproduction steps

Use an arrow function to define a short code.

eleventyConfig.addShortcode("works", function () {
  return "works"
});
eleventyConfig.addShortcode("fails", () => {
  return "fails"
});

And use in an njk template:

<html>
  <body>
    <p>{% works %}</p>
    <p>{% fails %}</p>
  </body>
</html>

Expected behavior

I would have expected the arrow function to work, but if that is not possible due to some technical reason, then a more informative error would have been great.

Reproduction URL

No response

Screenshots

No response

@jmole
Copy link
Author

jmole commented Mar 5, 2025

Actually, it looks like some other issue is at play here. I don't know what's triggering it, but I have been making other unrelated changes to the templates that cause the same error to occur, even with standard function() { ... } syntax.

If I then update the function to change it, it starts working again:

original code

  eleventyConfig.addShortcode("year", function () {
    return new Date().getFullYear(); 
  });

< make some unrelated change to a template >

[11ty] 1. Having trouble writing to "./_site/index.html" from "./src/index.njk" (via EleventyTemplateError)
[11ty] 2. (./src/includes/base.njk) [Line 20, Column 18]
[11ty]   unknown block tag: year (via Template render error)

❌ Compile fails

< make a change to the shortcode function >

  eleventyConfig.addShortcode("year", function () {
    return new Date().getFullYear();  
    // add a comment here that doesn't change anything
  });

✅ Everything compiles again

Hypothesis: there is some kind of optimization that is happening which removes this function from scope on a rebuild when a template changes.

@jmole jmole changed the title arrow function in short codes doesn't work No-arg shortcode functions randomly fail Mar 5, 2025
@jmole
Copy link
Author

jmole commented Mar 6, 2025

One more thing - this has all been happening while eleventy --serve is running.

I haven't tried stopping the node process and restarting when encountering this error.

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

No branches or pull requests

1 participant