diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a003fb2..4310c201 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ master (unreleased) * Fix regression to make `chokidar` an optional dependency again. * Fix issue when running `npm install nunjucks` with the `--no-bin-links` flag +* Fix regression that broke template caching. Fixes + [#1074](https://github.com/mozilla/nunjucks/issues/1074) 3.1.0 (Feb 19 2018) ------------------- diff --git a/nunjucks/index.js b/nunjucks/index.js index 05b4ea25..2544ef9d 100644 --- a/nunjucks/index.js +++ b/nunjucks/index.js @@ -59,6 +59,9 @@ module.exports = { nodes: nodes, installJinjaCompat: installJinjaCompat, configure: configure, + reset() { + e = undefined; + }, compile(src, env, path, eagerCompile) { if (!e) { configure(); diff --git a/nunjucks/src/environment.js b/nunjucks/src/environment.js index 0d6f7be5..e7fd163b 100644 --- a/nunjucks/src/environment.js +++ b/nunjucks/src/environment.js @@ -233,18 +233,25 @@ class Environment extends Obj { if (err) { if (cb) { cb(err); + return; } else { throw err; } + } + let newTmpl; + if (!info) { + newTmpl = new Template(noopTmplSrc, this, '', eagerCompile); } else { - info = info || {src: noopTmplSrc, path: ''}; - const newTmpl = new Template(info.src, this, info.path, eagerCompile); - if (cb) { - cb(null, newTmpl); - } else { - syncResult = newTmpl; + newTmpl = new Template(info.src, this, info.path, eagerCompile); + if (!info.noCache) { + info.loader.cache[name] = newTmpl; } } + if (cb) { + cb(null, newTmpl); + } else { + syncResult = newTmpl; + } }; lib.asyncIter(this.loaders, (loader, i, next, done) => { diff --git a/package.json b/package.json index a00906dc..7d85227e 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "eslint-plugin-import": "^2.8.0", "expect.js": "*", "express": "4.x", + "fs-extra": "^5.0.0", "get-port": "^3.2.0", "mocha": "*", "mocha-phantomjs-core": "^2.1.2", diff --git a/tests/core.js b/tests/core.js new file mode 100644 index 00000000..948f589e --- /dev/null +++ b/tests/core.js @@ -0,0 +1,76 @@ +(function() { + 'use strict'; + + var expect, + nunjucks, + fs, + os, + path; + + if (typeof require !== 'undefined') { + expect = require('expect.js'); + nunjucks = require('../nunjucks/index'); + fs = require('fs-extra'); + path = require('path'); + os = require('os'); + } else { + expect = window.expect; + nunjucks = window.nunjucks; + } + + function rmdir(dirPath) { + fs.emptyDirSync(dirPath); + fs.rmdir(dirPath); + } + + describe('nunjucks.configure', function() { + var tempdir; + + before(function() { + if (fs && path && os) { + try { + tempdir = fs.mkdtempSync(path.join(os.tmpdir(), 'templates')); + fs.emptyDirSync(tempdir); + } catch (e) { + rmdir(tempdir); + throw e; + } + } + }); + + after(function() { + nunjucks.reset(); + if (typeof tempdir !== 'undefined') { + rmdir(tempdir); + } + }); + + it('should cache templates by default', function() { + if (typeof fs === 'undefined') { + this.skip(); + return; + } + nunjucks.configure(tempdir); + + fs.writeFileSync(tempdir + '/test.html', '{{ name }}', 'utf-8'); + expect(nunjucks.render('test.html', {name: 'foo'})).to.be('foo'); + + fs.writeFileSync(tempdir + '/test.html', '{{ name }}-changed', 'utf-8'); + expect(nunjucks.render('test.html', {name: 'foo'})).to.be('foo'); + }); + + it('should not cache templates with {noCache: true}', function() { + if (typeof fs === 'undefined') { + this.skip(); + return; + } + nunjucks.configure(tempdir, {noCache: true}); + + fs.writeFileSync(tempdir + '/test.html', '{{ name }}', 'utf-8'); + expect(nunjucks.render('test.html', {name: 'foo'})).to.be('foo'); + + fs.writeFileSync(tempdir + '/test.html', '{{ name }}-changed', 'utf-8'); + expect(nunjucks.render('test.html', {name: 'foo'})).to.be('foo-changed'); + }); + }); +}());