[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:
Sébastien Chopin 2018-08-21 18:35:46 +02:00 committed by GitHub
parent 799c487d8c
commit 820f0fae1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 1 deletions

View File

@ -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 */

View File

@ -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)
}

View File

@ -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

View File

@ -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
},

View File

@ -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'))