Skip to content

Add a custom nunjucks transformer, with caching#5150

Open
domoscargin wants to merge 4 commits intomainfrom
bk-cache-nunjucks-env-in-transformer
Open

Add a custom nunjucks transformer, with caching#5150
domoscargin wants to merge 4 commits intomainfrom
bk-cache-nunjucks-env-in-transformer

Conversation

@domoscargin
Copy link
Contributor

@domoscargin domoscargin commented Feb 10, 2026

What

@metalsmith/in-place and @metalsmith/layouts both use a Nunjucks jstransformer under the hood to render templates.

This transformer sets up a new Nunjucks environment per file, causing in-place and layouts to take a long time to run.

npm run build without transformer cache

npm run build on main

This PR adds Nunjucks environment caching to the transformer, dramatically reducing build times for both plugins

npm run build with transformer cache

1  npm run build with transformer cache

Cached templates

The templates in /views are cached by the Nunjucks environment. This means if we change one of these files, that change will not propagate to all the templates where it's used.

To account for this, we watch the views directory and reset the Nunjucks environment cache whenever we detect a change to a file there.

We previously set the Nunjucks option noCache to false. If we set that back to true, it obliterates any gains from this PR's caching, so having this extra check/reset is preferable.

@netlify
Copy link

netlify bot commented Feb 10, 2026

You can preview this change here:

Name Link
🔨 Latest commit b69e5a8
🔍 Latest deploy log https://app.netlify.com/projects/govuk-design-system-preview/deploys/69a0c9cc88b94400086c0c1f
😎 Deploy Preview https://deploy-preview-5150--govuk-design-system-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@domoscargin domoscargin force-pushed the bk-cache-nunjucks-env-in-transformer branch 2 times, most recently from 2ecd33b to f6b7a3c Compare February 12, 2026 21:02
@domoscargin domoscargin changed the title [WIP] Add a custom nunjucks transformer, with caching Add a custom nunjucks transformer, with caching Feb 12, 2026
@domoscargin domoscargin marked this pull request as ready for review February 12, 2026 22:08
@domoscargin domoscargin requested a review from a team as a code owner February 12, 2026 22:08
@domoscargin domoscargin force-pushed the bk-cache-nunjucks-env-in-transformer branch from f6b7a3c to bed3a05 Compare February 19, 2026 15:52
Adds a simple cache check against the nunjucks environment used in the transformer.

Currently, each file that gets passed to the transformer by @metalsmith/in-place and @metalsmith-layouts sets up a new Nunjucks environment, which costs a lot of time.

We only use one Nunjucks environment during build, though, so we don't need to keep setting a new one up.

This dramatically cuts down in-place and layouts' build times.
@domoscargin domoscargin force-pushed the bk-cache-nunjucks-env-in-transformer branch from bed3a05 to b69e5a8 Compare February 26, 2026 22:31
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 this pull request may close these issues.

1 participant