你可以做这样的事情。检查一个不存在的页面是否应该用 404 或 410 响应的逻辑可以根据您的需要进行修改。
// _error.js
import Error from 'next/error';
const CustomError = ({ statusCode }) => (
<Error
statusCode={statusCode}
title={
statusCode === 410
? 'The requested resource is no longer available'
: undefined
}
/>
);
const gonePages = ['gone', 'moved', 'foo/bar'];
const compareURLs = (n, h) => {
const nParts = n.split('/').filter(Boolean);
const hParts = h.split('/').filter(Boolean);
return (
nParts.length <= hParts.length &&
nParts.every((part, i) => part === hParts[i])
);
};
const getServerSideProps = async ({ req, res }) => ({
props: {
statusCode:
res.statusCode === 404 &&
gonePages.some((slug) => compareURLs(slug, req.url))
? 410
: res.statusCode,
},
});
export default CustomError;
export { getServerSideProps };
检查沙箱:codesandbox.io/s/zealous-driscoll-ru62j