mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
[WIP] Hooks: render:routeContext and render:routeDone (#3773)
* hooks: Add render:routeDone hook * hooks: Deprecate render:context and add render:routeContext * refactor: delegate context to routeContext * test: Add test for render:routeDone hook * lint: Fix lint issue
This commit is contained in:
parent
799c487d8c
commit
820f0fae1a
@ -21,6 +21,7 @@ export default async function nuxtMiddleware(req, res, next) {
|
||||
} = result
|
||||
|
||||
if (redirected) {
|
||||
this.nuxt.callHook('render:routeDone', req.url, result, context)
|
||||
return html
|
||||
}
|
||||
if (error) {
|
||||
@ -33,6 +34,7 @@ export default async function nuxtMiddleware(req, res, next) {
|
||||
if (fresh(req.headers, { etag })) {
|
||||
res.statusCode = 304
|
||||
res.end()
|
||||
this.nuxt.callHook('render:routeDone', req.url, result, context)
|
||||
return
|
||||
}
|
||||
res.setHeader('ETag', etag)
|
||||
@ -79,6 +81,7 @@ export default async function nuxtMiddleware(req, res, next) {
|
||||
res.setHeader('Content-Type', 'text/html; charset=utf-8')
|
||||
res.setHeader('Content-Length', Buffer.byteLength(html))
|
||||
res.end(html, 'utf8')
|
||||
this.nuxt.callHook('render:routeDone', req.url, result, context)
|
||||
return html
|
||||
} catch (err) {
|
||||
/* istanbul ignore if */
|
||||
|
@ -82,6 +82,10 @@ export default class Nuxt {
|
||||
if (!name || typeof fn !== 'function') {
|
||||
return
|
||||
}
|
||||
if (name === 'render:context') {
|
||||
name = 'render:routeContext'
|
||||
consola.warn('render:context hook has been deprecated, please use render:routeContext')
|
||||
}
|
||||
this._hooks[name] = this._hooks[name] || []
|
||||
this._hooks[name].push(fn)
|
||||
}
|
||||
|
@ -351,7 +351,7 @@ export default class Renderer {
|
||||
HEAD += context.renderResourceHints()
|
||||
}
|
||||
|
||||
await this.nuxt.callHook('render:context', context.nuxt)
|
||||
await this.nuxt.callHook('render:routeContext', context.nuxt)
|
||||
|
||||
const serializedSession = `window.__NUXT__=${serialize(context.nuxt, {
|
||||
isJSON: true
|
||||
|
8
test/fixtures/basic/nuxt.config.js
vendored
8
test/fixtures/basic/nuxt.config.js
vendored
@ -1,5 +1,7 @@
|
||||
import path from 'path'
|
||||
|
||||
let _nuxt
|
||||
|
||||
export default {
|
||||
render: {
|
||||
dist: {
|
||||
@ -42,6 +44,7 @@ export default {
|
||||
modulesDir: path.join(__dirname, '..', '..', '..', 'node_modules'),
|
||||
hooks: {
|
||||
ready(nuxt) {
|
||||
_nuxt = nuxt
|
||||
nuxt.__hook_ready_called__ = true
|
||||
},
|
||||
build: {
|
||||
@ -49,6 +52,11 @@ export default {
|
||||
builder.__hook_built_called__ = true
|
||||
}
|
||||
},
|
||||
render: {
|
||||
routeDone(url) {
|
||||
_nuxt.__hook_render_routeDone__ = url
|
||||
}
|
||||
},
|
||||
bad: null,
|
||||
'': true
|
||||
},
|
||||
|
@ -51,6 +51,10 @@ describe('basic dev', () => {
|
||||
expect(html.includes('<h1>My component!</h1>')).toBe(true)
|
||||
})
|
||||
|
||||
test('Check render:routeDone hook called', () => {
|
||||
expect(nuxt.__hook_render_routeDone__).toBe('/stateless')
|
||||
})
|
||||
|
||||
// test('/_nuxt/test.hot-update.json should returns empty html', async t => {
|
||||
// try {
|
||||
// await rp(url('/_nuxt/test.hot-update.json'))
|
||||
|
Loading…
Reference in New Issue
Block a user