Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redirectOn: 'no prefix' does not redirect on the root URL #2132

Closed
phanect opened this issue Jun 3, 2023 · 7 comments · Fixed by #2164 or #2252
Closed

redirectOn: 'no prefix' does not redirect on the root URL #2132

phanect opened this issue Jun 3, 2023 · 7 comments · Fixed by #2164 or #2252
Assignees
Labels
browser language detection ❗ p4-important Priority 4: bugs that violate documented behavior, or significantly impact perf routing v8

Comments

@phanect
Copy link

phanect commented Jun 3, 2023

Environment

(Here's Stackblitz' environment information)

  • Operating System: Linux
  • Node Version: v16.14.2
  • Nuxt Version: 3.5.2
  • Nitro Version: 2.4.1
  • Package Manager: [email protected]
  • Builder: vite
  • User Config: ssr, components, modules, i18n
  • Runtime Modules: @nuxtjs/[email protected]
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/nuxt-starter-2yxmzd

Describe the bug

I added strategy: 'prefix' and redirectOn: 'no prefix' to nuxt.config.ts.

nuxt.config.ts

export default defineNuxtConfig({
  // ...
  i18n: {
    // ...
    strategy: 'prefix',
    // ...
    detectBrowserLanguage: {
      useCookie: true,
      cookieSecure: true,
      alwaysRedirect: true,
      fallbackLocale: 'en',
      redirectOn: 'no prefix',
    },
  },
});

If I understand correctly, with this config, if you open / (root URL), it should return 302 and redirect to /en or /ja. (Depending on your Accept-Language)
However, when you open / (root URL), it returns 404.

Additional context

No response

Logs

No response

@phanect phanect changed the title redirectOn: 'no prefix' does not redirect on the root URL [v8] redirectOn: 'no prefix' does not redirect on the root URL Jun 3, 2023
@phanect
Copy link
Author

phanect commented Jun 3, 2023

This issue might be related to #2131.

@kazupon kazupon added v8 ❗ p4-important Priority 4: bugs that violate documented behavior, or significantly impact perf pending triage routing and removed pending triage labels Jun 5, 2023 — with Volta.net
@phanect
Copy link
Author

phanect commented Jun 6, 2023

I guess the following code causes this issue:

i18n/src/runtime/utils.ts

Lines 294 to 299 in 8dc25cb

let redirectPath = ''
const isStaticGenerate = isSSG && process.server
if (!isI18nRouteDefined(route)) {
return redirectPath
}

isI18nRouteDefined(route) always returns false because route.matched[0]?.meta.nuxtI18n is always undefined on my end. (see the following isI18nRouteDefined() function:

i18n/src/runtime/utils.ts

Lines 514 to 517 in 8dc25cb

function isI18nRouteDefined(route: Route | RouteLocationNormalized | RouteLocationNormalizedLoaded): boolean {
const i18nLocales = route.matched[0]?.meta.nuxtI18n
return i18nLocales ? Object.keys(i18nLocales).length > 0 : false
}

Therefore, detectRedirect() returns redirectPath (=== empty string ('')) and the following redirection process is not executed.

I'm stuck here because I'm unsure how to make route.matched[0]?.meta.nuxtI18n a non-undefined value...

@phanect phanect changed the title [v8] redirectOn: 'no prefix' does not redirect on the root URL redirectOn: 'no prefix' does not redirect on the root URL Jun 6, 2023
Copy link
Collaborator

kazupon commented Jun 9, 2023

Thanks!

You can workaround with dedefaultLocale option.

BTW, docs recommend that it is specified default locale
https://v8.i18n.nuxtjs.org/guide/routing-strategies#configuration

@kazupon kazupon added workaround 🔨 p3-minor-bug Priority 3: a bug in an edge case that only affects very specific usage and removed ❗ p4-important Priority 4: bugs that violate documented behavior, or significantly impact perf labels Jun 9, 2023 — with Volta.net
@phanect
Copy link
Author

phanect commented Jun 9, 2023

@kazupon Thanks for the information.
However, setting defaultLocale does not solve the problem.

In the following repro, I set defaultLocale: "en".

https://stackblitz.com/edit/nuxt-starter-aj1bpj

However, / returns 200 and shows the English content, while it should return 302 to redirect to /en or /ja (Depending on your Accept-Language).

@kazupon kazupon added ❗ p4-important Priority 4: bugs that violate documented behavior, or significantly impact perf 🔨 p3-minor-bug Priority 3: a bug in an edge case that only affects very specific usage and removed workaround 🔨 p3-minor-bug Priority 3: a bug in an edge case that only affects very specific usage labels Jun 9, 2023 — with Volta.net
@urbgimtam
Copy link

@phanect Suffering the same issue.

Copy link
Collaborator

kazupon commented Jun 13, 2023

I seem that this issue is degraded by #1902
I'll try to revert that PR.

@phanect
Copy link
Author

phanect commented Jul 11, 2023

@kazupon Thanks so much for working on this! I confirmed this issue is partially fixed in v8.0.0-beta.13.

Now if you open https://example.com/, it automatically redirects to https://example.com/en.
However, it does not redirect to https://example.com/ja when you open the root page with Accept-Language: ja. It always redirects to https://example.com/en.

Here's the repro.
https://github.com/phanect/repro-nuxt-i18n-redirection

Note: This sample code is available on Stackblitz too, but Stackblitz seems to have a bug as of now and it didn't work as expected. Please clone the repro and run it on your local machine.

Here's step to reproduce:

  1. Run npm run dev at the project root of the repro.
  2. Open your browser settings to set your primary language "Japanese" so that the Accept-Language header would be ja. This issue happens when Accept-Language is something like ja,en;q=0.9,en-US;q=0.8 too.
  3. Open any page under http://localhost:3000/, open the browser developer tools (Chrome: Application panel, Firefox: Storage panel), and remove i18n_redirected cookie.
  4. Open http://localhost:3000/ (root page)

Expected: The browser redirects you to http://localhost:3000/ja
Actual: The browser redirects you to http://localhost:3000/en

Let me know if you prefer to close this issue and open another issue for Accept-Language bug.

@kazupon kazupon self-assigned this Jul 14, 2023
BobbieGoede added a commit to BobbieGoede/i18n that referenced this issue Mar 21, 2024
BobbieGoede added a commit to BobbieGoede/i18n that referenced this issue Mar 21, 2024
DarthGigi pushed a commit to DarthGigi/i18n that referenced this issue Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
browser language detection ❗ p4-important Priority 4: bugs that violate documented behavior, or significantly impact perf routing v8
Projects
None yet
3 participants