fix(vite-node): on-demand manifest (#5017)

This commit is contained in:
Anthony Fu 2022-06-27 20:08:52 +08:00 committed by GitHub
parent 026903c5e7
commit 7efdb486db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 9 deletions

View File

@ -30,7 +30,9 @@ interface RenderResult {
}
// @ts-ignore
const getClientManifest = () => import('#build/dist/server/client.manifest.mjs').then(r => r.default || r)
const getClientManifest = () => import('#build/dist/server/client.manifest.mjs')
.then(r => r.default || r)
.then(r => typeof r === 'function' ? r() : r)
// @ts-ignore
const getServerEntry = () => process.env.NUXT_NO_SSR ? Promise.resolve(null) : import('#build/dist/server/server.mjs').then(r => r.default || r)
@ -46,15 +48,22 @@ const getSSRRenderer = lazyCachedFunction(async () => {
if (!createSSRApp) { throw new Error('Server bundle is not available') }
// Create renderer
const renderToString = async (input, context) => {
const html = await _renderToString(input, context)
return `<div id="__nuxt">${html}</div>`
}
return createRenderer(createSSRApp, {
const renderer = createRenderer(createSSRApp, {
clientManifest,
renderToString,
publicPath: buildAssetsURL()
})
async function renderToString (input, context) {
const html = await _renderToString(input, context)
// In development with vite-node, the manifest is on-demand and will be available after rendering
if (process.dev && process.env.NUXT_VITE_NODE_OPTIONS) {
renderer.rendererContext.updateManifest(await getClientManifest())
}
return `<div id="__nuxt">${html}</div>`
}
return renderer
})
// -- SPA Renderer --

View File

@ -3,6 +3,4 @@ import { getViteNodeOptions } from './vite-node-shared.mjs'
const viteNodeOptions = getViteNodeOptions()
const manifest = await $fetch('/manifest', { baseURL: viteNodeOptions.baseURL })
export default manifest
export default () => $fetch('/manifest', { baseURL: viteNodeOptions.baseURL })