For example, we once used getServerSideProps
to redirect to a 404 page in the page component like this:
// pages/index.js export async function getServerSideProps(context) { const placeId = context.params.placeId; const places = await getPlace(placeId); if (!places.length) { return { notFound: true, } } return { props: { places[0], }, };
With the Next.js 13
and app
directories, we have the server component. getServerSideProps
How to redirect to a 404 page when it is no longer in use?
According to the documentation, you can use the
notFound( )
function as shown below, which will render the correspondingnot-found.js
document:If there is no
app/user/not-found.js
file, useapp/not-found.js
. If there is noapp/not-found.js
, it will use the default value given by Next.js.