matthew-andrews / isomorphic-fetch

Isomorphic WHATWG Fetch API, for Node & Browserify
MIT License
6.95k stars 289 forks source link

Fetching during SSR (using Next) should include cookies #141

Open tashburn opened 7 years ago

tashburn commented 7 years ago

Cookies are included in the Express routing when the fetch code runs on the browser, but when run during SSR with Next, the cookies are not available. I'd expect them to be.

Adding credentials: 'same-origin' or credentials: 'include' to the fetch options did not make a difference.

matthew-andrews commented 7 years ago

Are you sure? This is a very common use case. I believe credentials really should work…

tashburn commented 7 years ago

@matthew-andrews in discussions with the Next people, one said "I have the same problem with isomorphic-fetch", and another said "You'll have to manually pass the cookies off (reading from req) and pass them to axios / isomorphic-fetch"

Any idea how I "pass the cookies" to isomorphic-fetch?

credentials only works when the fetch happens on the browser client. when the fetch happens during SSR, credentials does not work.

matthew-andrews commented 7 years ago

Oh right, on the server, I see…… I think you'd need to add the cookie header manually in that environment. Something like this…

fetch(url, {
  headers: { Cookie: 'name=value' }
});
matthew-andrews commented 7 years ago

on the server, isomorphic-fetch just proxies node-fetch underneath, which as you say does not implement credentials:- https://github.com/bitinn/node-fetch#class-request