From a93f11d25abfa2a3c2727f293cad05e1df04a906 Mon Sep 17 00:00:00 2001 From: Martin Schuster Date: Thu, 2 Apr 2020 17:57:03 +0200 Subject: [PATCH] fix: respect cache render options --- src/liquid-options.ts | 12 ++++--- src/liquid.ts | 2 +- test/integration/liquid/cache.ts | 56 ++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/liquid-options.ts b/src/liquid-options.ts index eec218ee21..c283f4517a 100644 --- a/src/liquid-options.ts +++ b/src/liquid-options.ts @@ -89,11 +89,13 @@ export function normalize (options?: LiquidOptions): NormalizedOptions { if (options.hasOwnProperty('root')) { options.root = normalizeStringArray(options.root) } - let cache: Cache | undefined - if (typeof options.cache === 'number') cache = options.cache > 0 ? new LRU(options.cache) : undefined - else if (typeof options.cache === 'object') cache = options.cache - else cache = options.cache ? new LRU(1024) : undefined - options.cache = cache + if (options.hasOwnProperty('cache')) { + let cache: Cache | undefined + if (typeof options.cache === 'number') cache = options.cache > 0 ? new LRU(options.cache) : undefined + else if (typeof options.cache === 'object') cache = options.cache + else cache = options.cache ? new LRU(1024) : undefined + options.cache = cache + } return options as NormalizedOptions } diff --git a/src/liquid.ts b/src/liquid.ts index f74347f759..d3893bab8d 100644 --- a/src/liquid.ts +++ b/src/liquid.ts @@ -75,7 +75,7 @@ export class Liquid { } for (const filepath of paths) { - const { cache } = this.options + const { cache } = options if (cache) { const tpls = yield cache.read(filepath) if (tpls) return tpls diff --git a/test/integration/liquid/cache.ts b/test/integration/liquid/cache.ts index 789b577b38..4a3a6fcf6d 100644 --- a/test/integration/liquid/cache.ts +++ b/test/integration/liquid/cache.ts @@ -132,6 +132,34 @@ describe('LiquidOptions#cache', function () { const y = await engine.renderFile('foo') expect(y).to.equal('foo') }) + it('should respect passed in cache=false option', async function () { + const engine = new Liquid({ + root: '/root/', + extname: '.html', + cache: true + }) + mock({ '/root/files/foo.html': 'foo' }) + const x = await engine.renderFile('files/foo') + expect(x).to.equal('foo') + mock({ '/root/files/foo.html': 'bar' }) + const y = await engine.renderFile('files/foo') + expect(y).to.equal('foo') + const z = await engine.renderFile('files/foo', undefined, { cache: false }) + expect(z).to.equal('bar') + }) + it('should use cache when passing in other options', async function () { + const engine = new Liquid({ + root: '/root/', + extname: '.html', + cache: true + }) + mock({ '/root/files/foo.html': 'foo' }) + const x = await engine.renderFile('files/foo') + expect(x).to.equal('foo') + mock({ '/root/files/foo.html': 'bar' }) + const y = await engine.renderFile('files/foo', undefined, { greedy: true }) + expect(y).to.equal('foo') + }) }) describe('#renderFileSync', function () { @@ -174,5 +202,33 @@ describe('LiquidOptions#cache', function () { const y = await engine.renderFile('foo') expect(y).to.equal('foo') }) + it('should respect passed in cache=false option', async function () { + const engine = new Liquid({ + root: '/root/', + extname: '.html', + cache: true + }) + mock({ '/root/files/foo.html': 'foo' }) + const x = engine.renderFileSync('files/foo') + expect(x).to.equal('foo') + mock({ '/root/files/foo.html': 'bar' }) + const y = engine.renderFileSync('files/foo') + expect(y).to.equal('foo') + const z = engine.renderFileSync('files/foo', undefined, { cache: false }) + expect(z).to.equal('bar') + }) + it('should use cache when passing in other options', async function () { + const engine = new Liquid({ + root: '/root/', + extname: '.html', + cache: true + }) + mock({ '/root/files/foo.html': 'foo' }) + const x = engine.renderFileSync('files/foo') + expect(x).to.equal('foo') + mock({ '/root/files/foo.html': 'bar' }) + const y = engine.renderFileSync('files/foo', undefined, { greedy: true }) + expect(y).to.equal('foo') + }) }) })