fix: :bug: Show 404 when UUID not found
Tim Izzo tim@octree.ch
Wed, 28 Sep 2022 07:16:39 +0000
4 files changed,
69 insertions(+),
30 deletions(-)
M
frontend/lib/pageUtils.ts
→
frontend/lib/pageUtils.ts
@@ -7,7 +7,12 @@
type ServerSideExtension = ( context: any, apolloClient: ApolloClient<any> -) => Promise<Object | void>; +) => Promise<ExtensionResult | void>; + +type ExtensionResult = { + props?: Object; + notFound?: boolean; +}; const getServerSideProps = (extension?: ServerSideExtension) => async (context: any) => {@@ -38,8 +43,15 @@ query: ProfileDocument,
}); let extensionProps = {}; - if (extension) - extensionProps = (await extension(context, apolloClient)) || {}; + if (extension) { + const extensionReturn = await extension(context, apolloClient); + extensionProps = extensionReturn?.props || {}; + if (extensionReturn?.notFound) { + return { + notFound: true, + }; + } + } return { props: {
M
frontend/pages/e/[uuid]/details.tsx
→
frontend/pages/e/[uuid]/details.tsx
@@ -242,19 +242,28 @@ export const getServerSideProps = pageUtils.getServerSideProps(
async (context, apolloClient) => { const {uuid} = context.query; const {host = ''} = context.req.headers; + let event = null; // Fetch event - const {data} = await apolloClient.query({ - query: EventByUuidDocument, - variables: {uuid}, - }); - const event = data?.eventByUUID?.data; + try { + const {data} = await apolloClient.query({ + query: EventByUuidDocument, + variables: {uuid}, + }); + event = data?.eventByUUID?.data; + } catch (error) { + return { + notFound: true, + }; + } return { - eventUUID: uuid, - metas: { - title: event?.attributes?.name || '', - url: `https://${host}${context.resolvedUrl}`, + props: { + eventUUID: uuid, + metas: { + title: event?.attributes?.name || '', + url: `https://${host}${context.resolvedUrl}`, + }, }, }; }
M
frontend/pages/e/[uuid]/index.tsx
→
frontend/pages/e/[uuid]/index.tsx
@@ -82,13 +82,20 @@ async (context, apolloClient) => {
const {uuid} = context.query; const {host = ''} = context.req.headers; const session = await getSession(context); + let event = null; // Fetch event - const {data} = await apolloClient.query({ - query: EventByUuidDocument, - variables: {uuid}, - }); - const event = data?.eventByUUID?.data; + try { + const {data} = await apolloClient.query({ + query: EventByUuidDocument, + variables: {uuid}, + }); + event = data?.eventByUUID?.data; + } catch (error) { + return { + notFound: true, + }; + } // Fetch user vehicles if (session)@@ -97,10 +104,12 @@ query: FindUserVehiclesDocument,
}); return { - eventUUID: uuid, - metas: { - title: event?.attributes?.name || '', - url: `https://${host}${context.resolvedUrl}`, + props: { + eventUUID: uuid, + metas: { + title: event?.attributes?.name || '', + url: `https://${host}${context.resolvedUrl}`, + }, }, }; }
M
frontend/pages/e/[uuid]/waitingList.tsx
→
frontend/pages/e/[uuid]/waitingList.tsx
@@ -59,19 +59,28 @@ export const getServerSideProps = pageUtils.getServerSideProps(
async (context, apolloClient) => { const {uuid} = context.query; const {host = ''} = context.req.headers; + let event = null; // Fetch event - const {data} = await apolloClient.query({ - query: EventByUuidDocument, - variables: {uuid}, - }); - const event = data?.eventByUUID?.data; + try { + const {data} = await apolloClient.query({ + query: EventByUuidDocument, + variables: {uuid}, + }); + event = data?.eventByUUID?.data; + } catch (error) { + return { + notFound: true, + }; + } return { - eventUUID: uuid, - metas: { - title: event?.attributes?.name || '', - url: `https://${host}${context.resolvedUrl}`, + props: { + eventUUID: uuid, + metas: { + title: event?.attributes?.name || '', + url: `https://${host}${context.resolvedUrl}`, + }, }, }; }