Skip to content

Commit

Permalink
fix(plugin-search): generate data on demand (#492)
Browse files Browse the repository at this point in the history
* add new hook: onCreateRenderer

* Remove unused code
  • Loading branch information
egoist authored Oct 5, 2019
1 parent 002bb91 commit 18dd8b7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
13 changes: 5 additions & 8 deletions packages/saber-plugin-search/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions packages/saber/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions packages/saber/vue-renderer/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -279,6 +281,8 @@ class VueRenderer {
})
}

await this.api.hooks.onCreateRenderer.promise(this.renderer, isFirstTime)

return this.renderer
}

Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 18dd8b7

Please sign in to comment.