fridays / next-routes

Universal dynamic routes for Next.js
MIT License
2.47k stars 230 forks source link

Default path prefix (BaseRoot) #117

Closed ivomarino closed 6 years ago

ivomarino commented 6 years ago

hi there, I'm using next-routes in a project. I was wondering if I can set a default root path in production sites like https://project.foo.com/branch/master/ -- In this case I either need relative links or prepend /branch/master everywhere, is this somehow possible? thanks for suggestions.

fridays commented 6 years ago

Hi @ivomarino you could do something like this:

const base = path => `/branch/master${path}`

routes.add('home', base('/'))
routes.add('post', base('/post/:id'))
<Link route='home'> ...
<Link route='post' params={{id: 1}}> ...
ivomarino commented 6 years ago

Great, thanks for feedback, I‘ll test that asap;)

nwalters512 commented 6 years ago

@fridays Let me know if I should open a new issue instead, but I have a similar problem. I have an app that's proxied behind Apache, so for instance, the page mypage is served at mydomain.com/myapp/mypage, but the route that's received by Express is /mypage. So, I want to render links using that custom base url /myapp, but I want Express to be processing routes as /, if that makes sense. Would this be an easy thing to support? I'd be happy to submit a PR with this functionality if you'd want this in the main project, otherwise I'll just fork this code into my project.

fridays commented 6 years ago

I never worked with that combination, but maybe you could rewrite req.url in a middleware and prefix the base path? Express usually does it the other way around when mounting sub-apps, where the mount path is removed from req.url and the original URL is kept in req.originalUrl

cyrstron commented 6 years ago

Hi,

I'm trying to add localization prefix to url string for my app. However, I still need app pages to be available without prefix for executing redirect to url with localization prefix if such page exist.

At the moment best solution I see is given below:

routes.add('/:lang(en|de)/', 'index');
routes.add('/:lang(en|de)/about-us',` 'about-us');

I that case page is available for both situations: with and without (for further redirect) prefix and also I have 404-error when there is no matches with existing pages.

The only problem here is that I have to write each page I create to routes.js. Is there any way to add prefix for whole pages folder without writing every single page?

alololox commented 4 years ago

@nwalters512 did you ever implement a solution for this?