victorgarciaesgi / nuxt-typed-router

🚦Provide autocompletion and typecheck to Nuxt router
https://nuxt-typed-router.vercel.app
MIT License
361 stars 12 forks source link

LocaleRoutePathSchema doesn't include some routes from RoutePathSchema #119

Closed UfukUstali closed 1 year ago

UfukUstali commented 1 year ago

Describe the bug Some routes that are in the RoutePathSchema are not included in the LocaleRoutePathSchema

export type RoutePathSchema = 
    "/"|"/de/catagories/:slug"|"/de/catagories"|"/de/"|"/de/me/"|"/de/search"|"/de/signin"|"/de/verify-request"|"/en/catagories/:slug"|"/en/catagories"|"/en/"|"/en/me/"|"/en/search"|"/en/signin"|"/en/verify-request"|"/fr/catagories/:slug"|"/fr/catagories"|"/fr/"|"/fr/me/"|"/fr/search"|"/fr/signin"|"/fr/verify-request"|"/me/"|"/signin"|"/tr/"|"/tr/catagories/:slug"|"/tr/catagories"|"/tr/me/"|"/tr/search"|"/tr/signin"|"/tr/verify-request"|"/verify-request"
  ;

  export type LocaleRoutePathSchema = 
    "/"|"/me/"|"/signin"|"/verify-request"

As can be seen in the above example the "/catagories" and "/catagories/:slug" are not included in the LocaleRoutePathSchema. It is only the type that is broken when I pass "/catagories" to somewhere where the LocalRoutePathSchema is the type it functions without an issue.

On a completely unrelated matter could you guys take a look at my other issue on the nuxt i18n repo. It's about the newly introduced NuxtLinkLocale component.

Expected behavior LocaleRoutePathSchema should be identical to RoutePathSchema other than the fact that it should not include all of the locale variations.

Environnement infos

Your pages folder structure

pages β”œβ”€β”€ index β”‚ β”œβ”€β”€ catagories β”‚ β”‚ β”œβ”€β”€ [slug].vue β”‚ β”‚ └── index.vue β”‚ β”œβ”€β”€ index.vue β”‚ └── search.vue β”œβ”€β”€ index.vue β”œβ”€β”€ me β”‚ └── index.vue β”œβ”€β”€ me.vue β”œβ”€β”€ signin.vue └── verify-request.vue

Your nuxt.config.ts

i18n: {
  locales: [
    { code: "en", iso: "en-US", name: "English" },
    { code: "fr", iso: "fr-FR", name: "Français" },
    { code: "de", iso: "de-DE", name: "Deutsch" },
    { code: "tr", iso: "tr-TR", name: "Türkçe" },
  ],
  defaultLocale: "en",
  strategy: "prefix",
},
victorgarciaesgi commented 1 year ago

Hi! Thanks for the report i'll look into this

UfukUstali commented 1 year ago

I did a little more digging and the situation seems to be different then what i described above.

export type RoutePathSchema = 
    "/"|"/de/catagories/:slug"|"/de/catagories"|"/de/"|"/de/me/"|"/de/search"|"/de/signin"|"/de/verify-request"|"/en/catagories/:slug"|"/en/catagories"|"/en/"|"/en/me/"|"/en/search"|"/en/signin"|"/en/verify-request"|"/fr/catagories/:slug"|"/fr/catagories"|"/fr/"|"/fr/me/"|"/fr/search"|"/fr/signin"|"/fr/verify-request"|"/me/"|"/signin"|"/tr/"|"/tr/catagories/:slug"|"/tr/catagories"|"/tr/me/"|"/tr/search"|"/tr/signin"|"/tr/verify-request"|"/verify-request"
  ;

The RoutePathSchema is also missing the "/catagories" and "/catagories/:slug" fields. I was able to track it down until walkRoutes.ts file:

 if (parent?.path !== "/") {
  output.routesPaths.push({
    name: route.name,
    path: newPath,
    isLocale: isLocaleRoute,
  });
  }

This if statement was preventing the generation of the routes directly under index "/" thereby all of the following were not generated ["/catagories/:slug", "/catagories", "/search"]. The locale variations were not effected because their parent is technically "/en" or "/de" and so on. But when removed it causes the index route to be generated twice simply because i have a child route index under the root index route. e.g. ["/", "/", "/catagories/:slug", "/catagories", "/me/", "/search", "/signin", "/verify-request"]

pages β”œβ”€β”€ index <- from here β”‚ β”œβ”€β”€ catagories β”‚ β”‚ β”œβ”€β”€ [slug].vue β”‚ β”‚ └── index.vue β”‚ β”œβ”€β”€ index.vue <- the second index route was only generated after the modification β”‚ └── search.vue <- untill here was not generated unless it was locale route β”œβ”€β”€ index.vue < first index route was generated originally β”œβ”€β”€ me β”‚ └── index.vue β”œβ”€β”€ me.vue β”œβ”€β”€ signin.vue └── verify-request.vue

UfukUstali commented 1 year ago

This was the final solution i ended up that i think doesn't cause any issues

if (parent?.path !== "/" || newPath !== parent?.path) {
    output.routesPaths.push({
      name: route.name,
      path: newPath,
      isLocale: isLocaleRoute,
    });
  }
victorgarciaesgi commented 1 year ago

Do you want to make a PR or do you want me to udpate it? :) Thanks for the finding!

UfukUstali commented 1 year ago

Just opened the PR