Custom server Express doesn't match routes Express routes?
See original GitHub issuehttps://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?
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
@bresson, I just cloned and ran that example. Works exactly as intended. Navigating to
/a
renderspages/b.js
, and navigating to/b
renderspages/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 reachesserver.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).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