-
Notifications
You must be signed in to change notification settings - Fork 27.1k
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
Example: Passing data from server through API #2594
Conversation
console.log('Express is handling request') | ||
const itemData = api.getItem() | ||
|
||
res.format({ |
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.
Since this is an example, may be it's better to do this inside the "server.js" to keep it simple and short.
And I think we don't need to use res.format
as well.
do something like this:
server.get('/item', (req, res) => {
const itemData = api.getItem()
return app.render(req, res, '/item', { itemData })
})
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.
The res.format is to support isomorphic rendering of that page so it works when linked to. I could also mount 2 separate routes, one at /item
which renders the html, another at maybe /_data/item
which sends the json. But I've found that doing it in the same route method is more concise. Seems to me that it would be necessary, but let me know if I'm misunderstanding something.
I can move the route to server.js though, and leave how they break things off up to the reader.
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.
Then may be you need to expose a different route for the data.
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.
Updated the CR. Is that what you had in mind?
This is great. Could you send this against the |
@arunoda here it is against v3-beta, though there's another change in package.json that you'd have to verify. If it's not I can open a new PR with v3-beta on my fork. Edit: nevermind, it looks like that will go away with a merge conflict resolve. |
Hi @arunoda I've noticed a downside to doing it this way, which is that next inserts all the data passed into
This can be undesirable when injecting a large amount of data that's used incidentally during rendering, or passing sensitive data the user should not be able to see. Any advice? |
Hi @arunoda do you have any thoughts on my latest comment regarding leaking data in the HTML? |
@unregistered I think this is because: is calling this: https://github.com/zeit/next.js/blob/canary/lib/utils.js#L49 Which checks to see if there's a If it is it'll spread those props into If you could somehow call https://github.com/zeit/next.js/pull/2594/files#diff-3f14957c7ec2d722cdf540ed20059dd4R19 it should hopefully not inject those props (but I haven't tried it yet) per this: If not, then maybe @arunoda knows, however, this is a good example and I'd like to see it get added! EDIT: this looks like it does something similar: https://github.com/zeit/next.js/blob/18f8ab392ae2a93140df2545f537c544c1b88241/examples/with-apollo-auth/lib/withData.js#L42 |
* Add a nerv example. * Fix for indentation/style * Fix for name
Any news? |
@danielbayerlein we currently use a variation of this on our site, except instead of passing data through query, we mutate the |
Will merge when the tests pass 🙏 |
@timneutkens Thank you!!! 💚 |
As requested by @arunoda here is an example of how to pass data in the js api through the
query
params. Relates to Issue #1117I opened this against the master branch but let me know if it should go against v3 beta