[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 } = result
if (redirected) { if (redirected) {
this.nuxt.callHook('render:routeDone', req.url, result, context)
return html return html
} }
if (error) { if (error) {
@ -33,6 +34,7 @@ export default async function nuxtMiddleware(req, res, next) {
if (fresh(req.headers, { etag })) { if (fresh(req.headers, { etag })) {
res.statusCode = 304 res.statusCode = 304
res.end() res.end()
this.nuxt.callHook('render:routeDone', req.url, result, context)
return return
} }
res.setHeader('ETag', etag) 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-Type', 'text/html; charset=utf-8')
res.setHeader('Content-Length', Buffer.byteLength(html)) res.setHeader('Content-Length', Buffer.byteLength(html))
res.end(html, 'utf8') res.end(html, 'utf8')
this.nuxt.callHook('render:routeDone', req.url, result, context)
return html return html
} catch (err) { } catch (err) {
/* istanbul ignore if */ /* istanbul ignore if */

View File

@ -82,6 +82,10 @@ export default class Nuxt {
if (!name || typeof fn !== 'function') { if (!name || typeof fn !== 'function') {
return 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] = this._hooks[name] || []
this._hooks[name].push(fn) this._hooks[name].push(fn)
} }

View File

@ -351,7 +351,7 @@ export default class Renderer {
HEAD += context.renderResourceHints() 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, { const serializedSession = `window.__NUXT__=${serialize(context.nuxt, {
isJSON: true isJSON: true

View File

@ -1,5 +1,7 @@
import path from 'path' import path from 'path'
let _nuxt
export default { export default {
render: { render: {
dist: { dist: {
@ -42,6 +44,7 @@ export default {
modulesDir: path.join(__dirname, '..', '..', '..', 'node_modules'), modulesDir: path.join(__dirname, '..', '..', '..', 'node_modules'),
hooks: { hooks: {
ready(nuxt) { ready(nuxt) {
_nuxt = nuxt
nuxt.__hook_ready_called__ = true nuxt.__hook_ready_called__ = true
}, },
build: { build: {
@ -49,6 +52,11 @@ export default {
builder.__hook_built_called__ = true builder.__hook_built_called__ = true
} }
}, },
render: {
routeDone(url) {
_nuxt.__hook_render_routeDone__ = url
}
},
bad: null, bad: null,
'': true '': true
}, },

View File

@ -51,6 +51,10 @@ describe('basic dev', () => {
expect(html.includes('<h1>My component!</h1>')).toBe(true) 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 => { // test('/_nuxt/test.hot-update.json should returns empty html', async t => {
// try { // try {
// await rp(url('/_nuxt/test.hot-update.json')) // await rp(url('/_nuxt/test.hot-update.json'))