Published: Jul 31, 2025
From Chrome 139 participate in a new origin trail for extended lifetime shared workers. The trial adds a new extendedLifetime: true
option to let shared workers live beyond the last document unload.
The use case for the extended lifetime feature
Many sites want to perform some work as the user navigates away from the page. For example, writing to storage, or sending data back to servers, to save state or record analytics.
The web platform provides a few APIs to deal with some of the simpler use cases, but each has limitations:
- Synchronous JavaScript APIs, such as the
localStorage
writes, are run to completion before unloading the current page. - The
fetch API
has a number of options likekeepalive
, and more recentlyfetchLater
, that lets send requests outlive the document unload for a short period.
However, these only cover synchronous work except for the final fetch
request. They don't allow use of asynchronous APIs like IndexedDB, Compression Streams, or Web Crypto to hash or encrypt. Many APIs, particularly newer APIs, are asynchronous to avoid blocking the main thread so not being able to use these APIs on unload is restrictive.
The alternative is to use service workers, which live outside of individual page lifecycles. However, this is a rather heavyweight solution, with more complex lifecycle and management requirements for developers, not to mention additional process and memory requirements for users. It also don't match the main use case of service workers (to act as a proxy for network requests). Using full service workers solely for the use case of completing some work on page unload seems overkill.
The proposed solution
The SharedWorker API is a lighter API used to offload work from the main thread. However, they currently don't live beyond the origin's lifetime (when the last page for that origin is unloaded). Chrome is proposing adding a new option to the SharedWorker API to let shared workers outlive document destruction for a short period of time.
The HTML Standard already encourages implementations to keep shared workers alive for a short time after document unloading, so that navigating between same-origin pages doesn't tear down and then re-create the shared worker. The extended lifetime proposal just extends this by suggesting that, even if the user is not navigating to a same-origin destination, the user agent should keep the shared worker alive for some amount of time, so that async work can be finished.
The proposal is to allow shared workers to live on after the last document unload, for the same amount of time as server workers are allowed to remain idle—which is 30 seconds for Chrome. Note that for shared workers this is a maximum lifetime after unload, rather than an idle time. That is, the 30 second limit starts from unload, rather than from idle time. Work that has started and not yet completed within that time period will be cancelled.
Enable the extended lifetime
The feature can be enabled on sites for users by registering for an origin trial for extended lifetime shared workers. Alternatively developers can enable for their own browser using the chrome://flags/#enable-experimental-web-platform-features
flag.
Example code
After opting into the trial or feature flag, enable extended lifetime as follows:
const myWorker = new SharedWorker("worker.js", { extendedLifetime: true });
As shared workers also support blobs, this can also be enabled without a separate script. For example to write data to an IndexedDb:
const sharedWorkerScript = `
const transaction = db.transaction("analytics", "readwrite");
const store = transaction.objectStore("analytics");
const request = store.get("visitCount");
request.onsuccess = (event) => {
const newCount = (event.target.result || 0) + 1;
store.put(newCount, "visitCount");
};
`;
document.addEventListener("pagehide", () => {
const blob = new Blob([sharedWorkerScript], { type: "text/javascript" });
const blobURL = URL.createObjectURL(blob);
new SharedWorker(blobURL, { extendedLifetime: true });
});
We also have an example application here: https://sharedworker-extendedlifetime.netlify.app/. When the page is reloaded (or closed and reopened within 30 seconds), the previous calculation is still available.
Shared workers are viewable for a site at: chrome://inspect/#workers
and this will shortly be enhanced to show whether the extendedLifetime
option was used. Extended lifetime shared workers will also continue to show on this page for 30 seconds after page unload.
Share your feedback
We look forward to hearing your feedback on the extended lifetime shared worker origin trail.
The API shape is being discussed on GitHub and we have a more detailed technical explainer.
For feedback on Chrome's implementation, file a Chromium bug.