Skip to content
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

Closed
tashburn opened this issue Jul 3, 2017 · 6 comments

Comments

@tashburn
Copy link
Contributor

tashburn commented Jul 3, 2017

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.

@vanya2h
Copy link

vanya2h commented Jul 3, 2017

What the client you using for fetching?
Did you try Axios? Because I have the same problem with isomorphic-fetch

@timneutkens
Copy link
Member

You'll have to manually pass the cookies off (reading from req) and pass them to axios / isomorphic-fetch

@tashburn
Copy link
Contributor Author

tashburn commented Jul 6, 2017

@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.

@sscaff1
Copy link

sscaff1 commented Oct 10, 2017

@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 => ...)

@tashburn
Copy link
Contributor Author

@sscaff1 Thanks

@tomasdev
Copy link

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.

@lock lock bot locked as resolved and limited conversation to collaborators Feb 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants