// https://vike.dev/onRenderHtml import config from 'config'; import { renderToString as renderToString_ } from '@vue/server-renderer'; import { escapeInject, dangerouslySkipEscape } from 'vike/server'; /* eslint-disable-line import/extensions */ import { createApp } from './app.js'; function getTitle(location) { if (!location) { return config.title; } return `${config.title} - ${location.slice(0, 1).toUpperCase()}${location.slice(1)}`; } async function renderToString(app) { let err; // Workaround: renderToString_() swallows errors in production, see https://github.com/vuejs/core/issues/7876 app.config.errorHandler = (err_) => { // eslint-disable-line no-param-reassign err = err_; }; const appHtml = await renderToString_(app); if (err) { throw err; } return appHtml; } async function onRenderHtml(pageContext) { const { Page, pageProps } = pageContext; // This onRenderHtml() hook only supports SSR, see https://vike.dev/render-modes for how to modify // onRenderHtml() to support SPA if (!Page) { throw new Error('My render() hook expects pageContext.Page to be defined'); } const app = createApp(Page, pageProps, pageContext); const appHtml = await renderToString(app); // See https://vike.dev/head const { documentProps } = pageContext.exports; const title = getTitle(documentProps?.title || pageContext.title); const desc = (documentProps && documentProps.description) || 'traxxx'; const documentHtml = escapeInject` ${config.analytics.enabled ? dangerouslySkipEscape(``) : ''} ${title}
${dangerouslySkipEscape(appHtml)}
`; return { documentHtml, }; } export { onRenderHtml };