Skip to content

Add Register a Service Worker algorithm for use by other specifications - #1848

Open
monica-ch wants to merge 2 commits into
w3c:mainfrom
monica-ch:add-register-a-service-worker
Open

Add Register a Service Worker algorithm for use by other specifications#1848
monica-ch wants to merge 2 commits into
w3c:mainfrom
monica-ch:add-register-a-service-worker

Conversation

@monica-ch

@monica-ch monica-ch commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

Part of #1846.

This introduces a new exported algorithm — a stable public surface that external specifications can bind to.

Change

Adds an exported Register a Service Worker algorithm so other specifications such as FedCM and Payment Handler can register a service worker without a client-driven flow.

It also extracts the URL checks from Start Register into a shared Validate and Normalize a Service Worker URL algorithm. Both JavaScript registration and browser-initiated registration now use the same rules:

  • remove URL fragments;
  • require an HTTP or HTTPS scheme; and
  • reject paths containing encoded / or \ characters.

The existing JavaScript behavior is preserved: invalid URLs reject the register() promise with TypeError, and a missing scope still defaults to the script URL's directory. The exported algorithm is fire-and-forget and returns when validation fails.

Exported algorithm

Input: storage key, script URL, scope URL, worker type, and update-via-cache mode
Output: none

The algorithm validates and normalizes the URLs, requires the script and scope to be same-origin, checks that the script origin is potentially trustworthy, creates a register job with a null client and promise, and schedules it.

Because there is no calling service worker client, the algorithm sets the job's referrer to the normalized script URL. This allows the existing Register same-origin checks to remain the canonical enforcement point.

Adoption implications

  • FedCM (Add initial Identity Handler SW Registration Tie In. w3c-fedid/FedCM#842) and Payment Handler can use this instead of invoking non-exported job algorithms directly.
  • The (storageKey, scriptURL, scopeURL, workerType, updateViaCache) signature becomes a stability commitment.
  • Registration remains asynchronous and fire-and-forget; no completion or failure signal is returned.

Related


Preview | Diff

Add a new exported algorithm that other specifications (FedCM, Payment Handler) can call to register a service worker without going through a client-driven flow. The algorithm is a thin wrapper around Create Job and Schedule Job with job type register, taking storage key, script URL, scope URL, worker type, and update via cache mode as inputs.

The algorithm is fire-and-forget: no completion or failure signal is delivered to the caller. Because the job's client is null, the resulting job has no referrer, so the same-origin check between script URL and referrer that Register performs is skipped; a Note documents that callers must satisfy the same-origin precondition asserted in step 1.

Part of w3c#1846.
@monica-ch
monica-ch force-pushed the add-register-a-service-worker branch from d5ba669 to 4656c9e Compare September 2, 2026 19:32
@monica-ch monica-ch changed the title Editorial: add exported Register a Service Worker algorithm Add Register a Service Worker algorithm for use by other specifications Sep 4, 2026
@monica-ch
monica-ch marked this pull request as ready for review September 4, 2026 22:03
@monica-ch

Copy link
Copy Markdown
Collaborator Author

@yoshisatoyanagisawa CYTAL, thanks!

Comment thread index.bs
:: None

1. Assert: |scriptURL|'s [=url/origin=] and |scopeURL|'s [=url/origin=] are [=same origin=].
1. If the result of running <a>potentially trustworthy origin</a> with |scriptURL|'s [=url/origin=] as the argument is <code>Not Trusted</code>, return.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that Start Register (https://w3c.github.io/ServiceWorker/#start-register) does more validation inside. However, such validation looks not implemented here. For example, a scheme is limited to HTTP/HTTPS, a hash will be omitted, etc.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in e053de5. I extracted the fragment removal, HTTP/HTTPS scheme check, and encoded-slash path check into a shared Validate and Normalize a Service Worker URL algorithm. Both Start Register and the exported Register a Service Worker algorithm now use it, while Start Register retains its existing TypeError rejection and scope-defaulting behavior.

@yoshisatoyanagisawa

Copy link
Copy Markdown
Collaborator

Is it possible to have a common algorithm between an algorithm call via the JavaScript register() and the registration initiated by the browser (this case), and make the latter exported? I assume that the script URL validation can be common between the JavaScript and this.

Extract script and scope URL validation from Start Register so browser-
initiated registration uses the same normalization and rejection rules as
the JavaScript registration path.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: e60256f0-04fc-4a2f-b93c-ca14f2fea252
@monica-ch

Copy link
Copy Markdown
Collaborator Author

Thanks, @yoshisatoyanagisawa — updated in e053de5. The URL validation and normalization previously embedded in Start Register is now a shared algorithm used by both the JavaScript and browser-initiated registration paths. Client-specific behavior such as promise rejection and storage-key acquisition remains in Start Register.

The shared helper operates on a copy of each URL, so the exported algorithm does not mutate URL records owned by its caller. PTAL when you have a chance.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants