jstransformers / jstransformer-nunjucks

Nunjucks support for JSTransformers.
http://npm.im/jstransformer-nunjucks
MIT License
6 stars 6 forks source link

Error: The Transform "nunjucks" does not support rendering synchronously. #25

Closed wdmtech closed 6 years ago

wdmtech commented 6 years ago

Hi!

I have a very typical nunjucks setup on Metalsmith

I can output variables, and overwrite blocks in child templates:

{% extends "layouts/page.njk" %}

{% block content %}

  child content block

  {{ i18n.heading }}

{% endblock %}

but as soon as I want to do something like:

{% extends "layouts/page.njk" %}

{% block content %}

  child content block

  {{ default(i18n.heading, 'Welcome!') }}

{% endblock %}

I get the following error:

Error: The Transform "nunjucks" does not support rendering synchronously.
    at Transformer.render (/Users/stoaty/Dev/websites/menu-translators/static/node_modules/jstransformer/index.js:291:13)
    at resolve (/Users/stoaty/Dev/websites/menu-translators/static/node_modules/metalsmith-in-place/lib/index.js:60:33)
    at new Promise (<anonymous>)
    at render (/Users/stoaty/Dev/websites/menu-translators/static/node_modules/metalsmith-in-place/lib/index.js:29:10)
    at Promise.all.validFiles.map.filename (/Users/stoaty/Dev/websites/menu-translators/static/node_modules/metalsmith-in-place/lib/index.js:142:7)
    at Array.map (<anonymous>)
    at Ware.<anonymous> (/Users/stoaty/Dev/websites/menu-translators/static/node_modules/metalsmith-in-place/lib/index.js:141:16)
    at Ware.<anonymous> (/Users/stoaty/Dev/websites/menu-translators/static/node_modules/wrap-fn/index.js:45:19)
    at next (/Users/stoaty/Dev/websites/menu-translators/static/node_modules/ware/lib/index.js:85:20)
    at /Users/stoaty/Dev/websites/menu-translators/static/node_modules/wrap-fn/index.js:121:18
error Command failed with exit code 1.

The message is a bit vague to me - I though I was doing things synchronously?

Does anyone have any experience with this error?

I've looked around all afternoon and I can't find much except this SO post:

https://stackoverflow.com/questions/49799507/using-nunjucks-includes-with-metalsmith-in-place-causing-rendering-error

wdmtech commented 6 years ago

I can replicate this using the repo by @wernerglinka:

https://github.com/wernerglinka/minms

and modifying the following line

https://github.com/wernerglinka/minms/blob/71b150991b1b408bbb75e2938e9ab97aa3d98508/dev/content/index.njk#L11

to:

<h1>{{ default(title, 'missing title') }}</h1>

wdmtech commented 6 years ago

Maybe it's the default() function (and my inexperience with Nunjucks!)

I've discovered you can write:


{{ variable or 'default text' }}

Instead. And filters seem to work... 🤔

Anyway, I'm going to keep investigating for now.

wernerglinka commented 6 years ago

@wdmtech I typically get this error when I misspell something or use a wrong variable name.

wernerglinka commented 6 years ago

This might explain it: https://github.com/mozilla/nunjucks/issues/569

wdmtech commented 6 years ago

Thanks @wernerglinka, this seems to reflect my experience

wdmtech commented 6 years ago

I'll close this for now as it seems to be a problem with Nunjucks