redwood: NotFoundPage incorrectly gives 200 status error instead of 404
Because Redwood is a single-page app, the built-in 404-page web/src/pages/NotFoundPage/NotFoundPage.js is handled by WebPack as a “normal” page, i.e. it’s optimized and becomes a part of the dist/static/js/…chunk.js files during build. This can/could be problematic because it results in a 200 http status code for the 404 page and it also requires JS.
This seems like a good opportunity to take a step forward with Redwood pre-rendering! The goal would be to build NotFoundPage as a separate, static asset. Likely there are other solutions that would work (possible that WebPack already has a config option). But if there’s a way to learn and step toward Redwood having pre-render capability, I suggest that be the preferred option.
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 2
- Comments: 22 (20 by maintainers)
Same for GraphQL, I think the web don’t care about HTTP code anymore 😅
Per Core Team Discussion:
But if it redirects to a page that exists (the NotFoundPage) then by definition that’s a 200! The page has to either really not exist (and the web server returns a 404), of if you have access to the config of the web server itself you can tell it to return a 404 even though that page exists on disk and can be served.
I don’t think we can get around this in Jamstack land because everything is served from a CDN and the provider is probably not going to let you override response codes at the granular a level. But I could be wrong!
I don’t like that solution. It’s a documented bad way of doing it. See below.
- _https://thegray.company/blog/how-to-make-a-404-page-for-seo-usability#what-are-common-404-setup-mistakes_
I’m hoping we can come up with a better solution when we move to Vite+SSR. @dac09 What do you think?
Right, and that’s a web app acting in the correct manner: that URL doesn’t exist and so the web server returns 404 with a friendly UI. Our issue arises because of React, where ALL URLs are assumed to exist as far as the web server is concerned. It’s only React that knows, via the router, if that URL is correct. But the 200 has already come down from the server. It’s too late to return a 404.
So we were taking about having React redirect you to a URL that React doesn’t own, and that can always return a 404, but I don’t think that’s “correct” from an HTTP standpoint. But we haven’t got any updates in a while so I’m not sure where this stands…
@thedavidprice linked a page about 404s earlier in this thread. That page links to another page where we can read this:
We’re currently doing no3 in that list. Redirecting to a function that returns the 404 would be what they have at the top of their “don’t do this” list.
https://thegray.company/blog/how-to-make-a-404-page-for-seo-usability#what-are-common-404-setup-mistakes
AFAICT the only proper way to handle this is to mirror the router config on the server side and use that to determine if the page exists or not.
One interesting way to do that is to remove this config (for Netlify, other providers have something similar)
And then, when building the RW app, you look at the router and generate a bunch of index.html files in directories matching the routes and put that on the web server.
One downside with that approach is that you wouldn’t get a nice customized 404 page.
Not an optimal solution, but I proved this behavior would work:
redirect to the function
This of course doesn’t help when you customize since need to add the redirect logic etc – and many other DX considerations make this less than optimal.
But, the idea of a redirect (from Router) to a serverless function can set an actual 404 status
Exactly. I have only had some success with Netlify redirects and even then not 100% on the magic.
But really do need it for SEO and such
Note Remix touts proper 404s as a feature: https://remix.run/docs/en/v1/guides/not-found#how-to-send-a-404