2

I'm using Express JS together with Twing (node.js implementation of Twig template engine) and created a template file (index.twig.html). I'm trying to update the template without stoping and starting node.js over and over again, but it seems to be impossible.

According to https://nightlycommit.github.io/twing/api.html I can set cache to false and auto_reload to true, so that it enables templates to be recompiled whenever their content changes instead of fetching them from the cache.

const express = require('express');
const { TwingEnvironment, TwingLoaderFilesystem } = require('twing');

let loader = new TwingLoaderFilesystem('./templates');
let twing = new TwingEnvironment(loader, { // https://nightlycommit.github.io/twing/api.html
    debug: true,
    cache: false,
    auto_reload: true, //for debugging
});
const app = express();
const port = 3000;

app.use('/public', express.static('./public'));

app.get('/', (req, res) => {
    twing.render('index.twig.html', req.params).then((output) => {
        res.end(output);
    });
});

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`);
});

I also thought of disabling the cache of Express JS itself with the tips from this topic: How to disable webpage caching in ExpressJS + NodeJS? Didn't help either. However, looking at the console of my browser, the HTTP response code says 200, so I don't think Express JS is the issue.

Eric MORAND
  • 6,373
  • 4
  • 27
  • 34
Chris
  • 63
  • 1
  • 3

1 Answers1

0

See the comment here:

https://gitlab.com/nightlycommit/twing/-/issues/538

Specifically, in your case, you have to instanciate a fresh environment in your route handler, like this:

app.get('/', (req, res) => {
    let twing = new TwingEnvironment(loader, {
        debug: true
    });

    twing.render('index.twig.html', req.params).then((output) => {
        res.end(output);
    });
});

Long explanation

Twing environment uses two different layers of cache:

  • An internal cache. This cache can't be bypassed and can't be controled: if a compiled version of a template exists in this cache, it will always be used by the environment to load and render the template. And whenever the environment loads a template, it caches its compiled version in this internal cache. This cache has the same lifetime as the environment: whenever the environment is destroyed, it is destroyed.

  • An external cache, provided by the cache option. This cache can be bypassed if the template source file is more recent than the cached version and auto_reload is set to true. It has an undefined lifetime: it survives when the environment is destroyed. Said differently: it is persistent.

So, if you always want the fresh version of a template to be used, but also want to benefit from the external cache performance boost, you have to instanciate a new version of the envionrment (to get a fresh internal cache instance), set the cache option to a cache instance (or a path, to use the filesystem cache) and set auto_reload to true.

Eric MORAND
  • 6,373
  • 4
  • 27
  • 34