From c540b133a4e64ac9d168804359f0f2b83c7205ca Mon Sep 17 00:00:00 2001 From: Dumitru Motpan Date: Fri, 12 Jun 2020 16:21:20 +0300 Subject: [PATCH] feat(server): add new `render:beforeResponse` hook (#7469) --- packages/server/src/middleware/nuxt.js | 2 ++ packages/server/test/middleware/nuxt.test.js | 14 ++++++++++---- packages/types/config/hooks.d.ts | 1 + 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/server/src/middleware/nuxt.js b/packages/server/src/middleware/nuxt.js index 39bb81248d..2efec4b58a 100644 --- a/packages/server/src/middleware/nuxt.js +++ b/packages/server/src/middleware/nuxt.js @@ -42,6 +42,7 @@ export default ({ options, nuxt, renderRoute, resources }) => async function nux const etag = hash ? hash(html, options.render.etag) : generateETag(html, options.render.etag) if (fresh(req.headers, { etag })) { res.statusCode = 304 + await nuxt.callHook('render:beforeResponse', url, result, context) res.end() await nuxt.callHook('render:routeDone', url, result, context) return @@ -80,6 +81,7 @@ export default ({ options, nuxt, renderRoute, resources }) => async function nux res.setHeader('Content-Type', 'text/html; charset=utf-8') res.setHeader('Accept-Ranges', 'none') // #3870 res.setHeader('Content-Length', Buffer.byteLength(html)) + await nuxt.callHook('render:beforeResponse', url, result, context) res.end(html, 'utf8') await nuxt.callHook('render:routeDone', url, result, context) return html diff --git a/packages/server/test/middleware/nuxt.test.js b/packages/server/test/middleware/nuxt.test.js index 5027a17974..7a33e206e5 100644 --- a/packages/server/test/middleware/nuxt.test.js +++ b/packages/server/test/middleware/nuxt.test.js @@ -47,9 +47,10 @@ describe('server: nuxtMiddleware', () => { expect(context.renderRoute).toBeCalledTimes(1) expect(context.renderRoute).toBeCalledWith(req.url, { req, res }) - expect(context.nuxt.callHook).toBeCalledTimes(2) + expect(context.nuxt.callHook).toBeCalledTimes(3) expect(context.nuxt.callHook).nthCalledWith(1, 'render:route', req.url, result, { req, res }) - expect(context.nuxt.callHook).nthCalledWith(2, 'render:routeDone', req.url, result, { req, res }) + expect(context.nuxt.callHook).nthCalledWith(2, 'render:beforeResponse', req.url, result, { req, res }) + expect(context.nuxt.callHook).nthCalledWith(3, 'render:routeDone', req.url, result, { req, res }) expect(res.setHeader).toBeCalledTimes(3) expect(res.setHeader).nthCalledWith(1, 'Content-Type', 'text/html; charset=utf-8') @@ -94,9 +95,10 @@ describe('server: nuxtMiddleware', () => { const html = await nuxtMiddleware(req, res, next) - expect(context.nuxt.callHook).toBeCalledTimes(2) + expect(context.nuxt.callHook).toBeCalledTimes(3) expect(context.nuxt.callHook).nthCalledWith(1, 'render:route', req.url, result, { req, res, nuxt }) - expect(context.nuxt.callHook).nthCalledWith(2, 'render:routeDone', req.url, result, { req, res, nuxt }) + expect(context.nuxt.callHook).nthCalledWith(2, 'render:beforeResponse', req.url, result, { req, res, nuxt }) + expect(context.nuxt.callHook).nthCalledWith(3, 'render:routeDone', req.url, result, { req, res, nuxt }) expect(res.statusCode).toEqual(404) expect(html).toEqual(result.html) @@ -145,6 +147,10 @@ describe('server: nuxtMiddleware', () => { await nuxtMiddleware(req, res, next) expect(res.statusCode).toEqual(304) + expect(context.nuxt.callHook).toBeCalledTimes(3) + expect(context.nuxt.callHook).nthCalledWith(1, 'render:route', req.url, result, { req, res }) + expect(context.nuxt.callHook).nthCalledWith(2, 'render:beforeResponse', req.url, result, { req, res }) + expect(context.nuxt.callHook).nthCalledWith(3, 'render:routeDone', req.url, result, { req, res }) expect(res.end).toBeCalledTimes(1) expect(res.end).toBeCalledWith() }) diff --git a/packages/types/config/hooks.d.ts b/packages/types/config/hooks.d.ts index 60ab9d073a..0feda8fb7e 100644 --- a/packages/types/config/hooks.d.ts +++ b/packages/types/config/hooks.d.ts @@ -45,6 +45,7 @@ export interface NuxtOptionsHooks { route?(url: string, result: any, context: any): void routeContext?(context: any): void routeDone?(url: string, result: any, context: any): void + beforeResponse?(url: string, result: any, context: any): void setupMiddleware?(app: ConnectServer): void } }