提交 1862086f 编写于 作者: fxy060608's avatar fxy060608

feat(ssr): support teleport head meta

上级 1fa3c412
......@@ -24,7 +24,7 @@ export async function render(url, manifest = {}) {
// itself on ctx.modules. After the render, ctx.modules would contain all the
// components that have been instantiated during this render call.
const ctx = {}
const html = await renderToString(app, ctx)
const appHtml = await renderToString(app, ctx)
// the SSR manifest generated by Vite contains module -> chunk/asset mapping
// which we can then use to determine what files need to be preloaded for this
......@@ -40,7 +40,15 @@ export async function render(url, manifest = {}) {
__uniSSR[UNI_SSR_STORE] = store.state
}
const appContext = renderAppContext(ctx)
return [html, preloadLinks, appContext, ctx[UNI_SSR_TITLE] || '']
const title = ctx[UNI_SSR_TITLE] || ''
const headMeta = renderHeadMeta(ctx)
return {
title,
headMeta,
preloadLinks,
appHtml,
appContext,
}
}
function renderPreloadLinks(modules, manifest) {
......@@ -74,3 +82,12 @@ function renderPreloadLink(file) {
function renderAppContext(ctx) {
return `<script>window.__uniSSR = ${JSON.stringify(ctx[UNI_SSR])}</script>`
}
function renderHeadMeta(ctx) {
if (!ctx.__teleportBuffers || !ctx.__teleportBuffers.__head__) {
return ''
}
return ctx.__teleportBuffers.__head__
.map((buffer) => buffer.toString())
.join('\n')
}
......@@ -81,7 +81,8 @@ export async function createSSRServer(
)
).render
const [appHtml, preloadLinks, appContext, title] = await render(url)
const { title, headMeta, preloadLinks, appHtml, appContext } =
await render(url)
const icon = template.includes('rel="icon"')
? ''
......@@ -89,6 +90,7 @@ export async function createSSRServer(
const html = template
.replace(/<title>(.*?)<\/title>/, `${icon}<title>${title}</title>`)
.replace(`<!--head-meta-->`, headMeta)
.replace(`<!--preload-links-->`, preloadLinks)
.replace(`<!--app-html-->`, appHtml)
.replace(`<!--app-context-->`, appContext)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册