svobik7 / next-roots

Next.js utility to generate i18n routes in the new APP directory.
https://next-roots-svobik7.vercel.app/
MIT License
189 stars 12 forks source link

getHref in route with multiple dynamic params #207

Closed zto-sbenning closed 3 months ago

zto-sbenning commented 7 months ago

Context:

roots.config.js content

const path = require('path')

module.exports = {
    originDir: path.resolve(__dirname, 'src/roots'),
    localizedDir: path.resolve(__dirname, 'src/app/(routes)'),
    locales: ['fr', 'en'],
    defaultLocale: 'fr',
    prefixDefaultLocale: false, // serves "fr" locale on / instead of /fr
}

Folder structure

- src
  - roots
      . page.tsx 
      - hub
          . i18n.ts 
          . page.tsx
          - [country]
              . page.tsx
              - [department]
                  . page.tsx
                  - [city]
                      . page.tsx
                      - [district]
                           . page.tsx

src/roots/hub/i18n.ts content

export async function generateRouteNames() {
    return [
        { locale: 'fr', path: "chercher-un-salon" },
        { locale: 'en', path: "find-an-hairdresser" },
    ]
}

Problem:

In src/roots/hub/page.tsx if I do:

const href = new Router(schema).getHref("/hub/[country]", { locale: "fr", country: "toto" });

The href returned is "/chercher-un-salon/toto" witch is all right.

But in src/roots/hub/[country]/page.tsx if I do:

const href = new Router(schema).getHref("/hub/[country]/[department]", { locale: "fr", country: "toto", department: "tata" });

The returned href is "/" instead of "/chercher-un-salon/toto/tata" .

The same problem arrises for the route names "/hub/[coutrny]/[department]/[city]" and "/hub/[coutrny]/[department]/[city]/[district]".

I do not use any generateStaticParams as I don't want all the possible pages to be generated at build time (can this be the source of the issue ?). I use typescript and the two getHref statement are valids in types (I ran the npx next-roots beforehand).

Do you know why the href is not being computed correctly when there is more than 1 dynamic parameter ? Do you need a reproduction repo to investigate, or do you spot something I'm doing wrong ?

Thank you in advance for any tips you can have !

svobik7 commented 7 months ago

Hi @zto-sbenning,

I dont think that generateStaticParams has any effect on this neither multiple dynamic parameters as I am doing the same forblogs/[author]/[article] route in https://github.com/svobik7/next-roots/blob/master/examples/basic/src/routes/blogs/%5Bauthor%5D/%5Barticle%5D/page.tsx

☝️The only difference I can see is that you are using new Router(schema) multiple times while I have that instantiated only once (see https://github.com/svobik7/next-roots/blob/master/examples/basic/src/server/router/index.ts#L20). Not sure if that can be the issue but it is the difference I can spot right now.

Please try to adjust the code accordingly and if it does not help please create some minimal repro repo.

svobik7 commented 6 months ago

Hi @zto-sbenning have you solved your issue? Could you please leave a short feedback for others if so? Thank you.

zto-sbenning commented 3 months ago

@svobik7 Sorry for the delay. It was indeed an issue with next caching the previous schema.

TY a lot!