i18n icon indicating copy to clipboard operation
i18n copied to clipboard

Critical: Language routes are not properly created

Open tonqa opened this issue 2 years ago • 7 comments

Version

@nuxtjs/i18n: 7.2.3 nuxt: 2.15.8

Nuxt configuration

  • [x] Applies to a site deployed to a server with a Node backend

@nuxtjs/i18n configuration

  i18n: {
    locales: [
      {
        code: "en",
        name: "English",
      },
      {
        code: "de",
        name: "Deutsch",
      },
    ],
    strategy: "prefix_except_default",
    defaultLocale: "en",
    vueI18n: {
      fallbackLocale: "en",
      messages: {
        en: {
          welcome: "Welcome",
        },
        de: {
          welcome: "Willkommen",
        },
      },
    },
    detectBrowserLanguage: {
      useCookie: true,
      cookieKey: "lang",
    },
  },

Steps to reproduce

  • I imported the nuxt/i18n module to my running project and set it up like above.

  • For all my pages only the index page works with /de Screenshot 2022-08-06 at 13 43 57

  • All other pages run into an error (any route with /de/{route}):

[vue-router] Route with name 'reports' does not exist
  • I had a look to the nuxt/router.js and it generated a strange route, if I delete that route it seems to work Screenshot 2022-08-06 at 13 44 14

What is Expected?

All old and new routes should work when opening them manually in my browser (this actually has been my first test)

What is actually happening?

For all my pages, only the default language can be accessed (not any route with /de)

tonqa avatar Aug 06 '22 11:08 tonqa

Please prepare a repo that reproduces. I don't think this should be happening with default Nuxt settings at least.

rchl avatar Aug 06 '22 20:08 rchl

This is the pure setup with some basic libraries. Please feel free to clone. https://github.com/tonqa/nuxt-demo

tonqa avatar Aug 07 '22 10:08 tonqa

You are adding custom route with * path:

  router: {
    base: "/",
    extendRoutes(routes, resolve) {
      routes.push({
        name: "custom",
        path: "*",
        component: resolve(__dirname, "layouts/error.vue"),
      });
    },
  },

You can change the path to /*.

rchl avatar Aug 07 '22 14:08 rchl

Also, maybe it's just your example that is a simplification of the real issue but not sure what's the point of adding a "catch-all" route that shows the error as that should be the case by default.

rchl avatar Aug 07 '22 14:08 rchl

I see, I forgot about it. The reason is as follows: I have a dynamic page _post.vue, which has a validate function.

  validate({ params }) {
    // Must be a number
    return /^\d+$/.test(params.post);
  },

If the validation fails it should redirect to the 404 page. It's suggested here at the docs https://nuxtjs.org/docs/configuration-glossary/configuration-router/

It could also happen that there's a better way to do it. I will have a look. As a suggestion, why don't you ignore such custom catch-all routes in your logic?

tonqa avatar Aug 07 '22 14:08 tonqa

why don't you ignore such custom catch-all routes in your logic?

That's not the main issue. The main issue in my eye is that the sortRoutes function from Nuxt doesn't work correctly in this case. The proper sorting should result in this route being last.

I've created Nuxt issue at https://github.com/nuxt/nuxt.js/issues/10630

rchl avatar Aug 07 '22 14:08 rchl

Thanks. you helped me a lot.

tonqa avatar Aug 08 '22 17:08 tonqa