Sketch for an asynchronous cookies API: https://github.com/WICG/async-cookies-api; excerpt from the explainer:
Async cookies API explained
This is a proposal to bring an asynchronous cookie API to scripts running in HTML documents and service workers.
HTTP cookies have, since their origins at Netscape (documentation preserved by archive.org), provided a valuable state-management mechanism for the web.
The synchronous single-threaded script-level
document.cookie
and<meta http-equiv="set-cookie" ...>
interface to cookies has been a source of complexity and performance woes further exacerbated by the move in many browsers from:
- a single browser process,
- a single-threaded event loop model, and
- no general expectation of responsiveness for scripted event handling while processing cookie operations
… to the modern web which strives for smoothly responsive high performance:
- in multiple browser processes,
- with a multithreaded, multiple-event loop model, and
- with an expectation of responsiveness on human-reflex time scales.
On the modern a web a cookie operation in one part of a web application cannot block:
- the rest of the web application,
- the rest of the web origin, or
- the browser as a whole.
Newer parts of the web built in service workers need access to cookies too but cannot use the synchronous, blocking
document.cookie
and<meta http-equiv="set-cookie" ...>
interfaces at all as they both have nodocument
and also cannot block the event loop as that would interfere with handling of unrelated events.
A taste of the proposed change
Although it is tempting to rethink cookies entirely, web sites today continue to rely heavily on them, and the script APIs for using them are largely unchanged over their first decades of usage.
Today writing a cookie means blocking your event loop while waiting for the browser to synchronously update the cookie jar with a carefully-crafted cookie string in
Set-Cookie
format:
document.cookie = '__Secure-COOKIENAME=cookie-value' + '; Path=/' + '; expires=Fri, 12 Aug 2016 23:05:17 GMT' + '; Secure' + '; Domain=example.org'; // now we could assume the write succeeded, but since // failure is silent it is difficult to tell, so we // read to see whether the write succeeded var successRegExp = /(^|; ?)__Secure-COOKIENAME=cookie-value(;|$)/; if (String(document.cookie).match(successRegExp)) { console.log('It worked!'); } else { console.error('It did not work, and we do not know why'); }
What if you could instead write:
cookieStore.set( '__Secure-COOKIENAME', 'cookie-value', { expires: Date.now() + 24*60*60*1000, domain: 'example.org' }).then(function() { console.log('It worked!'); }, function(reason) { console.error( 'It did not work, and this is why:', reason); }); // Meanwhile we can do other things while waiting for // the cookie store to process the write...
This also has the advantage of not relying on
document
and not blocking, which together make it usable from service workers, which otherwise do not have cookie access from script.
This proposal also includes a power-efficient monitoring API to replace
setTimeout
-based polling cookie monitors with cookie change observers.
There is also a document
-only polyfill which works in recent Chromium and Google Chrome developer builds with the Experimental JavaScript chrome://flags/#enable-javascript-harmony flag active.