shack/renderer/_default.page.server.js

66 lines
1.6 KiB
JavaScript

// import { renderToString as renderToString_ } from '@vue/server-renderer';
import { renderToNodeStream } from '@vue/server-renderer';
import { escapeInject } from 'vite-plugin-ssr/server';
import { createApp } from './app';
import { useUser } from '../stores/user';
import logoUrl from './logo.svg';
async function render(pageContext) {
// See https://vite-plugin-ssr.com/head
const { documentProps } = pageContext.exports;
const title = (documentProps && documentProps.title) || 'shack';
const desc = (documentProps && documentProps.description) || 'Shack';
const { app } = createApp(pageContext);
const stream = renderToNodeStream(app);
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">${stream}</div>
</body>
</html>
`;
const userStore = useUser();
userStore.user = pageContext.session.user;
return {
documentHtml,
pageContext: {
// initialState: store.state.value,
enableEagerStreaming: true,
},
};
}
async function onBeforeRender(pageContext) {
const pageData = await pageContext.exports.getPageData?.(pageContext, pageContext.session);
return {
pageContext: {
// initialState: store.state.value,
pageData,
user: pageContext.session.user,
now: new Date(),
},
};
}
export {
render,
onBeforeRender,
};
export const passToClient = ['urlPathname', 'initialState', 'pageData', 'pageProps', 'routeParams', 'user', 'now'];