例如,我们曾经使用getServerSideProps
重定向到页面组件中的404页面,如下所示。有了新版本,我们就有了Server组件函数。在不使用getServerSideProps
的情况下,如何重定向到404页?
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],
},
};
发布于 2022-11-07 10:35:11
根据文档,您可以使用notFound()
函数作为示例,如下所示,它将呈现位于app/feed/not-found.js
中的not-found.js
文件。
import { notFound } from 'next/navigation';
export default async function Profile({ params }) {
const res = await fetch(`/user/${params.id}`);
if (!res.ok) {
notFound();
}
return <div>Actual Data</div>;
}
// app/feed/not-found.js
export default function NotFound() {
return <p>404 Not Found</p>
}
https://stackoverflow.com/questions/74345219
复制相似问题