Need for a mechanism to detect supported conditions and sources in the SW Static Routing API
Nobody has claimed this yet.
- 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:
- 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', ...]
- 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.');
}
- 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, theaddRoutes()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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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