From 18dd8b7eac3f0eb8f73f74ed5ef6a7474f35f9d3 Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Sat, 5 Oct 2019 15:50:00 +0800 Subject: [PATCH] fix(plugin-search): generate data on demand (#492) * add new hook: onCreateRenderer * Remove unused code --- packages/saber-plugin-search/lib/index.js | 13 +++++-------- packages/saber/lib/index.js | 2 ++ packages/saber/vue-renderer/lib/index.js | 8 ++++++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/packages/saber-plugin-search/lib/index.js b/packages/saber-plugin-search/lib/index.js index 5a04cf711..13367513a 100644 --- a/packages/saber-plugin-search/lib/index.js +++ b/packages/saber-plugin-search/lib/index.js @@ -79,18 +79,15 @@ 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.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 85ffcedf2..4983fd2c8 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')