Skip to content

Commit

Permalink
perf: cache layout access check (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
mweberxyz committed Mar 6, 2024
1 parent 988042f commit 2fa7ee8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
16 changes: 13 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,13 +645,23 @@ async function fastifyView (fastify, opts) {
}

function hasAccessToLayoutFile (fileName, ext) {
const layoutKey = `layout-${fileName}-${ext}`
let result = fastify[viewCache].get(layoutKey)

if (typeof result === 'boolean') {
return result
}

try {
accessSync(join(templatesDir, getPage(fileName, ext)))

return true
result = true
} catch (e) {
return false
result = false
}

fastify[viewCache].set(layoutKey, result)

return result
}
}

Expand Down
7 changes: 6 additions & 1 deletion test/test-handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ test('fastify.view with handlebars engine with layout option on render', t => {
})

test('fastify.view with handlebars engine with invalid layout option on render should throw', t => {
t.plan(3)
t.plan(5)

const fastify = Fastify()
const handlebars = require('handlebars')
Expand All @@ -349,6 +349,11 @@ test('fastify.view with handlebars engine with invalid layout option on render s
t.ok(err instanceof Error)
t.equal(err.message, 'unable to access template "./templates/invalid-layout.hbs"')
})
// repeated for test coverage of layout access check lru
fastify.view('./templates/index-for-layout.hbs', data, { layout: './templates/invalid-layout.hbs' }, (err, compiled) => {
t.ok(err instanceof Error)
t.equal(err.message, 'unable to access template "./templates/invalid-layout.hbs"')
})
})
})

Expand Down

0 comments on commit 2fa7ee8

Please sign in to comment.