-
Notifications
You must be signed in to change notification settings - Fork 3
Handle 404 errors from API during server render #1678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
458f192 to
efc100e
Compare
frontends/main/src/app/styled.tsx
Outdated
|
|
||
| export const PageWrapper = styled.div({ | ||
| height: "calc(100vh - 80px)", | ||
| height: "100vh", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| return ( | ||
| <Container maxWidth="sm"> | ||
| <MuiCard sx={{ marginTop: "1rem" }}> | ||
| <MuiCard sx={{ marginTop: "4rem" }}> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
frontends/main/src/app/styled.tsx
Outdated
|
|
||
| export const PageWrapper = styled.div({ | ||
| height: "calc(100vh - 80px)", | ||
| height: "100vh", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gumaerc
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried setting NODE_ENV=production to test throwing an error and seeing the global error page, but I kept getting this error on every page with NODE_ENV set to anything but development:
I don't think that error has anything to do with the changes here, but nonetheless I wasn't able to test the error handling component of this. It does seem to be set up correctly in global-error.tsx, but I did see this in the documentation (https://nextjs.org/docs/app/building-your-application/routing/error-handling#handling-errors-in-root-layouts):
Even if a global-error.js is defined, it is still recommended to define a root error.js whose fallback component will be rendered within the root layout, which includes globally shared UI and branding.
I'm not worried about this, since it seems your strategy of creating ErrorPageTemplate includes the header and it looks okay in your screenshots, but I thought it worth mentioning.




What are the relevant tickets?
Fixes: https://github.com/mitodl/hq/issues/5734
Description (What does it do?)
Renders the not found page and returns a 404 status for requests for resources that are not found during server render.
How can this be tested?
Request a page with a drawer open for a non existing resource. The not found page html should be returned with 404 status, e.g. http://open.odl.local:8062/?resource=noexist
Note that the global error page is not active in dev mode. To test we need to throw and error in any server component and then
yarn build; yarn start;. The error message should be displayed (in place of "Something went wrong" in the image above).Additional Context
Next.js provides a file convention for catching errors from root layout (and generateMetadata()), however they are stripped to just
{ message, digest }, so we are not able to detect 404s to show the not found page. Instead we need to handle errors in place, in this case by try..catching around our API calls and callingnext/navigationsnotFound() to render the ./app/not-found.tsx page.