w3c / w3c/ServiceWorker

Need for a mechanism to detect supported conditions and sources in the SW Static Routing API

Open
#1,793 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

TPAC2025
Dominant language
Bikeshed
Stars
3.6k
Forks
324
Avg merge
14d 22h
Merged PRs (30d)
1

Description

The Service Worker Static Routing API defines routing rules using a dictionary. This design presents a potential issue for developers. If a rule includes a condition that the user agent does not recognize or support, it may be silently ignored rather than causing a syntax error. This could lead to the rule being applied in unintended situations, creating unpredictable behavior that is difficult to debug.

For example, if a developer includes a new or experimental condition in a rule, the rule might be partially applied on browsers that don't support that specific condition, effectively ignoring a critical piece of the intended logic. This behavior makes feature detection and graceful degradation difficult to implement reliably.

To address this, I propose the introduction of a mechanism to allow developers to verify which conditions and sources are supported by the user agent. This could be achieved in a few ways:

  1. An API to Enumerate Supported Features: Introduce a method that returns a list of supported condition and source values. For example:
const capabilities = await event.getStaticRoutingCapabilities();
console.log(capabilities.supportedConditions); // ['urlPattern', 'requestMode', ...]
console.log(capabilities.supportedSources); // ['fetch', 'cache', ...]
  1. An API to Validate a Rule: Provide a method that allows developers to check if a set of conditions or sources is supported before adding the rule. This would enable proactive validation.
// Returns true if all specified condition keys are supported by the UA.
const areConditionsSupported = await event.supportsConditions([
  'requestMethod', 
  'or', 
]);

if (areConditionsSupported) {
  // All conditions are supported, so we can safely add the rule.
  await event.addRoutes({
    condition: { 
      or: [
        { requestMethod: "GET" },
      ]
    },
    source: "network"
  });
} else {
  // A condition is not supported. Use fallback logic.
  console.log('A required routing condition is not supported. Using a simpler rule.');
}
  1. A "Required" or "Mandatory" Field in addRoutes(): Add an option to specify that certain conditions are essential for a rule. If a mandatory condition is not supported by the user agent, the addRoutes() call would reject, rather than silently ignoring the condition.
await event.addRoutes([{
  condition: {
    or: [
      { requestMethod: "GET" }
    ]
  },
  source: "network"
  // This would cause the call to fail if `requestMethod` is not supported.
  requiredConditions: ["requestMethod"]
}]);

This topic has been previously discussed in https://github.com/WICG/service-worker-static-routing-api/issues/28.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the Service Worker Static Routing API discussion linked as issue 28 and compare it with the current routing-rule behavior described here. The issue presents three alternative API designs rather than a selected implementation, so done would require an agreed mechanism and corresponding specification changes for detecting unsupported conditions or sources.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.