-
Notifications
You must be signed in to change notification settings - Fork 26.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
Data-fetch using isomorphic-fetch (per the example) doesn't include cookie on server. #2455
Comments
What the client you using for fetching? |
You'll have to manually pass the cookies off (reading from req) and pass them to axios / isomorphic-fetch |
@kv9991 what do you mean by "client"? @timneutkens thanks. i'll see if i can find out how to pass them to isomorphic fetch -- hopefully as a callback, because they may change, and isomorphic-fetch always needs the latest. |
@tashburn you can do something like this: const options = {
method: 'GET',
credentials: 'include', // for client side requests
};
if (req) {
options.headers = { cookie: req.headers.cookie }; // for server side request
}
const prefix = req ? `${req.protocol}://${req.get('Host')}` : window.location.origin;
return fetch(`${prefix}/admin/api/endPoint, options).then(res => ...) |
@sscaff1 Thanks |
Thank you all for this. Found out the solution by myself, but heh it was somewhere! Would love to see this as part of the docs explaining how isomorphic fetch works. |
Here's the data fetch example: https://github.com/zeit/next.js/tree/v3-beta/examples/data-fetch
When there is a cookie, that cookie is part of the request as seen from the server only when the fetch is a browser-side (non-SSR) fetch.
When fetching during SSR, the cookie is not part of the request that the server sees.
I expect the cookies to be available on the server (to Express routes) isomorphically -- during SSR and otherwise.
The text was updated successfully, but these errors were encountered: