- Published on
Early return for unhappy path
- Authors
- Name
- Nico Prananta
- Follow me: @2co_p
A PR was made, something like this:
// next.js getServerSideProps
export const getServerSideProps = async (ctx) => {
const { valid, event } = await isValidEvent({
organizationSlug,
projectSlug,
probeSlug,
eventSlug,
userID: ctx.user?.id,
})
const currentEvent = {
id: event?.id,
locationId: event?.locationId,
monika: event?.monikaId,
alert: event?.alertId,
response: parseEventResponse(event?.response),
createdAt: event?.createdAt.toString(),
recoveredAt: event?.recoveredAt?.toString() || '',
location: event?.location,
}
return {
props: {
currentEvent,
},
}
}
The problem with that PR is that it's reading the optional event
too many times. Furthermore, in this case since it's Next.js's getServerSideProps, it's better to check the existence of event
and return notFound when it's null
or undefined
since the page component expects a valid event
data.
Did you like this post?
I'm looking for a job as full stack developer. If you're interested, you can read more about me here.