-
With Next.js 10 it is now possible to return On our website we have also some pages and posts which were outdated and had only a few or none inbound links. Our content team decided to remove them without replacement, which is perfect use case for HTTP status 410 Gone. It informs both visitors and crawlers that indeed, the URL is correct, but the content is not available anymore. Google (and possibly other search engines) honors the code and won't attempt to reindex the page and will remove it from the index sooner. My question is what would be the best way to return 410 for some paths on Next.js website? From my point of view it seems like export async function getServerSideProps({ res }) {
res.statusCode = 410
return { props: {} }
} And point other URLs to it through rewrites or reuse the logic on particular pages. Check out this codesandbox with pages This works. But I wonder whether there is some other option which would better leverage static-site generation, especially since we know about these pages during build time? Related: #16554 |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 12 replies
-
Yes, I have the same need for the same SEO reasons.
We need to add either a |
Beta Was this translation helpful? Give feedback.
-
Here is what i tried: import { GetServerSideProps, NextPage } from "next";
interface ErrorPageProps {
statusCode: number;
}
const CustomError: NextPage<ErrorPageProps> = ({ statusCode }) => {
return (
<Error
statusCode={statusCode}
title={
statusCode === 410
? "The requested resource is no longer available"
: undefined
}
/>
);
};
const redirects = [{ slug: "/test/", status: 410 }];
const getServerSideProps: GetServerSideProps = async ({ res, query }) => {
const redirectStatus = redirects.find(
(f) =>
f.from.split("/")[1].toLowerCase() ===
(query.slug as string).toLowerCase()
).status;
const statusCode =
res.statusCode === 404 && redirectStatus === 410 ? 410 : res.statusCode;
res.statusCode = statusCode;
return {
props: {
statusCode,
},
};
};
export default CustomError;
export { getServerSideProps }; |
Beta Was this translation helpful? Give feedback.
-
It is not enough to have only 404 error. I Want to show 410 error for the goods that were sold out but I cannot. |
Beta Was this translation helpful? Give feedback.
-
obviously it should be possible to do return {staticHttpCode: anything that is not redirect} I can see why redirect you need to handle before it gets to the static rendering but other codes that can be rendered as essentially static pages should all be allowed. probably there should also be some other parameters returned at that point than just the http code page. |
Beta Was this translation helpful? Give feedback.
-
Recently they proposed similar APIs for forbidden and unauthorized. I posted on the RFC for that asking if a more universal API would be possible. #73753 (comment) |
Beta Was this translation helpful? Give feedback.
-
We have the very same need here. I created a PR to add a
Documentation
TestsThe PR includes:
Example UsageApp Routerimport { gone } from 'next/navigation'
export default function PostPage({ params }) {
// Check if content has been deliberately removed
if (isDeleted(params.slug)) {
gone() // Returns 410 Gone status
}
// Continue rendering content...
} Pages Router export async function getServerSideProps({ params }) {
// Check if content has been deliberately removed
if (isDeleted(params.id)) {
return { gone: true } // Returns 410 Gone status
}
return { props: { /* ... */ } }
} |
Beta Was this translation helpful? Give feedback.
We have the very same need here.
I created a PR to add a
Gone()
option that works analogously toNotFound()
#78706
App Router Support:
gone()
function in thenext/navigation
modulegone.js
file convention for customizing the 410 error pagePages Router Support:
{ gone: true }
from data fetching functions/410
page support similar to/404
pageAPI Route Support:
Documentation