2022-09-15 03:01:56 -07:00
|
|
|
import { GetServerSideProps } from "next";
|
2022-09-14 22:56:45 -07:00
|
|
|
import ItemSearchPageToolbar from "../../../src/app/components/ItemSearchPageToolbar";
|
|
|
|
import ItemPage from "../../../src/app/ItemPage";
|
|
|
|
import PageLayout from "../../../src/app/PageLayout";
|
2022-09-15 03:01:56 -07:00
|
|
|
import { gql, loadGraphqlQuery } from "../../../src/server/ssr-graphql";
|
2022-09-14 22:56:45 -07:00
|
|
|
import type { NextPageWithLayout } from "../../_app";
|
2022-09-15 03:01:56 -07:00
|
|
|
// @ts-ignore doesn't understand module.exports
|
|
|
|
import { oneDay, oneWeek } from "../../../src/server/util";
|
2022-09-14 22:16:39 -07:00
|
|
|
|
|
|
|
const ItemPageWrapper: NextPageWithLayout = () => {
|
|
|
|
return <ItemPage />;
|
|
|
|
};
|
|
|
|
|
[WIP] Refactor to renderWithLayout function
Okay, when I saw the recipe in the Next.js docs with `getLayout`, I was like "psh this API is so confusing, this should just be a component"
anyway now we see why it wasn't a component: the _whole point_ of it was to circumvent the usual React diffing algorithm's belief that two different components _can't_ ever share UI. But here we were, making different `layoutComponent`s that were meant to share UI, lol!
Anyway, if you just _return JSX in a function_, the React diffing algorithm never sees that it came from a different place, so it's generous when diffing them. Neat!
But I still changed the recipe's `getLayout` name to `renderWithLayout`, because it just confused me so much at first lol, I thought it was going to like, return a layout function? This is much clearer verbing to me imo
2022-09-14 22:50:56 -07:00
|
|
|
ItemPageWrapper.renderWithLayout = (children) => {
|
2022-09-14 22:16:39 -07:00
|
|
|
return (
|
|
|
|
<PageLayout>
|
|
|
|
<ItemSearchPageToolbar marginBottom="8" />
|
|
|
|
{children}
|
|
|
|
</PageLayout>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-09-15 03:01:56 -07:00
|
|
|
export const getServerSideProps: GetServerSideProps = async ({
|
|
|
|
params,
|
|
|
|
res,
|
|
|
|
}) => {
|
|
|
|
if (params?.itemId == null) {
|
|
|
|
throw new Error(`assertion error: itemId param is missing`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the most important, most stable item data to get onto the page ASAP.
|
|
|
|
// We'll cache it real hard, to help it load extra-fast for popular items!
|
|
|
|
const { errors, graphqlState } = await loadGraphqlQuery({
|
|
|
|
query: gql`
|
|
|
|
query ItemsIndex_GetServerSideProps($itemId: ID!) {
|
|
|
|
item(id: $itemId) {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
thumbnailUrl
|
|
|
|
description
|
|
|
|
isNc
|
|
|
|
isPb
|
|
|
|
createdAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: { itemId: params.itemId },
|
|
|
|
});
|
|
|
|
if (errors) {
|
|
|
|
console.warn(
|
|
|
|
`[SSR: /items/[itemId]] Skipping GraphQL preloading, got errors:`
|
|
|
|
);
|
|
|
|
for (const error of errors) {
|
|
|
|
console.warn(`[SSR: /items/[itemId]]`, error);
|
|
|
|
}
|
|
|
|
return { props: { graphqlState: {} } };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache this very aggressively, because it's such stable data!
|
|
|
|
res.setHeader(
|
|
|
|
"Cache-Control",
|
|
|
|
`public, s-maxage=${oneDay}, stale-while-revalidate=${oneWeek}`
|
|
|
|
);
|
|
|
|
|
|
|
|
return { props: { graphqlState } };
|
|
|
|
};
|
|
|
|
|
2022-09-14 22:16:39 -07:00
|
|
|
export default ItemPageWrapper;
|