In my page template i have custom types such as 'sidebar' and 'team_member' that are included via the content relationship field.
if someone using the CMS goes and deletes a sidebar or team member without also removing the reference to it in the content relationship field, it will throw a big error that breaks the page. i can catch the error, but still have problems because getById returns undefined rather than null when nothing is found.
so i ended up with something like this, which works, but im wondering if there is a cleaner way to do it?
export async function getStaticProps({ params, previewData, locale }) {
const client = createClient({ previewData })
const page = await client.getByUID('page', params.uid)
const pageCategoryId = page.data.pagecategory?.id
const pageCategory = pageCategoryId
? await client.getByID(pageCategoryId).catch(() => {
return null
})
: null
const sidebarId = page.data.sidebar?.id
const sidebar = sidebarId
? await client.getByID(sidebarId).catch(() => {
return null
})
: null
const team = await client.getAllByType('team_member')
if (!page) {
return {
notFound: true,
}
}
return {
props: {
page,
sidebar,
pageCategory,
team,
...(await serverSideTranslations(locale)),
},
}
}