Skip to content

Extended lifetime shared workers #492

Open

Description

@domenic

WebKittens

@annevk @youennf

Title of the proposal

Extended lifetime shared workers

URL to the spec

whatwg/html#10997 (no spec PR quite yet)

URL to the spec's repository

https://github.com/whatwg/html

Issue Tracker URL

No response

Explainer URL

https://gist.github.com/domenic/c5bd38339f33b49120ae11b3b4af5b9b#file-1-explainer-md

TAG Design Review URL

w3ctag/design-reviews#1089

Mozilla standards-positions issue URL

mozilla/standards-positions#1227

WebKit Bugzilla URL

No response

Radar URL

No response

Description

We propose adding a new option to the SharedWorker constructor that serves as a request to extend its lifetime after all current clients have unloaded:

const sharedWorker = new SharedWorker(url, { extendedLifetime: true });

The primary use case here is to allow pages to perform some async work that requires JavaScript after a page unloads, without needing to rely on a service worker.

Since @annevk mentioned WebKit has some trepidation about the existing extended lifetime service workers, let me emphasize a point from the explainer that we intend this new feature to provide parity with such service workers, and not give any capabilities beyond. For example, if WebKit decided to shut down all service workers for non-running pages, they would also do so for these shared workers.

The main motivation here is to reduce redundant service worker (ab)use that people currently have to resort to for this sort of work. (And which we've seen evidence of in the wild.)

Activity

  1. annevk commented on May 19, 2025

    @annevk
    Contributor

    I discussed this with colleagues and we do indeed consider the extended lifetime of service workers an issue. It's 10 seconds at the moment, but there's not a lot of guarantees around it so you might get far short of that number.

    And while we concede that this in principle does not have to give new capabilities, we don't think it's a good idea for these reasons:

    1. https://w3ctag.github.io/design-principles/#leave-the-web-better
    2. It doesn't seem worth building out the infrastructure for this.

    We rather have people continue to use service workers for this, with the reliability that service workers advertise.

    As such, I suggest we resolve this with "position: oppose" one week from now.

  2. domenic commented on May 20, 2025

    @domenic
    Author

    Thanks for the quick response!

    It's 10 seconds at the moment

    Are you sure? https://github.com/WebKit/WebKit/blob/a4ab006bdf55fb59302f8cffb4c8ade78cdf0dad/Source/WebKit/NetworkProcess/NetworkProcess.cpp#L2964 seems to imply 70 seconds.

    1. https://w3ctag.github.io/design-principles/#leave-the-web-better

    It's a bit unclear how to apply the principle in this case. I would argue that allowing web developers to use less user resources (by not spinning up long-running and persistently-stored service workers) is leaving the web better than you found it, i.e. "Where possible, build new web capabilities that improve the overall platform quality by mitigating existing defects.".

    2. It doesn't seem worth building out the infrastructure for this.

    This is fair. Do you have any thoughts on what sort of data might change your assessment on this? For example, if we were able to estimate how many user CPU-hours / disk loads / etc. were saved by Chromium's implementation with an A/B experiment? Or maybe just as simple as how many page loads go through a service worker vs. not?

  3. annevk commented on May 21, 2025

    @annevk
    Contributor

    That's the time a service worker has to handle a navigation fetch, as I understand it. https://github.com/WebKit/WebKit/blob/ab1ae05fa010398600e34dd4914aab14239717f9/Source/WebCore/workers/service/server/SWServer.h#L260-L261 is the one I was talking about.

    An A/B experiment could be interesting for sure as well as more information as to which websites would want to adopt this (but would not want to work offline).

  4. domenic commented on May 22, 2025

    @domenic
    Author

    (I think there might be ways of initiating fetches that trigger that 70 second delay as part of unloading, but maybe the smaller delays win. Doesn't matter much.)

    That sounds good. We'll report back with results from the origin trial and other statistics when they're available.

  5. yoshisatoyanagisawa commented on Apr 2, 2026

    @yoshisatoyanagisawa

    Hi @annevk, @youennf,

    Following up on our earlier discussion by @domenic, I'd like to share the key findings from our Origin Trial (OT) involving 35 participating origins and how they’ve shaped the finalized proposal.

    1. Interoperability and the "Navigation Gap" The OT confirmed a strong developer demand for a guaranteed way to maintain state during same-origin navigations within a single tab. While the HTML Standard encourages implementations to keep SharedWorkers alive briefly after unload, current implementations vary, leading to "races" where workers may terminate prematurely. extendedLifetime provides a spec-defined, interoperable mechanism to bridge this gap reliably.

    2. Addressing Technical "Footguns": Web Locks A technical takeaway from the OT was the identification of a potential issue with Web Locks. We discussed that a long-lived worker could indefinitely hold a lock required by a client that had already unloaded. To ensure safety, we decided to enforce a strict separation between extended and non-extended lifetime workers, preventing resource management issues that could arise from overlapping lifetimes.

    3. Operational Constraints vs. Service Workers The OT highlighted that while Service Workers are a possible alternative, the operational burden of hosting a same-origin script file is a major blocker for many third-party library providers. Feedback showed that the ability to instantiate SharedWorkers via blob: URLs (where CSP allows) offers a much more lightweight and deployable integration path for cleanup and final synchronization tasks.

    4. Standardization The feature was successfully integrated into the HTML Standard during the trial period. We believe these technical refinements and the evidenced demand make this a safe and valuable addition to the web platform.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions