Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: cache layout access check #414

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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