shack/pages/posts/post.page.server.js

41 lines
814 B
JavaScript

import { RenderErrorPage } from 'vite-plugin-ssr/RenderErrorPage';
import { fetchShelf } from '../../src/shelves';
import { fetchPost } from '../../src/posts';
import { fetchPostComments } from '../../src/comments';
async function getPageData(pageContext) {
const [shelf, post, comments] = await Promise.all([
fetchShelf(pageContext.routeParams.id),
fetchPost(pageContext.routeParams.postId),
fetchPostComments(pageContext.routeParams.postId),
]);
if (!shelf) {
throw RenderErrorPage({
pageContext: {
pageProps: {
errorInfo: 'This shelf does not exist',
},
},
});
}
if (!post) {
throw RenderErrorPage({
pageContext: {
pageProps: {
errorInfo: 'This post does not exist',
},
},
});
}
return {
shelf,
post,
comments,
};
}
export { getPageData };