Skip to content

Commit

Permalink
fix: respect cache render options
Browse files Browse the repository at this point in the history
  • Loading branch information
mastodon0 authored and harttle committed Apr 3, 2020
1 parent 3cf8072 commit a93f11d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/liquid-options.ts
Expand Up @@ -89,11 +89,13 @@ export function normalize (options?: LiquidOptions): NormalizedOptions {
if (options.hasOwnProperty('root')) {
options.root = normalizeStringArray(options.root)
}
let cache: Cache<Template[]> | 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<Template[]>(1024) : undefined
options.cache = cache
if (options.hasOwnProperty('cache')) {
let cache: Cache<Template[]> | 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<Template[]>(1024) : undefined
options.cache = cache
}
return options as NormalizedOptions
}

Expand Down
2 changes: 1 addition & 1 deletion src/liquid.ts
Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions test/integration/liquid/cache.ts
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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')
})
})
})

0 comments on commit a93f11d

Please sign in to comment.