Repository navigation
Handle accept headers totally inside Next.js - #385
Conversation
Now user doesn't need to handle it anymore.
| if (accept.type(['json', 'html']) === 'json') { | ||
| // When we are asking for the page json, we always need to give | ||
| // the real pathname. But not the pathname user provided. | ||
| const { pathname: realPathname } = parse(req.url) |
There was a problem hiding this comment.
Now we've to parse the url twice to get the pathname. But we don't need query params just like in the other case.
This might add some overhead but it's negligible because of following reasons:
- Now we don't parse accept headers twice
- Rendering the React component on the server is so much costly than this.
|
|
||
| this.router.get('/:path*', async (req, res) => { | ||
| const { pathname, query } = parse(req.url, true) | ||
| const accept = accepts(req) |
There was a problem hiding this comment.
Now we don't need to parse accept headers twice. We only do it once.
|
I also was considering this way which I think is a valid option but has some problems.
|
It isn't too much magical. We need to document what's its for inside our code. May be going back to
Yes it is. But that might not be general use of the public API. If the user wanna do that, he know what's he is going. Most of the user may don't need to use it. (If that's not the case correct me) |
Because it's generally a nicer way. I think we can change it if we have valid reasons.
A possible use case of I think API would be cleaner if the difference of |
| return handle(req, res) | ||
| } | ||
| next() | ||
| }) |
There was a problem hiding this comment.
I was rather thinking how to make handling JSON easier, for example, we can support a middleware-like API.
server.use(app.handleJSON())If server is not express:
function requestHandler (req, res) {
app.handleJSON(req, res, (err) => {
// called only if Accept header is not JSON.
})
}There was a problem hiding this comment.
I think this is something pretty great.
I'll change the API to be like this.
Anyway, how about changing it back to .json. Accept headers won't give us any advantages but problems.
With this way, we could keep actual URLs and page JSON separately. Actually, that's how they work.
| }) | ||
|
|
||
| this.router.get('/_next/pages/:path*', async (req, res, params) => { | ||
| const path = params.path || [''] |
There was a problem hiding this comment.
I think this has to be params.path.join('/') when :path* is nested.
|
So cool |
* add 'next' api * add render APIs * add 'as' prop to Link * check Accept header to serve json response * check if response was finished on getInitialProps call * move server/app to server/index * load webpack-hot-middleware-client by absolute path * server: options for testing * add tests * example: improve * server: make dir optional * fix client routing * add parameterized routing example * link: fix display url * Add custom-server-express example (#352) * Add custom-server-express example * Remove extraneous nexts in express routes defs * Update next config in server.js * Handle accept headers totally inside Next.js (#385) * Handle accept headers totally inside Next.js Now user doesn't need to handle it anymore. * Move json pages serving to /_next/pages base path. * Join paths correctly. * remove next/render
Now user doesn't need to handle accept headers anymore. So the code looks like this:
So now users doesn't need to know anything about accept headers and routing looks so simple.