From 273cf144b454194864815d187f06b28e60122f02 Mon Sep 17 00:00:00 2001 From: keyan Date: Thu, 8 Jul 2021 12:27:52 -0500 Subject: [PATCH] ssr item for metatags --- pages/items/[id].js | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/pages/items/[id].js b/pages/items/[id].js index 555efc3d..24b279ec 100644 --- a/pages/items/[id].js +++ b/pages/items/[id].js @@ -9,21 +9,41 @@ import { ITEM_FIELDS } from '../../fragments/items' import { gql, useQuery } from '@apollo/client' import styles from '../../styles/item.module.css' import Seo from '../../components/seo' +import ApolloClient from '../../api/client' + +// ssr the item without comments so that we can populate metatags +export async function getServerSideProps ({ req, params: { id } }) { + const { error, data: { item } } = await (await ApolloClient(req)).query({ + query: + gql` + ${ITEM_FIELDS} + { + item(id: ${id}) { + ...ItemFields + text + } + }` + }) + + if (!item || error) { + return { + notFound: true + } + } -export async function getServerSideProps ({ params: { id } }) { return { props: { - id + item } } } -export default function FullItem ({ id }) { +export default function FullItem ({ item }) { const query = gql` ${ITEM_FIELDS} ${COMMENTS} { - item(id: ${id}) { + item(id: ${item.id}) { ...ItemFields text comments { @@ -34,6 +54,7 @@ export default function FullItem ({ id }) { return ( + ) @@ -60,7 +81,6 @@ function LoadItem ({ query }) { return ( <> - {item.parentId ? : (