Sebagai contoh, kami pernah menggunakangetServerSidePropsuntuk mengubah hala ke halaman 404 dalam komponen halaman seperti ini:
// 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], }, };
Dengan direktori Next.js13danapp, kami mempunyai komponen pelayan.13和app目录,我们有了服务器组件。getServerSidePropsBagaimana untuk mengubah hala ke halaman 404 apabila tidak lagi digunakan?
Mengikut dokumentasi, anda boleh menggunakan fail
notFound( )函数如下所示,它会渲染相应的not-found.js:// app/user/page.js import { notFound } from 'next/navigation'; export default async function Profile({ params }) { const res = await fetch(`/user/${params.id}`); if (!res.ok) { notFound(); } returnActual Data ; }// app/user/not-found.js export default function NotFound() { return404 Not Found
}Tanpa
app/user/not-found.js文件,则使用app/not-found.js。如果没有app/not-found.js, ia akan menggunakan nilai lalai yang diberikan oleh Next.js.