diff --git a/index.js b/index.js index 07f383f1..c97fddf9 100644 --- a/index.js +++ b/index.js @@ -121,6 +121,15 @@ function fastifyView (fastify, opts, next) { return this }) + if (!fastify.hasReplyDecorator('locals')) { + fastify.decorateReply('locals', null) + + fastify.addHook('onRequest', (req, reply, done) => { + reply.locals = {} + done() + }) + } + function getPage (page, extension) { const pageLRU = `getPage-${page}-${extension}` let result = lru.get(pageLRU) diff --git a/test/test.js b/test/test.js index 1f71b6ac..11290ac8 100644 --- a/test/test.js +++ b/test/test.js @@ -133,6 +133,37 @@ test('reply.view exist', t => { }) }) +test('reply.locals exist', t => { + t.plan(6) + const fastify = Fastify() + + fastify.register(require('../index'), { + engine: { + ejs: require('ejs') + } + }) + + fastify.get('/', (req, reply) => { + t.ok(reply.locals) + reply.send({ hello: 'world' }) + }) + + fastify.listen({ port: 0 }, err => { + t.error(err) + + sget({ + method: 'GET', + url: 'http://localhost:' + fastify.server.address().port + }, (err, response, body) => { + t.error(err) + t.equal(response.statusCode, 200) + t.equal(response.headers['content-length'], '' + body.length) + t.same(JSON.parse(body), { hello: 'world' }) + fastify.close() + }) + }) +}) + test('reply.view can be returned from async function to indicate response processing finished', t => { t.plan(6) const fastify = Fastify()