-
Notifications
You must be signed in to change notification settings - Fork 319
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 navigator.serviceWorker.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 navigator.serviceWorker.supportsConditions([
'requestMethod',
'or',
]);
if (areConditionsSupported) {
// All conditions are supported, so we can safely add the rule.
await registration.routing.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 registration.routing.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 WICG/service-worker-static-routing-api#28.