nuxt-modules / i18n

I18n module for Nuxt
https://i18n.nuxtjs.org
MIT License
1.7k stars 473 forks source link

Homepage not redirecting to prefixed route when implementing "Wait for page transition" with ssr: false, and strategy: prefix #2820

Open adesombergh opened 6 months ago

adesombergh commented 6 months ago

Environment

Reproduction

Stackblitz reproduction

To reproduce visit homepage without any prefix. You should get a 404 Page not found: /.

Project configuration:

# nuxt.config.js
export default defineNuxtConfig({
  modules: ['@nuxtjs/i18n'],
  ssr: false,
  i18n: {
    ...,
    strategy: 'prefix',
    skipSettingLocaleOnNavigate: true,
  },
});
<template>
  <div>
    <LangSwitcher />
    <NuxtPage
      :transition="{
        name: 'page',
        mode: 'out-in',
        onBeforeEnter,
      }"
    />
  </div>
</template>

<script lang="ts" setup>
const { finalizePendingLocaleChange } = useI18n();

const onBeforeEnter = async () => {
  await finalizePendingLocaleChange();
};
</script>

<style>
.page-enter-active,
.page-leave-active {
  transition: all 0.4s;
}
.page-enter-from,
.page-leave-to {
  opacity: 0;
  filter: blur(1rem);
}
</style>

Describe the bug

When implementing the "Wait for page transition" (so that messages are updated after page transition and not before), in a project with ssr: false and strategy: "prefix", the un-prefixed homepage isn't redirecting to the prefixed route (/en, etc..), leading to a 404 Page not found: / error.

Additional context

No response

Logs

No response

szulcus commented 6 months ago

I have the same problem

Tienisto commented 4 months ago

I believe there is a bug in https://github.com/nuxt-modules/i18n/blob/b3114556a92f5f7622b37d7b57e681e1f7e3660d/src/runtime/utils.ts#L383

It needs to also check if the next route has i18n enabled or not. Currently, it seem to always set the locale based on the next route regardless if the next route has i18n or not