我试过四处寻找,但找不到有我的问题的人。当我运行时,next build
我收到一条错误消息,说我不能拥有 getInitalProps/getServerSideProps,但在我的 404.tsx 文件中没有使用 getInitalProps 或 getServerSideProps。
Error Message:
Error: `pages/404` can not have getInitialProps/getServerSideProps, https://err.sh/next.js/404-get-initial-props
at /Users/myuser/.nvm/versions/node/v15.5.0/lib/node_modules/next/dist/build/index.js:15:5606
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:93:5)
at async /Users/myuser/.nvm/versions/node/v15.5.0/lib/node_modules/next/dist/build/tracer.js:3:470
at async Promise.all (index 0)
at async /Users/myuser/.nvm/versions/node/v15.5.0/lib/node_modules/next/dist/build/index.js:15:2910
at async /Users/myuser/.nvm/versions/node/v15.5.0/lib/node_modules/next/dist/build/tracer.js:3:470
at async /Users/myuser/.nvm/versions/node/v15.5.0/lib/node_modules/next/dist/build/index.js:15:2147
at async /Users/myuser/.nvm/versions/node/v15.5.0/lib/node_modules/next/dist/build/tracer.js:3:470
但我的 404 文件如下所示:
import * as React from 'react';
import Head from 'next/head';
import Layout from '../components/Layout';
const Error = () => {
return (
<Layout>
<Head>
<title>TM</title>
</Head>
<h1>Error 404</h1>
<h2>Page not found</h2>
</Layout>
);
};
export default Error;
布局组件:我注释掉了默认道具,认为它可能是出于某种奇怪的原因造成的。
import * as React from 'react';
import Footer from './Footer';
import Head from 'next/head';
import Header from './Header';
import styles from '../styles/main.module.css';
type Props = {
showFooter?: boolean;
showHeader?: boolean;
title?: string;
};
const Layout: React.FC<Props> = ({
showFooter,
showHeader,
title,
children,
}) => {
showFooter = showFooter || true;
showHeader = showHeader || true;
title = title || 'title';
return (
<div>
<Head>
<title>{title}</title>
</Head>
{showHeader && <Header />}
<div className={styles.content}>{children}</div>
{showFooter && <Footer />}
</div>
);
};
// Layout.defaultProps = {
// showFooter: true,
// showHeader: true,
// title: 'title',
// };
export default Layout;
谢谢您的帮助!