Skip to content

Commit 5c2e062

Browse files
authored
cache option to eta engine in production (#251)
* Use cache option for eta templates * Use prod variable * Add eta test for cache in production mode
1 parent a1e16f7 commit 5c2e062

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,10 @@ function fastifyView (fastify, opts, next) {
570570
templates: options.templates ? options.templates : lru
571571
})
572572

573-
const config = Object.assign({ views: templatesDir }, options)
573+
const config = Object.assign({
574+
cache: prod,
575+
views: templatesDir
576+
}, options)
574577

575578
data = Object.assign({}, defaultCtx, this.locals, data)
576579
// Append view extension (Eta will append '.eta' by default,

test/test-eta.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,40 @@ test('fastify.view with eta engine and callback in production mode', t => {
800800
})
801801
})
802802

803+
test('fastify.view with eta engine in production mode should use cache', t => {
804+
t.plan(1)
805+
806+
const fastify = Fastify()
807+
const cache = {
808+
cache: {},
809+
get (k) {
810+
if (typeof this.cache[k] !== 'undefined') {
811+
t.pass()
812+
}
813+
return this.cache[k]
814+
},
815+
define (k, v) {
816+
this.cache[k] = v
817+
}
818+
}
819+
820+
fastify.register(pointOfView, {
821+
production: true,
822+
engine: {
823+
eta: eta
824+
},
825+
options: {
826+
templates: cache
827+
}
828+
})
829+
830+
fastify.ready(async () => {
831+
await fastify.view('templates/index.eta', { text: 'test' })
832+
await fastify.view('templates/index.eta', { text: 'test' }) // This should trigger the cache
833+
fastify.close()
834+
})
835+
})
836+
803837
test('fastify.view with eta engine and custom cache', t => {
804838
t.plan(9)
805839
const fastify = Fastify()

0 commit comments

Comments
 (0)