简体   繁体   English

部署 Nextjs 应用程序后,vercel 上的 ERR_TOO_MANY_REDIRECTS

[英]ERR_TOO_MANY_REDIRECTS on vercel after deploying Nextjs App

I deployed my nextjs app to Vercel and it was a success.我将我的nextjs应用程序部署到 Vercel,它是成功的。

I can see a preview of the website and even the log saying it works well.我可以看到网站的预览,甚至可以看到运行良好的日志。

But i try to access the website via the default Vercel domain:但我尝试通过默认的 Vercel 域访问该网站:

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

I get an ERR_TOO_MANY_REDIRECTS .我得到一个ERR_TOO_MANY_REDIRECTS

I do not have this problem locally.我在本地没有这个问题。

I tried:我试过了:

  • Disabling a language redirect that I use (/en for english folks, and / for french people).禁用我使用的语言重定向(/en 用于英国人,/ 用于法国人)。
  • Disabling the language detector of i18next .禁用i18next的语言检测器。

But none of these solutions changed anything.但是这些解决方案都没有改变任何东西。 Any ideas?有任何想法吗?

i18n.js file 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;

My change Language function:我的更改语言 function:

 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); } }); };

What happend at your server is following:您的服务器上发生的情况如下:

You enter https://tly-nextjs.vercel.app/ and it is redirected to /en with HTTP-Status-Code 307 (Temporary Redirect).您输入https://tly-nextjs.vercel.app/并使用 HTTP-Status-Code 307(临时重定向)重定向到 /en。

And /en redirect with 301 (Permanent Redirect) to /.并且 /en 使用 301(永久重定向)重定向到 /。

You can reproduce this by open the Browser-Dev-Tools and have a look at the Network Tab.您可以通过打开 Browser-Dev-Tools 并查看Network选项卡来重现这一点。

It might be, that you have some kind of url-rewriting activated at your server, which redirect everything to your domain-root.可能是您在服务器上激活了某种 url 重写,它将所有内容重定向到您的域根目录。

Is there a public repo available for this?是否有可用的公共回购? Here is how it worked for me.这是它对我的工作方式。

Try changing the order of the locales and the default locale (not sure this helps, but it changed something for me. Undocumented if that is the case!) So I put the default locale first (which is nl for me) in both the locales array and the domains array.尝试更改语言环境和默认语言环境的顺序(不确定这是否有帮助,但它为我改变了一些东西。如果是这种情况,则未记录!)所以我将默认语言环境(对我来说是 nl)放在两个locales中数组和domains数组。

Let me know if that helps!让我知道这是否有帮助!

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,
};

I changed all my getInitialProps to getServerSideProps我将所有 getInitialProps 更改为 getServerSideProps

and realised I was doing a redirect on response:并意识到我正在对响应进行重定向:

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

I just delete it.我只是删除它。 And now I don't have this endless redirect.现在我没有这种无休止的重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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