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

Custom server Express doesn't match routes Express routes? #2710

Closed
bresson opened this issue Aug 4, 2017 · 5 comments
Closed

Custom server Express doesn't match routes Express routes? #2710

bresson opened this issue Aug 4, 2017 · 5 comments

Comments

@bresson
Copy link

bresson commented Aug 4, 2017

https://github.com/zeit/next.js/tree/master/examples/custom-server-express

Based on the documentation for custom-server-express, my understanding is the express backend can match income requests and render the appropriate assets. The example leads me to believe it would be possible to define routes using Express However, when any of the <Link .../> components displaying on a page is clicked, the browser will bypass the matching route on the backend and proceed directly to the catch all route.

For instance, in the custom-server-express example, the following request will match '/a' and respond with a rendering of the page at 'b'.

server.get('/a', (req, res) => { return app.render(req, res, '/b', req.query) })
However, the match never seems to occur on the backend. Am I mistaken in my understanding of the documentation?

@bresson bresson changed the title Custom server Express doesn't match Express routes? Custom server Express doesn't match routes Express routes? Aug 4, 2017
@unregistered
Copy link
Contributor

unregistered commented Aug 4, 2017

When you click the <Link> component, Next.js will fetch only the data and render the next page on the client instead of invoking SSR. Here's one example which integrates express routes with next.js: #2594

@nickdandakis
Copy link

@bresson, can you share your entire server.js?

@bresson
Copy link
Author

bresson commented Aug 4, 2017

@unregistered Thanks for your answer and link. Have I misunderstood the Next.js 2.0 documentation and examples which discuss custom server routing? Here's a link to the example which I think documents the feature : https://github.com/zeit/next.js/tree/master/examples/custom-server-express

@nickdandakis I'm only experimenting with the Next.js custom server example in the link above. The extent of my experimentation is trying to switch routing matches on the backend

@nickdandakis
Copy link

@bresson, I just cloned and ran that example. Works exactly as intended. Navigating to /a renders pages/b.js, and navigating to /b renders pages/a.js.

I now understand your question though. Using <Link> navigates to the actual page the href is set to. When you click on a <Link>, the client-side application navigates to the route, which never even reaches server.js. It's a bit easier to understand if you click on a <Link> and then hard refresh the page, to trigger a server render (or by looking at "View source" of the page).

@timneutkens
Copy link
Member

timneutkens commented Aug 5, 2017

Okay so what you have to do is this:
<Link href="/page?id=1" as="/blog/hello-world"><a>Your link</a></Link>
This will render /page?id=1 but set the url to /blog/hello-world

@lock lock bot locked as resolved and limited conversation to collaborators May 11, 2018
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

4 participants