From add169ff0781c31c0babc761a829fcea656eb609 Mon Sep 17 00:00:00 2001 From: Fran Dios Date: Tue, 16 Feb 2021 21:02:47 +0900 Subject: [PATCH] feat: Inject custom properties in SSR context. Closes #5 --- core/entry-server.js | 18 ++++++++++++------ example/src/pages/Homepage.vue | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/core/entry-server.js b/core/entry-server.js index 0e9c4c9..fefb14e 100644 --- a/core/entry-server.js +++ b/core/entry-server.js @@ -39,12 +39,14 @@ export default function (App, { routes, base }, hook) { await router.isReady() - const initialState = JSON.stringify( - router.currentRoute.value.meta.state || {} - ) + // This can be injected with useSSRContext() in setup functions + const context = { + request, + ...extra, + initialState: router.currentRoute.value.meta.state || {}, + } - const ctx = {} - let html = await renderToString(app, ctx) + let html = await renderToString(app, context) const [helmet = ''] = html.match(/]*?>(.|\s)*?<\/html>/im) || [] let [, head = ''] = helmet.match(/]*?>((.|\s)*?)<\/head>/im) || [] @@ -59,11 +61,15 @@ export default function (App, { routes, base }, hook) { html = html.replace(helmet, '') } - const dependencies = manifest ? findDependencies(ctx.modules, manifest) : [] + const dependencies = manifest + ? findDependencies(context.modules, manifest) + : [] if (preload && dependencies.length > 0) { head += renderPreloadLinks(dependencies) } + const initialState = JSON.stringify(context.initialState || {}) + return { // This string is replaced at build time // and injects all the previous variables. diff --git a/example/src/pages/Homepage.vue b/example/src/pages/Homepage.vue index 972a104..b419dae 100644 --- a/example/src/pages/Homepage.vue +++ b/example/src/pages/Homepage.vue @@ -14,7 +14,9 @@