Search code examples
templateshandlebars.jsnestjsaws-serverless

Handlebars template are not updated during compilation in .build folder


Situation

I'm currently developing a serverless project which renders some HTML (.hbs templates) with NestJS and Handlebars.

  1. When I first run nest build && sls offline start, I can test my app endpoints in offline mode with postman => the templates are rendered perfectly.
  2. But if I now update something inside a .hbs template and then stop/restart the offline server, my template changes are not taking in account when I test my endpoints (it is like my template are compiled only once).
  3. If I change something in a .css file and then stop/restart the offline server, the compiled files seem to be overrided as expected and I can see the new CSS in the rendered templates.

Currently, I have to delete the .build/ folder and then stop/restart the offline server to see my template changes in the rendered HTML when I test my endpoints...

How can I avoid this workaround ?

I would like my compiled templates to be at least updated when I just stop/restart the offline server.
And the best solution would be to update the compiled templates "in watch mode" but I've read somewhere that templates are only compiled once when the server starts (https://github.com/nest-modules/mailer/issues/42)
Here is my nest-cli.json conf:

{
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "assets": ["**/*.hbs"],
    "watchAssets": true
  }
}

Solution

  • Maybe it's not the best solution, but I solved similar problem this way: I wrote gulp task, which watches .hbs files and imitates change of main.ts file.

    function watchHandlebars() {
      watch('src/views/**/*.hbs').on('all', () => {
        console.log('Restarting Nest');
        const path = './src/main.ts';
        fs.utimesSync(path, new Date(), new Date());
      });
    }