|
import { renderToString } from '@vue/server-renderer'
|
|
import { dangerouslySkipEscape, escapeInject } from 'vite-plugin-ssr'
|
|
import type { PageContextBuiltIn } from 'vite-plugin-ssr'
|
|
|
|
import { createApp } from './app'
|
|
import logoUrl from './logo.svg'
|
|
import type { PageContext } from './types'
|
|
|
|
export { render }
|
|
// See https://vite-plugin-ssr.com/data-fetching
|
|
export const passToClient = ['pageProps', 'urlPathname']
|
|
|
|
async function render(pageContext: PageContextBuiltIn & PageContext) {
|
|
const app = createApp(pageContext)
|
|
const appHtml = await renderToString(app)
|
|
|
|
// See https://vite-plugin-ssr.com/head
|
|
const { documentProps = {} } = pageContext.pageExports as {
|
|
documentProps: {
|
|
title?: string
|
|
description?: string
|
|
}
|
|
}
|
|
const title = (documentProps.title) || 'Vite SSR app'
|
|
const desc = (documentProps.description) || 'App using Vite + vite-plugin-ssr'
|
|
|
|
const documentHtml = escapeInject`<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<link rel="icon" href="${logoUrl}" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<meta name="description" content="${desc}" />
|
|
<title>${title}</title>
|
|
</head>
|
|
<body>
|
|
<div id="app">${dangerouslySkipEscape(appHtml)}</div>
|
|
</body>
|
|
</html>`
|
|
|
|
return {
|
|
documentHtml,
|
|
pageContext: {
|
|
// We can add some `pageContext` here, which is useful if we want to do page redirection https://vite-plugin-ssr.com/page-redirection
|
|
},
|
|
}
|
|
}
|