feat(server): add new render:beforeResponse hook (#7469)

This commit is contained in:
Dumitru Motpan 2020-06-12 16:21:20 +03:00 committed by GitHub
parent 752d587ef1
commit c540b133a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 4 deletions

View File

@ -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) const etag = hash ? hash(html, options.render.etag) : generateETag(html, options.render.etag)
if (fresh(req.headers, { etag })) { if (fresh(req.headers, { etag })) {
res.statusCode = 304 res.statusCode = 304
await nuxt.callHook('render:beforeResponse', url, result, context)
res.end() res.end()
await nuxt.callHook('render:routeDone', url, result, context) await nuxt.callHook('render:routeDone', url, result, context)
return 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('Content-Type', 'text/html; charset=utf-8')
res.setHeader('Accept-Ranges', 'none') // #3870 res.setHeader('Accept-Ranges', 'none') // #3870
res.setHeader('Content-Length', Buffer.byteLength(html)) res.setHeader('Content-Length', Buffer.byteLength(html))
await nuxt.callHook('render:beforeResponse', url, result, context)
res.end(html, 'utf8') res.end(html, 'utf8')
await nuxt.callHook('render:routeDone', url, result, context) await nuxt.callHook('render:routeDone', url, result, context)
return html return html

View File

@ -47,9 +47,10 @@ describe('server: nuxtMiddleware', () => {
expect(context.renderRoute).toBeCalledTimes(1) expect(context.renderRoute).toBeCalledTimes(1)
expect(context.renderRoute).toBeCalledWith(req.url, { req, res }) 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(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).toBeCalledTimes(3)
expect(res.setHeader).nthCalledWith(1, 'Content-Type', 'text/html; charset=utf-8') 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) 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(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(res.statusCode).toEqual(404)
expect(html).toEqual(result.html) expect(html).toEqual(result.html)
@ -145,6 +147,10 @@ describe('server: nuxtMiddleware', () => {
await nuxtMiddleware(req, res, next) await nuxtMiddleware(req, res, next)
expect(res.statusCode).toEqual(304) 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).toBeCalledTimes(1)
expect(res.end).toBeCalledWith() expect(res.end).toBeCalledWith()
}) })

View File

@ -45,6 +45,7 @@ export interface NuxtOptionsHooks {
route?(url: string, result: any, context: any): void route?(url: string, result: any, context: any): void
routeContext?(context: any): void routeContext?(context: any): void
routeDone?(url: string, result: any, context: any): void routeDone?(url: string, result: any, context: any): void
beforeResponse?(url: string, result: any, context: any): void
setupMiddleware?(app: ConnectServer): void setupMiddleware?(app: ConnectServer): void
} }
} }