Chrome/FF follow redirect before session is fully saved
See original GitHub issueHere’s a simple testcase incrementing a counter: https://gist.github.com/antishok/fb3d003d16eb72f672a7cc36401657d9 On chrome I have to refresh after incrementing in order to see the new count.
In response to a request, I write to the session and send a redirect to another page, but the new page is rendered with the old session data, and the changes I just wrote are not visible. The new data appears only after refreshing the page.
This only happens in Chrome, maybe due to an optimization they added that follows a redirect before the redirect response even ended.
I’m using connect-session-knex
with postgresql to store my session data. If I use the MemoryStore, or use sqlite3 instead of postgresql, it works fine, but probably only because writing to them is much quicker.
The gist also includes debug logs for the request (one when I tested with chrome, and one with firefox). You can see that for chrome, the new url is fetched before the session has finished saving, and before the redirect response ends.
(The logs include a log I manually added in the beginning of express-session
’s writeend()
function, to indicate when a response actually ends)
Thanks! And thanks to @joepie91 for figuring out where the issue lies
Issue Analytics
- State:
- Created 7 years ago
- Comments:28 (8 by maintainers)
Top GitHub Comments
settings of ‘resave’ and ‘saveUninitialized’ may cause race conditions. I configured my app to work with them set to ‘false’ They are true by default. Read document for more detail info. https://www.npmjs.com/package/express-session#saveuninitialized
Yes, it could, but this would be a leaky abstraction and a footgun.
I understand the concerns, but this would not be any different when calling
save
manually - you still have to wait for the session save to complete before writing the header. Sinceexpress-session
already short-circuits when there’s no modified session data to save, a patch inexpress-session
for redirects would not incur a performance penalty.That’s a workaround, not a fix.