QwikDev / qwik

Instant-loading web apps, without effort
https://qwik.dev
MIT License
20.64k stars 1.28k forks source link

[šŸž] translated routes from rewriteRoutes get ignored for [...catchall] route in same folder #5665

Closed denisyilmaz closed 5 months ago

denisyilmaz commented 8 months ago

Which component is affected?

Qwik City (routing)

Describe the bug

I have the following routing folder structure:

The paths /en/projects//de/projects are correctly rendered by the projectsroute folder. Any other urls /en/some-pageor /de/example/some-other-sub-path get correctly rendered by the [...catchall] route folder.

For better i18n for the application i know wanted to use the new rewriteRoutes feature and added the following:

qwikCity({
      rewriteRoutes: [
        {
          prefix: '',
          paths: {
            'projects': 'projekte'
          },
        },
      ],
    })

Expected behavior: /de/projekte/some-projekt should be rendered via the projects route folder.

Actual behavior: Unfortunately this is not the case but i can see the catchall route is used instead. When deleting the catchall route from the folder structure the rewriteRoutes is working correctly though.

It seems that the catchall routes are matched before the routes defined via the rewriteRoutes config. Is this an expected behavior or is there a way to prioritise the rewriteRoutes?

Reproduction

https://stackblitz.com/edit/qwik-starter-jnup5o?file=vite.config.ts

Steps to reproduce

  1. open reproduction
  2. visit: https://qwikstarterjnup5o-s2um--5173--a2aabdd9.local-credentialless.webcontainer.io/de/projects
  3. visit: https://qwikstarterjnup5o-s2um--5173--a2aabdd9.local-credentialless.webcontainer.io/de/projects/someproject
  4. visit: https://qwikstarterjnup5o-s2um--5173--a2aabdd9.local-credentialless.webcontainer.io/de/somepage/test
  5. visit: https://qwikstarterjnup5o-s2um--5173--a2aabdd9.local-credentialless.webcontainer.io/de/projekte/someproject
    • it renders the catchall route
  6. delete or rename the [...uri] route folder
  7. visit: https://qwikstarterjnup5o-s2um--5173--a2aabdd9.local-credentialless.webcontainer.io/de/projekte/someproject
    • it renders the projects route

System Info

System:
    OS: macOS 14.2.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 2.74 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 16.15.0 - ~/.nvm/versions/node/v16.15.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.15.0/bin/yarn
    npm: 8.15.1 - ~/.nvm/versions/node/v16.15.0/bin/npm
  Browsers:
    Chrome: 120.0.6099.199
    Edge: 119.0.2151.46
    Safari: 17.2.1
  npmPackages:
    @builder.io/qwik: ^1.3.2 => 1.3.2 
    @builder.io/qwik-city: ^1.3.2 => 1.3.2 
    @builder.io/sdk-qwik: ^0.7.5 => 0.7.5 
    undici: 5.26.0 => 5.26.0 
    vite: 4.4.9 => 4.4.9

Additional Information

No response

wmertens commented 7 months ago

Indeed, that can be considered a bug. Would you be interested in contributing a PR? The rewrites should happen before the route matching.

denisyilmaz commented 7 months ago

Alright, thanks for the confirmation that this is not an expected behavior. I hope to find some time the next few days to dig into this!

fprl commented 5 months ago

Damn, I was going to write exactly this issue. Will take a look if I can create a PR. Any clue @wmertens

Thanks

wmertens commented 5 months ago

This is where routes are matched, you can look up the call chain from there. https://github.com/BuilderIO/qwik/blob/302b27eb7f6cd9c7e47c70cdfcf478c13c916faf/packages/qwik-city/middleware/request-handler/request-handler.ts#L20

fprl commented 5 months ago

This is where routes are matched, you can look up the call chain from there.

https://github.com/BuilderIO/qwik/blob/302b27eb7f6cd9c7e47c70cdfcf478c13c916faf/packages/qwik-city/middleware/request-handler/request-handler.ts#L20

Hi @wmertens , thanks for the tip. I finally realised the issue was at build time, as the rewritten routes were pushed at the end of the routes array, instead of the original route + 1 index.

Hope its all good and it can be merged soon!