shack/renderer/_default.page.server.js

85 lines
2.1 KiB
JavaScript
Raw Normal View History

2023-06-03 21:32:50 +00:00
// import { renderToString as renderToString_ } from '@vue/server-renderer';
import { renderToNodeStream } from '@vue/server-renderer';
import { escapeInject } from 'vite-plugin-ssr/server';
2023-06-25 17:52:00 +00:00
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';
2023-06-11 03:32:02 +00:00
const { app } = createApp(pageContext);
2023-06-03 21:32:50 +00:00
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>
2023-06-03 21:32:50 +00:00
<div id="app">${stream}</div>
</body>
</html>
`;
const userStore = useUser();
userStore.user = pageContext.session.user;
return {
2023-06-03 21:32:50 +00:00
documentHtml,
pageContext: {
2023-06-11 03:32:02 +00:00
// initialState: store.state.value,
2023-06-03 21:32:50 +00:00
enableEagerStreaming: true,
},
};
}
2023-06-11 03:32:02 +00:00
async function onBeforeRender(pageContext) {
2023-06-25 17:52:00 +00:00
try {
const pageData = await pageContext.exports.getPageData?.(pageContext, pageContext.session);
const shelves = await fetchAllShelves({ user: pageContext.session.user });
2023-06-11 03:32:02 +00:00
2023-06-25 17:52:00 +00:00
return {
pageContext: {
// initialState: store.state.value,
pageData: {
...pageData,
shelves,
},
2023-06-25 17:52:00 +00:00
me: pageContext.session.user,
now: new Date(),
},
};
} catch (error) {
console.error(error);
throw RenderErrorPage({
pageContext: {
pageProps: error,
2023-06-25 17:52:00 +00:00
me: pageContext.session.user,
now: new Date(),
},
});
}
2023-06-11 03:32:02 +00:00
}
export {
render,
2023-06-11 03:32:02 +00:00
onBeforeRender,
};
2023-06-25 17:52:00 +00:00
export const passToClient = ['urlPathname', 'initialState', 'pageData', 'pageProps', 'routeParams', 'me', 'now'];