From a57c050e5326a6b72dc1fecbb7f447cf1da80f79 Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Wed, 2 Oct 2019 00:55:41 +0800 Subject: [PATCH 1/2] add new hook: onCreateRenderer --- packages/saber-plugin-search/lib/index.js | 15 ++++++++------- packages/saber/lib/index.js | 2 ++ packages/saber/vue-renderer/lib/index.js | 8 ++++++-- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/packages/saber-plugin-search/lib/index.js b/packages/saber-plugin-search/lib/index.js index 5a04cf711..7e03b27fb 100644 --- a/packages/saber-plugin-search/lib/index.js +++ b/packages/saber-plugin-search/lib/index.js @@ -79,18 +79,19 @@ exports.apply = (api, options) => { api.browserApi.add(join(__dirname, 'saber-browser.js')) if (api.dev) { - api.hooks.onCreatePages.tapPromise(ID, async () => { - db = await generateDatabase() - }) + // api.hooks.onCreateRenderer.tapPromise(ID, async () => { + // db = await generateDatabase() + // }) api.hooks.onCreateServer.tap(ID, server => { - server.get('/_saber/plugin-search/:locale.json', (req, res) => { - const db = getLocale(req.params.locale) - if (db) { + server.get('/_saber/plugin-search/:locale.json', async (req, res) => { + db = await generateDatabase() + const dbByLocale = getLocale(req.params.locale) + if (dbByLocale) { res.writeHead(200, { 'Content-Type': 'application/json' }) - return res.end(JSON.stringify(db)) + return res.end(JSON.stringify(dbByLocale)) } res.statusCode = 404 diff --git a/packages/saber/lib/index.js b/packages/saber/lib/index.js index dba57fc25..d14e19dc0 100644 --- a/packages/saber/lib/index.js +++ b/packages/saber/lib/index.js @@ -59,6 +59,8 @@ class Saber { emitPages: new AsyncSeriesHook(), // Call this hook to manipulate a page, it's usually used by file watcher manipulatePage: new AsyncSeriesHook(['data']), + // Call when server renderer is created and updated + onCreateRenderer: new AsyncSeriesHook(['renderer', 'isFirstTime']), // Called before exporting a page as static HTML file beforeExportPage: new AsyncSeriesHook(['context', 'exportedPage']), // Called after exporting a page diff --git a/packages/saber/vue-renderer/lib/index.js b/packages/saber/vue-renderer/lib/index.js index d3f11ba0e..d7b88a02c 100644 --- a/packages/saber/vue-renderer/lib/index.js +++ b/packages/saber/vue-renderer/lib/index.js @@ -266,9 +266,11 @@ class VueRenderer { ]) } - initRenderer({ clientManifest, serverBundle } = {}) { + async initRenderer({ clientManifest, serverBundle } = {}) { const { createBundleRenderer } = require('vue-server-renderer') + const isFirstTime = !this.renderer + if (serverBundle && clientManifest) { log.verbose(`Creating server renderer`) this.renderer = createBundleRenderer(serverBundle, { @@ -279,6 +281,8 @@ class VueRenderer { }) } + await this.api.hooks.onCreateRenderer.promise(this.renderer, isFirstTime) + return this.renderer } @@ -310,7 +314,7 @@ class VueRenderer { const clientManifest = readJSON( this.api.resolveCache('bundle-manifest/client.json') ) - const renderer = this.initRenderer({ serverBundle, clientManifest }) + const renderer = await this.initRenderer({ serverBundle, clientManifest }) const getOutputFilePath = permalink => { const filename = permalink.endsWith('.html') From c11a3c318eb07e26409b27d35f66218eb898b574 Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Sat, 5 Oct 2019 15:01:38 +0800 Subject: [PATCH 2/2] Remove unused code --- packages/saber-plugin-search/lib/index.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/saber-plugin-search/lib/index.js b/packages/saber-plugin-search/lib/index.js index 7e03b27fb..13367513a 100644 --- a/packages/saber-plugin-search/lib/index.js +++ b/packages/saber-plugin-search/lib/index.js @@ -79,10 +79,6 @@ exports.apply = (api, options) => { api.browserApi.add(join(__dirname, 'saber-browser.js')) if (api.dev) { - // api.hooks.onCreateRenderer.tapPromise(ID, async () => { - // db = await generateDatabase() - // }) - api.hooks.onCreateServer.tap(ID, server => { server.get('/_saber/plugin-search/:locale.json', async (req, res) => { db = await generateDatabase()