-
Notifications
You must be signed in to change notification settings - Fork 27.8k
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
Handle accept headers totally inside Next.js #385
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
@@ -62,12 +62,7 @@ export default class Server { | |||
|
|||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
@@ -50,6 +49,12 @@ export default class Server { | |||
await this.serveStatic(req, res, p) | |||
}) | |||
|
|||
this.router.get('/_next/pages/:path*', async (req, res, params) => { | |||
const path = params.path || [''] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this has to be params.path.join('/')
when :path*
is nested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. Good call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
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.