i18n icon indicating copy to clipboard operation
i18n copied to clipboard

i18n infinite redirect loop on static build with cookie browser detection enabled

Open DaniGTA opened this issue 2 years ago • 2 comments

Version

@nuxtjs/i18n: 7.2.0 nuxt: v2.15.8

Nuxt configuration

Please change to [x] if relevant for this issue:

  • [x ] Applies to a site deployed to a static server (site generated with nuxt generate)
  • [ ] Applies to a site deployed to a server with a Node backend

@nuxtjs/i18n configuration

  i18n: {
    strategy: "prefix_and_default",
    defaultLocale: "en",
    locales: [
      {
        code: "de",
        iso: "de-DE",
        file: "de.json",
      },
      {
        code: "en",
        iso: "en-US",
        file: "en.json",
      },
    ],
    detectBrowserLanguage: {
      useCookie: true,
      cookieKey: "i18n_redirected",
      redirectOn: "root"
    },

    langDir: "./locales/",
    vueI18nLoader: true,
  }

-->

Reproduction Link

(https://codesandbox.io/s/nuxt-nuxtjs-i18n-base-template-forked-6w90eg)

Steps to reproduce

Use Nuxt static build. Generate source with nuxt generated Access the sources via nuxt start Reload a default lang page (test.com/about)

Every first view of the site works fine or to be exact every call without local cookie from i18n nuxt.

/about or /test would work at first but when the user have a i18n cookie and is refreshing the default page the user get stuck in a infinite redirect from root to root

this bug doesnt exist on the root of the website test.com/ only on other pages like test.com/about

  1. Generate and start sources with nuxt
  2. Change language to anything
  3. Go to about page
  4. try access default lang about page direktly test.com/about not test.com/en/about

(This bug only exist on generated sources) https://codesandbox.io/s/nuxt-nuxtjs-i18n-base-template-forked-6w90eg

What is Expected?

When no cookie is set, it should redirect to the client browser lang.

callingtest.com/about should redirect to test.com/de/about

When the browser has a i18n cookie:

calling test.com/about with cookie en should not redirect, because en is the default lang of the page. but calling test.com/about with cookie de should redirect to test.com/de/about

What is actually happening?

i18n uses the cookie en to go to default page but at the default page it trys to go to en. test.com/about -> test.com/about -> test.com/about ... infinite loop

DaniGTA avatar Mar 16 '22 17:03 DaniGTA

I'm not sure what could the module do about it but setting Nuxt's trailingSlash option to true should fix it.

The issue is that the server itself redirects to a path with a slash while the module insists to have no slash.

Whether the trailing slash is added on the server or not depends on the server and how the files are generated so there is no universal fix for that.

rchl avatar Mar 17 '22 07:03 rchl