When testing the SharedWorker in Chrome, we found that a ShareWorker does not
persist during page transitions.
If page A creates a SharedWorker myworker:
var worker1 = new SharedWorker('shared.js', myworker);
When page A transitions to page B (same origin as A) who creates the same
myworker:
var worker2 = new SharedWorker('Websocket.js', myworker);
the worker1 is released and does not persist to page B.
It seems the only way to share myworker between the pages is for pages A and B
to be open at the same time.
This causes several problems for applications, for example for persistent
WebSocket, as discussed in [1]-[5].
To address this problem, we propose to add an optional 4th parameter to the
constructor of SharedWorker to indicate the target pages (of the same origin)
it should persist to:
var worker1 = new SharedWorker('shared.js', myworker, [URI_to_page_B]);
There is no change to page B code.
When page A transitions to page B, the browser will delay release of worker1
until page B JavaScript context is initialized, i.e. worker2 is created by page
B.
When page B transits back to page A, the worker2 will persist similarly as
worker1.
To simplify the target page parameter, we can also use URI prefixes to specify
a range of target pages, or [*] to indicate all pages, of the same origin.
This approach is fully backward compatible and provides a safe and simple
extension to persist ShareWorker without opening multiple pages.
Thanks.
Li Li
Huawei
[1] http://stackoverflow.com/questions/9578680/reusing-websockets-between-pages
[2]
http://stackoverflow.com/questions/10886910/how-to-maintain-a-websockets-connection-between-pages
[3]
http://stackoverflow.com/questions/9554896/sharing-websocket-across-browser-tabs
[4]
http://stackoverflow.com/questions/9336205/using-websockets-for-live-feeds-across-multiple-web-pages
[5]
http://stackoverflow.com/questions/9336774/do-shared-web-workers-persist-across-a-single-page-reload-link-navigation/9337749#comment11795892_9337749<http://stackoverflow.com/questions/9336774/do-shared-web-workers-persist-across-a-single-page-reload-link-navigation/9337749>