Skip to content
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

Route matching with params #59

Closed
nickdandakis opened this issue Oct 25, 2016 · 20 comments
Closed

Route matching with params #59

nickdandakis opened this issue Oct 25, 2016 · 20 comments

Comments

@nickdandakis
Copy link

Is there any support for route params like so?
/photos/:id

If not, how would that look with the proposed pages/*.js structure?

@nodegin
Copy link
Contributor

nodegin commented Oct 25, 2016

Similar question, what if I place a directory under /pages?

e.g. /pages/test/profile.js should mapping to /test/profile etc.

@impronunciable
Copy link
Contributor

@nodegin yes, it maps that way
screen shot 2016-10-25 at 7 40 59 pm

@nickdandakis It's not possible right now. It doesn't mean it won't be possible in the future.

@CreepGin
Copy link

Since zeit.co is built with next.js, do all their blog urls just map to static files???

  • https://zeit.co/blog/next -> /pages/blog/next.js
  • https://zeit.co/blog/now-certs -> /pages/blog/now-certs.js

Anyhow, I do believe the majority of non-trivial sites depend on some form of dynamic routing (route params). So this should be pretty high up there in the future todo list, right?

@arunoda
Copy link
Contributor

arunoda commented Oct 26, 2016

I'm so happy to (and want to) move a lot of apps to next once we've dynamic routing.

@nkzawa
Copy link
Contributor

nkzawa commented Oct 26, 2016

See also #25. It's the issue about custom server support which you can implement any routings.

@dstreet
Copy link
Contributor

dstreet commented Oct 27, 2016

I'm proposing a change where parameterized routes can be defined in package.json. Mapping a parameterized route path to a specific file in the paths directory:

{
    "next": {
        "routes": {
            "/posts/:id": "posts/post",
            "/posts/:id/comments": "posts/post/comments"
        }
    }
}

This implementation shouldn't interfere with existing routing logic. Parameters are also passed through the component props via params.

Implementation:
dstreet@55d3173

@dlindenkreuz
Copy link
Contributor

Related: #91

@beepboopitschloe
Copy link

@dstreet Personally, I think package.json should only contain NPM configuration (i.e. dependencies, scripts, and metadata). I'd prefer using a next.conf.js file for configuration specific to Next. Otherwise I like the idea 👍

@dstreet
Copy link
Contributor

dstreet commented Oct 27, 2016

@nmuth, I completely agree. Not sure why I didn't think of that at first.

@sebasgarcep
Copy link

I have parametric routing working using the fantastic https://github.com/hapijs/call. It allows me to define all sorts of parametric routes. For example a /users/{name}.js file will match to either of /users/tom or /users/mark without having to do any configuration. It is what HapiJS uses to do routing, so it's battle-tested.

@parkerproject
Copy link

@sebasgarcep do you have an example or docs on how to use this?

@arunoda
Copy link
Contributor

arunoda commented Dec 26, 2016

Hey all, now we've support for custom routes.
See: https://github.com/zeit/next.js#custom-server-and-routing

@arunoda arunoda closed this as completed Dec 26, 2016
@sebasgarcep
Copy link

@arunoda I have some issues with the way custom routes work. Mainly that Next does not exposes its render methods and you have to pass it the raw request and response objects. Of course, that doesn't always plays nice with frameworks like Hapi when you want to embed Next.

@parkerproject If you're still interested you can email me.

@rauchg
Copy link
Member

rauchg commented Dec 26, 2016

Mainly that Next does not exposes its render methods and you have to pass it the raw request and response objects.

Requiring the raw request and response objects sounds like a good abstraction. Maybe Hapi should expose them, rather than us not require them?

@sebasgarcep
Copy link

sebasgarcep commented Dec 27, 2016

They do expose them 😄, though it is not recommended to interact directly with them.

Anyways, thanks for the input. It's a great project you have here.

@rauchg
Copy link
Member

rauchg commented Dec 28, 2016

@sebasgarcep also, we do plan on exposing low-level APIs (they have been picked and designed with that in mind), but when we played around with them, they ended up being really not ergonomic.

Furthermore, we found that app.render covers the majority of use cases (like caching, fancy routing, etc) without requiring more than just the raw, low-level HTTP request / response objects.

@sheerun
Copy link
Contributor

sheerun commented Dec 13, 2017

I've prepared with-extracted-stylesheet example with custom routes that fallback to appropriate pages in both development and production, even for dynamic urls like described here: #3451

@hariantara
Copy link

how about this one. project 1 (SSR using next js) project 2 (the same), but project 1 front end call the project 2 server, how do i get the params in server of project 2 that url is rendered in project 1 on front end side?

@fortil
Copy link

fortil commented Sep 18, 2018

if I want to export the routes to static files your solution (Custom server and Routing) works? @arunoda

@trusktr
Copy link

trusktr commented Oct 21, 2018

@fortil, I think it does not work with static routes and that it requires a server. A static server wouldn't be able to replace an ID in the URL with static-served content (if it could then it wouldn't be a static server anymore).

What I mean is, if we have a URL /posts/some-post-from-a-database, there's no way a static server could map that to a file that actually exists. The server is needed in a rider to fetch the data for the post and to serve content not based on the filesystem.

ijjk pushed a commit to ijjk/next.js that referenced this issue Apr 13, 2019
* Add Travis CI example
* Fixes changes requested in pull request 59
@lock lock bot locked as resolved and limited conversation to collaborators Oct 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests