[pid].js
我的 Next.js 项目中有一个文件。我还想实现自定义 404 页面,但问题是:我将404.js
文件放在/pages
目录中。如果我删除我的[pid].js
文件,404 页面就可以正常工作。但是,如果我保留我的[pid].js
文件,第一个请求将进入 pids,并且由于 url 与 pids 中定义的任何页面都不匹配,我会收到错误。我应该从 pid 显式返回我的 404 组件吗?这是一个好的做法吗?
这是代码(现在不会重定向到 404 页面):
[pid].js
const Pid = ({ url, props }) => {
const getPages = () => {
let component = null;
switch (url) {
case 'checkout':
component = <CheckoutPage {...props} />;
break;
//other switch cases...
default:
//should I return my 404 component here?
component = <DefaultPage {...props} />;
}
return component;
};
return getPages(); …
Run Code Online (Sandbox Code Playgroud)