85 lines
2.1 KiB
JavaScript
85 lines
2.1 KiB
JavaScript
// import { renderToString as renderToString_ } from '@vue/server-renderer';
|
|
import { renderToNodeStream } from '@vue/server-renderer';
|
|
import { escapeInject } from 'vite-plugin-ssr/server';
|
|
import { RenderErrorPage } from 'vite-plugin-ssr/RenderErrorPage';
|
|
|
|
import { createApp } from './app';
|
|
import { useUser } from '../stores/user';
|
|
import logoUrl from './logo.svg';
|
|
|
|
import { fetchAllShelves } from '../src/shelves';
|
|
|
|
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) {
|
|
try {
|
|
const pageData = await pageContext.exports.getPageData?.(pageContext, pageContext.session);
|
|
const shelves = await fetchAllShelves({ user: pageContext.session.user });
|
|
|
|
return {
|
|
pageContext: {
|
|
// initialState: store.state.value,
|
|
pageData: {
|
|
...pageData,
|
|
shelves,
|
|
},
|
|
me: pageContext.session.user,
|
|
now: new Date(),
|
|
},
|
|
};
|
|
} catch (error) {
|
|
console.error(error);
|
|
|
|
throw RenderErrorPage({
|
|
pageContext: {
|
|
pageProps: error,
|
|
me: pageContext.session.user,
|
|
now: new Date(),
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|
|
export {
|
|
render,
|
|
onBeforeRender,
|
|
};
|
|
|
|
export const passToClient = ['urlPathname', 'initialState', 'pageData', 'pageProps', 'routeParams', 'me', 'now'];
|