mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
feat(server): add new render:beforeResponse
hook (#7469)
This commit is contained in:
parent
752d587ef1
commit
c540b133a4
@ -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
|
||||
|
@ -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()
|
||||
})
|
||||
|
1
packages/types/config/hooks.d.ts
vendored
1
packages/types/config/hooks.d.ts
vendored
@ -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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user