简体   繁体   中英

Error occurred prerendering page "/404" : TypeError: res.writeHead is not a function - Next Js

a 404 error is when build time, I using next js

Terminal :

[ ===] Exporting (0/3) Error occurred prerendering page "/404". Read more: https://err.sh/next.js/prerender-error TypeError: res.writeHead is not a function at redirectWithoutCache (/node_modules/next-i18next/dist/commonjs/utils/redirect-without-cache.js:12:7) ...

Error occurred prerendering page "/404.html". Read more: https://err.sh/next.js/prerender-error TypeError: res.writeHead is not a function at redirectWithoutCache (/node_modules/next-i18next/dist/commonjs/utils/redirect-without-cache.js:12:7)

[= ] Exporting (2/3) Error occurred prerendering page "/". Read more: https://err.sh/next.js/prerender-error TypeError: res.writeHead is not a function at redirectWithoutCache (/node_modules/next-i18next/dist/commonjs/utils/redirect-without-cache.js:12:7) ...

Exporting (3/3) Error: Export encountered errors on following paths: / /404 /404.html at exportApp (/node_modules/next/dist/export/index.js:24:1103)

config files :

next.config.js

const { nextI18NextRewrites } = require('next-i18next/rewrites')

const localeSubpaths = {
  fa: 'fa',
  en: 'en'
}

const withSass = require("@zeit/next-sass");
const withLess = require("@zeit/next-less");
const withCSS = require("@zeit/next-css");

const isProd = process.env.NODE_ENV === "production";

// fix: prevents error when .less files are required by node
if (typeof require !== "undefined") {
  require.extensions[".less"] = (file) => {};
}

module.exports = withCSS({
  rewrites: async () => nextI18NextRewrites(localeSubpaths),
  publicRuntimeConfig: {
    localeSubpaths,
  },
  cssModules: true,
  cssLoaderOptions: {
    importLoaders: 1,
    localIdentName: "[local]___[hash:base64:5]",
    url: false
  },
  ...withLess(
    withSass({
      lessLoaderOptions: {
        javascriptEnabled: true,
      },
      webpack (config, options) {
        config.module.rules.push({
          test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
          use: {
            loader: 'url-loader',
            options: {
              limit: 100000
            }
          }
        })
        return config
      }
    }),
  ),
  exportTrailingSlash: true,
  exportPathMap: function() {
    return {
      '/': { page: '/' }
    };
  },
});

i18n.js

const NextI18Next = require('next-i18next').default
const { localeSubpaths } = require('next/config').default().publicRuntimeConfig
const path = require('path')

module.exports = new NextI18Next({
  defaultLanguage: 'fa',
  otherLanguages: ['en'],
  defaultNS: 'common',
  localeSubpaths: {
    fa: 'fa',
    en: 'en',
  },
  localePath: path.resolve('./public/static/locales'),
  localeStructure: '{{lng}}/{{ns}}',
})

I am supposed that you are running next export which next-i18next does not support.

https://github.com/isaachinman/next-i18next/issues/780

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM