-
Notifications
You must be signed in to change notification settings - Fork 27.6k
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
Proposition: parameters in URL #91
Comments
This idea is amazing. |
How would you pass the parameter into the component? getInitialProps? |
Thanks! I think the best way is to add it as an argument in So then you could have this for example with getInitialProps(ctx, { id }) {
// Do request using id
} |
How would this translate a path with nested params like so? Would it make more sense to use a structure like this?
The above would support routes for
|
Ah, I now see that I created a duplicate PR 😅 .
It would have a folder structure like this:
But I do find that your structure feels more natural since it doesn't add a magical |
It's important to note that certain OS's will barf on the colons in directory names. In OSX, you can create a directory like |
What about using a dot instead of a colon?
|
@nickdandakis @nmuth -1 for this solution since it's all |
@nodegin It could just as easily follow the existing convention of
I think this fits in pretty well with how |
The concept of a filesystem based router shows its limits when it comes to advanced routing with RegExps, conditional routes and alike. @CompuIves, I think your suggestion is really elegant within those boundaries, but I also think the project ultimately needs more than the current filesystem based routing to cater to the requirements of larger web apps. This is of course something for the Next.js team to decide; I'd appreciate some comments and am thrilled to see this discussion about routing evolve. |
I approached his problem differently, through configuration. Defining routes this way allows for more complex routing. It also allows the file system to maintain semantic meaning. For instance, the route '/users/:id/friends/:id' could point to the file 'pages/users/friend', which avoids the use of overly complex hierarchies and naming conventions. You can see my implementation in #59. I'm still working out a few kinks, but I think it's a good solution to the problem. |
@dstreet Question about your implementation. If I have a route configuration like this:
Then navigating to |
I think @Compulves' solution is the best one yet:
which mapping to:
|
@nmuth That is one of the kinks that needs to be ironed out. Currently it will fall through to the default route, which maps directly to the filesystem, so |
@nodegin how would you implement Something like this?
Also how would you handle more complex routes. For instance, routes with or regex? |
@dstreet If we just use filesystem for the automatic routing then it must be limits, in this case we can use the solution which embedding the routes in the next section of package.json. For
And in this case: We can just change to: |
@dstreet It might be worth having two approaches: one based on the filesystem, like @Compulves suggested, and one based on a configuration file that can handle arbitrary routes and regexes. @nodegin I get what you're saying, but I would rather not sacrifice expressiveness just to avoid using |
@nodegin Assuming On another note, and my solution exhibits the same problem, given the following filesystem, what component would render with the route
That path would match two possible components: |
@nmuth I too am leaning towards to dual approach. My only concern is that it opens up the framework to too much confusion, and how would conflicts between the two solutions be resolved? |
@nmuth To solve the issue of the routes falling through to the filesystem paths, the implementation could be adjusted such that routes: [
{ path: '/posts/:id', module: 'arbitrary/directories/post', accessDirect: false }
] would disallow the default routing from matching on a route where |
|
How about we don't use any folder:
where |
Hah. I like the way you think, @nodegin |
@nodegin, pretty sure |
@nickdandakis How about use
|
This syntax needed to solve this within the filesystem alone is awkward and extremely limiting. |
Naive question (from someone unfamiliar with routing and the decision-making behind Everything is provided as a component (so can use composability and the React component lifecycle): |
Hi guys, please take a look at PR #147 |
We should try to keep keep the need of diving into configuration files to a minimum. Therefore I think we should have parameterized routing with just the file system if we can find a simple, not confusing implementation while also keeping the possibility to use your own system (eg with this exploration #25). |
I'm a little late to the party, but you could use parameters in routes like swaggerize-express does it. tl;dr
|
lol, definitely late to the party :) Thanks @dstreet |
I commented elsewhere that APIs like this will be possible in userland, by using the programmatic API. Quoting: Considering #291 (comment), I suggest you start looking into transforming this into an API like: const nextRoutes from 'next-routes'
const app = next()
createServer(nextRoutes(app)).listen(process.env.PORT || 3000) Users of this approach will have to use Then you can extend This will give you the great expressiveness and ease-of-use, without us having to bloat the core :) |
I've written some code to have next routing behave similar to how .htaccess works in php. My routes file looks like this :
and the interpretation code is done as so :
I might spend some time trying to get this into a package but having moved from php to node and now to next, this is a pretty good solution for me |
I think it would be great to have parameter in URL functionality (for example
/users/:id
) while still keeping the simplicity ofnext
itself.My proposition is to do this for structure:
id.js
will be seen as a parameter path and will receiveid
as prop. You could also go further and make it like this to map/users/:id/friends/:friendid
:If the id is not specified (eg.
/users/
) then we'll first check if there is anindex.js
and otherwise use_id.js
.I created this issue to see if others are interested in this and to see what everyones thoughts/feedback is on this. I'm willing to build this if this is a wanted feature! 😄
The text was updated successfully, but these errors were encountered: