tatethurston / nextjs-routes

Type safe routing for Next.js
MIT License
571 stars 23 forks source link

[Question] 'Rewrites' support #19

Closed erickriva closed 2 years ago

erickriva commented 2 years ago

Hello ๐Ÿ™‚! This package supports 'rewrites'? If don't, there are reasons (or issues) to support it?

tatethurston commented 2 years ago

Rewrites in next.config.js https://nextjs.org/docs/api-reference/next.config.js/rewrites?

erickriva commented 2 years ago

yeah, i'd like to generate types using aliases instead of original routes... is it supported?

tatethurston commented 2 years ago

This isnโ€™t supported in the current version. Could you share an example with me and the outcome you would like?

erickriva commented 2 years ago

in my next.config.js, i have something like this:

module.exports = {
  async rewrites() {
    return {
      beforeFiles: [
        {
          source: '/loja',
          destination: '/store',
        },
      ]
    }
  }
}

in this case, the folder inside pages directory is called store for a better organization purpose, so there is a /store route, right? but i'm doing a map on next.config.js to /loja (store in my language, portuguese)

i want the types to have /loja instead of /store... is this doable?

tatethurston commented 2 years ago

I see, so you would want the generated route type for something like

/store/[id]/ to instead be /loja/[id]?

And this is because you want your public facing URL paths to be in english, but your source code to be in Portuguese?

erickriva commented 2 years ago

And this is because you want your public facing URL paths to be in english, but your source code to be in Portuguese?

I need the folder names to be in English, but routes must be in Portuguese. The Next part of it was already made, setting the map of rewrites on next.config.js. But the typing keeps using folder name as route path. Would be possible to look to next config file and map to these new routes?

tatethurston commented 2 years ago

But the typing keeps using folder name as route path. Would be possible to look to next config file and map to these new routes?

I'm concerned the scope for parsing the next.config.js and updating the routes would get large quite quickly. rewrites accept arbitrary regex patterns and also path-to-regexp strings. I also don't think this is the correct default behavior, one of the opinions that Nextjs' routing layer takes is that the filesystem should describe your routing layer, so this seems to be working against the framework.

There is an interesting question around internationalization here though, and it seems like Nextjs still does not have a good answer.

Could you help me understand the english naming constraint more? It seems like you could move the store folder to something like src/en-pages/store and then inside pages/loja or src/pages/loja you would have:

export * from './src/en-pages/store';

I'll keep this open to see if others have a similar need, but in the meantime you could write a script to replace store with loja in the generated nextjs-routes.d.ts.

erickriva commented 2 years ago

Oh, I understand your concerns, it makes much sense... There's an update being prepared for router, isn't? Maybe it will be solved (or at least a part of) in that update. Would be great if this package was 'absorbed' by Next, to be a default type generator for router. Thank you for your answers! It was really helpful ๐Ÿ˜Š

tatethurston commented 2 years ago

No problem! Iโ€™ll look into next pathname intl more, it would be great to have a canonical system for the next ecosystem