0

I've managed to get Nunjucks working with Sails.js, however it seems that the changes are not being picked up until I restart the server. I'll automatically see the changes reflected once or twice, but after that, even manually refreshing the browser will not show my changes.

I implemented LiveReload with the advice here:

Get livereload to work with Sails.js

but I don't suspect it's an issue with LiveReload.

Has anyone else gotten Sails.js and Nunjucks to play nicely together? If so, how?

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
Niall O'Brien
  • 182
  • 1
  • 1
  • 9
  • I've updated my boilerplate repo to show where I'm replacing Jade with Nunjucks. https://github.com/niallobrien/sails-boilerplate/tree/feature/replace-jade-with-nunjucks – Niall O'Brien Dec 08 '15 at 20:21

3 Answers3

3

The problem is nunjucks itself. It has a watch option which by default is set to false:

You can enable it in sails/config/bootstrap.js:

var nunjucks = require('nunjucks')
 module.exports.bootstrap = function(cb) {

    nunjucks.configure({
    watch:true
  })
  // It's very important to trigger this callback method when you are finished
  // with the bootstrap!  (otherwise your server will never lift, since it's waiting on the bootstrap)
  cb();
};

In combination with the livereload everything works fine.

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
Aron
  • 1,179
  • 15
  • 29
0

in /config/views.js

  engine: {
          ext: 'html',
          fn: function (str, options, fn) {
              var engine = require('nunjucks');
                  engine.configure('views', {
                    autoescape       : true,
                    throwOnUndefined : true,
                    trimBlocks       : true,
                    lstripBlocks     : true,
                    express          : sails.hooks.http.app,
                    watch            : true,
                    noCache          : false,
                    web              : {
                          useCache : true,
                          async    : false
                    }
              });
              engine.render(str, options, fn);
          }
      },
0

For Sails.js 1 the solution has slightly changed:

In /config/views.js

module.exports.views = {
    ...
    getRenderFn: () => {
      // Import nunjucks.
      const nunjucks = require('nunjucks');

      // Configure nunjucks.
      const env = nunjucks.configure('views', {
        autoescape       : false,
        throwOnUndefined : true,
        trimBlocks       : true,
        lstripBlocks     : true,
        watch            : true,
        noCache          : false,
        web              : {
          useCache : true,
          async    : false
        }
      });

      // Here you can add filter
      env.addFilter('filtername', (name) => {
        return name;
      });

      return nunjucks.render;
    }
}

Hope this will help someone ;)

Bogdan Le
  • 2,216
  • 1
  • 20
  • 21