-
Notifications
You must be signed in to change notification settings - Fork 26k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
decouple route path from filename #8241
Comments
(as a developer in Switzerland) we also had to solve this multiple times. You have multiple ways:
// getPathname.ts
// implementation could be simplified, but it gives you the idea
function getPathname (page: string, lang: string) {
if (page === '/about' && lang === 'de') return '/about'
if (page === '/about' && lang === 'fr') return '/a-propos-de';
if (page === '/about' && lang === 'de') return '/uber-uns'
throw new RangeError(`Unknown page ${page} for lang ${lang}`);
} // PageLink.tsx
import Link from 'next/link';
// ...
const PageLink: React.FC<LinkProps> = (props) => {
const { i18n: { language } } = useTranslation(); // if you use react-i18next / next-i18next;
const pathname = getPathname(props.href, language);
const { children, ...rest } = props;
return <Link {...rest} as={pathname}>{children}</Link>;
} then you'd just have to <PageLink href="/about">Go to the about page with the correct pathname</PageLink> Hope it helps. More generally, I second decoupling pages and "views", but I don't feel it's going to change soon. |
What I came up was something like this.
Now it should be possible to detect the locale in the index page and set lang accordingly when rendering links. You can probably use the custom link components @martpie mentioned above. |
For reference, I think the most lively discussion around this recently has been in the dynamic routing RFC: #7607. Specifically, these comments caught my attention:
The RFC description specifically mentions this:
I think that would resolve the filesystem-based routing limitations perfectly. |
Want to chime in by saying that downstream dependencies like Functionality which previously worked in NextJs v8 is now broken in v9. If the core team is happy with merging a fix for this, and can point me in the right direction, I can get a PR through. Thanks! |
In regard to #8444, I believe the solution is already there just needs to be repurposed/modified existing functionality.
This way I can store a mapping of URL to page in an external backend system and insert new URL as new items gets added. It has following issues:
I will also face same issue with locales code in near future. Maybe it's possible to take out locale info from url and put it as query-param like following.
If something can be configured in
Possible Solution: If Next.js provide some kind of hook or inheritance to extend or Can I provide my own implementation of So this way framework will be free to provide some default way to map urls and users can implement their own in any way instead of waiting for new solution to implement at framework level. I have implemented already something like that but in server.js. Would like to do it in Next config file for better integration and extending framework. Thanks |
A different RFC will be posted soon. |
I copied this code to
The drawback is that I did not find any way to update these routes dynamically after regular interval to fetch any updated path mapping from backend. I am looking into if JS generators based method can help to achieve that. |
Any light on this matter? Anyone found a workaround that does not involve changing the route system :) |
Handled by #9081 |
@martpie Thanks for the solution! |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Feature request
Is your feature request related to a problem? Please describe.
url structures are still visible on devices, so its a UI-element and often is bound to editorial decisions, yet in nextjs, url routes always reflect the filesystem of the project
Having the filename of a page be the route path segment might be ok, if you develop for an english target group, but leads to problems in other markets. E.g. in switzerland we often need to have an app in multiple languages and customers often wish that the url paths are also translated.
Also, there is a common practice, that source code is always in english. So this leads to the problem, that when you decide to have e.g. german url paths, you need to have german-named files in your app, which just feels wrong.
Nextjs should therefore provide a firstclass support to translate url path segments.
Describe the solution you'd like
There should be a way to provide aliases to pages in code. So a page would be delivered on all available aliases. It should be possible to use translation services to provide these aliases. They should work both on server and on client.
If you use a
<Link />
� to a page, you can provide some context, so that nextjs would use the right alias to make the route. E.g. you could provide the locale.I don't know nextjs good enough to suggest an api for it yet, but suggestions are very welcome!
Describe alternatives you've considered
Doing it with dynamic routes (next 9 feature), is nearly impossible, because every page and folder would basically look like
[slug].js
, so you would need to deliver the right page yourself, defeating the purpose of the whole file-system-routing.It's only possible with a custom server and some boilerplate, so that your
<Link />
behave correctly. But this is needs careful setup, so that you don't have duplicated route declarations and it is therefore error-prone.The text was updated successfully, but these errors were encountered: