Next.js '在预渲染页面“/”时发生错误:错误:缩小反应错误 #321;' 在生产构建期间

Rec*_*con 5 javascript reactjs next.js

我正在尝试在 firebase 函数上运行 Next.js,并且之前已经成功部署过。但是,在添加 webpack 插件 (svgr) 并进行进一步自定义后,我在构建时遇到了错误。它是在 Next.js 自动优化页面时成功编译后发生的。

我的函数文件夹和我的 dev 文件夹的依赖项完全相同,除了函数文件夹有两个附加项(用于 firebase)。

经过检查,它似乎认为我有多个 React 实例。在我的 /app 文件夹之外以开发模式运行应用程序工作正常,但是,在开发模式下在 /functions 文件夹之外运行应用程序仍然无法正常工作。

Error occurred prerendering page "/": Error: Minified React error #321; visit https://reactjs.org/docs/error-decoder.html?invariant=321 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.

Error occurred prerendering page "/404.html": Error: Minified React error #321; visit https://reactjs.org/docs/error-decoder.html?invariant=321 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.

下一个.config.js

module.exports = {
    distDir: "../functions/next",
    webpack(config) {
        config.module.rules.push({
            test: /\.svg$/,
            use: ['@svgr/webpack'],
        });
        return config;
    },
    externals: {
        react: {
            root: 'React',
            commonjs2: 'react',
            commonjs: 'react',
            amd: 'react'
        },
        'react-dom': {
            root: 'ReactDOM',
            commonjs2: 'react-dom',
            commonjs: 'react-dom',
            amd: 'react-dom'
        }
    },

};
Run Code Online (Sandbox Code Playgroud)

目录布局

app/package.json

  "dependencies": {
    "@material-ui/core": "^4.7.0",
    "@material-ui/icons": "^4.5.1",
    "@svgr/webpack": "^4.3.3",
    "clsx": "^1.0.4",
    "next": "^9.1.4",
    "prop-types": "^15.7.2",
    "react": "^16.12.0",
    "react-dom": "^16.12.0"
  },

functions/package.json

  "engines": {
    "node": "8"
  },
  "dependencies": {
    "firebase-admin": "^8.0.0",
    "firebase-functions": "^3.1.0",
    "@material-ui/core": "^4.7.0",
    "@material-ui/icons": "^4.5.1",
    "clsx": "^1.0.4",
    "next": "^9.1.4",
    "prop-types": "^15.7.2",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "@svgr/webpack": "^4.3.3"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.1.6"
  },
Run Code Online (Sandbox Code Playgroud)

小智 0

让我们保留 distDir 不变,用 to.next代替../functions/.next。在某种程度上,您可以在构建后立即使用cp .next ../functions/.next. dev当您调用next({ dev: true, ...otherOptions })或设置NODE_ENV=production为 run时,您还需要将值更改为 true firebase serve