From 820f0fae1a94793a2b9cf75e398531ab77e5bc8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Tue, 21 Aug 2018 18:35:46 +0200 Subject: [PATCH] [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 --- lib/core/middleware/nuxt.js | 3 +++ lib/core/nuxt.js | 4 ++++ lib/core/renderer.js | 2 +- test/fixtures/basic/nuxt.config.js | 8 ++++++++ test/unit/basic.dev.test.js | 4 ++++ 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/core/middleware/nuxt.js b/lib/core/middleware/nuxt.js index 839f457d3c..8c1a14c591 100644 --- a/lib/core/middleware/nuxt.js +++ b/lib/core/middleware/nuxt.js @@ -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 */ diff --git a/lib/core/nuxt.js b/lib/core/nuxt.js index 6061a3f002..b55fd4511b 100644 --- a/lib/core/nuxt.js +++ b/lib/core/nuxt.js @@ -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) } diff --git a/lib/core/renderer.js b/lib/core/renderer.js index a21b309e32..99d897b193 100644 --- a/lib/core/renderer.js +++ b/lib/core/renderer.js @@ -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 diff --git a/test/fixtures/basic/nuxt.config.js b/test/fixtures/basic/nuxt.config.js index b1006e5a00..1abd93fded 100644 --- a/test/fixtures/basic/nuxt.config.js +++ b/test/fixtures/basic/nuxt.config.js @@ -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 }, diff --git a/test/unit/basic.dev.test.js b/test/unit/basic.dev.test.js index 1cd0654ac8..b723e31c32 100644 --- a/test/unit/basic.dev.test.js +++ b/test/unit/basic.dev.test.js @@ -51,6 +51,10 @@ describe('basic dev', () => { expect(html.includes('

My component!

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