Repository navigation
Redirect to 404 #10960
|
How do you redirect to the 404 page from within |
Replies: 14 comments 46 replies
|
Update: export async function getStaticProps(context) {
const res = await fetch(`https://.../data`)
const data = await res.json()
if (!data) {
return {
notFound: true,
}
}
return {
props: { data }, // will be passed to the page component as props
}
}I'm assuming in your case you're asking this because you're using the 404 is a state of the UI so you should render it based on the properties: |
|
@timneutkens I'm a little concerned with returning a 200 success code for my 404 pages, especially when google explicitly says that it's a bad practice. https://support.google.com/webmasters/answer/181708?hl=en Why can't we return a headers object from getStaticProps or have some other solution? Redirects would be helpful too. |
I don't think that's the best way to think about this, due to what @stevez86 has said. A 404 is a standardised indication by the server, that the requested resource is missing. |
|
I'm disappointed (and kind of amazed) that Next would take this route deliberately. The whole point of using a n SSR framework like Next JS is SEO, I can't think of many other reasons people would need SSR. So, if Next can't be SEO friendly then we may as well just build using create-react-app, leave the server out of it and get a faster app. Returning a 200 status code for a 404 error is just not right, and how would it be possible to implement a 301 solution for moved pages when using fallback? Seems like this just hasn't been thought through. |
|
@timneutkens, in your solution you are assuming that having no posts means a 404 page, and that could be correct in that specific case of a route like More broadly though, having an empty response may also mean that there are currently no items that correspond to that route and then the UI should inform the user about it. No 404 pages or even errors are applicable. I am building an app with small ads for multiple categories ( about 200 ) and cities ( could be north of 20, 000 ). As you can understand it is not realistic to pre-build all pages and there are a number of ways an user can simply type a completely wrong URL. So, I was thinking about using The question is how to handle the mistyped URLs. I was thinking about doing a sanity check on If the URL does not pass that basic sanity check it would mean it is plainly wrong and I would like to throw a 404 directly from within Can that be accomplished correctly, meaning with an actual 404 page with the correct HTTP code? |
|
The problem with the suggested solution is that it doesn't allow you to build the error page statically as the component is imported rather than the error page used. Example: building a site with a headless CMS that creates all pages on dynamic routes. These are fetched in With |
|
would like to know more about this but seems that #15672 is the answer for now |
|
My concern to this approach is that very time a page that genuinely doesn't exist is encountered, the fallback code in But that means that every time a user lands on a missing page, every time there's a typo in the url, or even through sheer malice, the SSG process will actually run. HTML and JSON files will actually get generated. For every missing page. This seems wasteful to me and I feel there's definitely a place for throwing or redirecting to a It also seems dangerous. One tiny script that makes repeated requests to random garbage urls on my site could result in mountains of junk html and json files on the server. |
|
why this code is not prerender in the page |
|
Shall we all get back to the actual problem? i.e. it is not normal for a server to give a 200 on an actual 404.. that the ui says 404 is irrelevant. |
|
Just leaving my solution here that throws a 404 page. It might be a bit specific, but hope it helps someone. I have a Next.js site (v9.5) that brings in data from a headless CMS. You can set the URL of a page in the CMS to be whatever you want. In my I use My |
|
Guys now in version 10, inside getStaticProps you can return "notFound: true" to return a 404 status and reedirect the user to the 404.js page or _error.js page. It works even with fallback: true set. https://nextjs.org/docs/basic-features/data-fetching#getstaticprops-static-generation |
|
we can use inside getStaticProps |
|
This discussion seems to have an obsolete answer. There's a better way of doing this after some changes based on discussion #14890. Example code form that discussion: export default function HomePage() {
return Hello World
}
export async function getStaticProps({params}) {
const item = await getData(params.id)
if(!item) {
return {
redirect: {
permanent: false,
destination: '/somewhere-else'
}
}
}
return {
props: {
item
}
}
} |

Update:
Since Next.js v10, you can return
notFound: truefromgetStaticPropswhich allows the page to return a 404 status and 404 Page. Learn more here: https://nextjs.org/docs/api-reference/data-fetching/get-static-props#notfoundI'm assuming in your case you're asking this because you're using the
fallback: truebehavior in getStaticPaths?404 is a state of the UI so you should render it based on the properties: