mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 08:02:01 +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
|
} = 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 */
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
8
test/fixtures/basic/nuxt.config.js
vendored
8
test/fixtures/basic/nuxt.config.js
vendored
@ -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
|
||||||
},
|
},
|
||||||
|
@ -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'))
|
||||||
|
Loading…
Reference in New Issue
Block a user