ERR_TOO_MANY_REDIRECTS 部署 Nextjs 应用后在 Vercel 上

ERR_TOO_MANY_REDIRECTS on vercel after deploying Nextjs App

我将我的 nextjs 应用程序部署到 Vercel,它很成功。

我可以看到该网站的预览,甚至可以看到日志说它运行良好。

但我尝试通过默认的 Vercel 域访问该网站:

https://tly-nextjs.vercel.app/

我得到一个 ERR_TOO_MANY_REDIRECTS

我本地没有这个问题

我试过了:

但是 none 这些解决方案改变了一切。 有什么想法吗?

i18n.js 文件

import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import Cache from 'i18next-localstorage-cache';
import LanguageDetector from 'i18next-browser-languagedetector';



const fallbackLng = ['fr']; 
const availableLanguages = ['fr', 'en'];

const en = require('./locales/en/common.json');
const fr = require('./locales/fr/common.json');

const options = {
  order: ['querystring', 'navigator'],
  lookupQuerystring: 'lng'
}

const cacheOptions = {
  // turn on or off
  enabled: true,

  // prefix for stored languages
  prefix: 'i18next_res_',

  // expiration
  expirationTime: 365*24*60*60*1000,

  // language versions
  //versions: {}
}

i18n
  .use(Cache)
  .use(initReactI18next)
  .use(LanguageDetector)
  .init({
    cache: cacheOptions,
    fallbackLng: fallbackLng,
    debug: true,
    detection: options,
    supportedLngs: availableLanguages,
    nonExplicitSupportedLngs: true,
    resources: {
      en: {translation: en},
      fr: {translation: fr},
    },
    interpolation: {
      escapeValue: false,
    },
    react: {
      wait: true,
      useSuspense: true,
    },
  });


export default i18n;

我的更改语言功能:

const changeLanguageInHouse = (lang, bool) => {
    setLoading(true);

    i18next.changeLanguage(lang).then((t) => {
      setLanguage(lang);
      bake_cookie("langChoice", lang);
      setLoading(false);

      if (bool === true) {
        var newUrl2 = (lang === "fr" ? "" : "/en") + asPath;

        window.location.replace(newUrl2);
      }
    });
  };

您的服务器发生的情况如下:

您输入 https://tly-nextjs.vercel.app/ 并使用 HTTP-Status-Code 307(临时重定向)重定向到 /en。

并且 /en 使用 301(永久重定向)重定向到 /.

您可以通过打开 Browser-Dev-Tools 并查看 Network 选项卡来重现此内容。

可能是您在服务器上激活了某种 url 重写,将所有内容重定向到您的域根目录。

是否有 public 存储库可用于此?这是它对我有用的方法。

尝试更改语言环境和默认语言环境的顺序(不确定这是否有帮助,但它对我来说改变了一些东西。如果是这种情况则未记录!) 所以我在 locales 数组和 domains 数组中都将默认语言环境放在首位(对我来说是 nl)。

如果有帮助请告诉我!

module.exports = {
  i18n: {
    localeDetection: false,
    // These are all the locales you want to support in
    // your application
    locales: ['nl', 'en'],
    // This is the default locale you want to be used when visiting
    // a non-locale prefixed path e.g. `/hello`
    defaultLocale: 'nl',
    // This is a list of locale domains and the default locale they
    // should handle (these are only required when setting up domain routing)
    domains: [
      {
        domain: 'example.be',
        defaultLocale: 'nl',
      },
      {
        domain: 'example.com',
        defaultLocale: 'en',
      },
    ],
  },
  trailingSlash: true,
};

我将所有 getInitialProps 更改为 getServerSideProps

并意识到我正在对响应进行重定向:

res.writeHead(301, { Location: "/" })

我直接删了。 现在我没有这种无休止的重定向了。