[w3c/ServiceWorker] Scope matching algorithm breaks sites that don't end in a slash (#1272)

The [*Match Service Worker Registration* algorithm](https://www.w3.org/TR/service-workers-1/#scope-match-algorithm) is a simple string prefix match, rather than a path segment prefix match. This means that if the SW scope does not end in a slash, you get unexpected behaviour, e.g.: if scope is "https://www.google.com/maps", you will match a (hypothetical) URL "https://www.google.com/mapsearch", which is intended to be a different product. The work-around for this issue is to always include a trailing slash in the scope.

I asked the Maps team to do this and they raised an important point: if they did that, then yes, "/mapsearch" would be correctly filtered out of the scope. But then the URL "https://www.google.com/maps" (no trailing slash) would also not be in the SW scope, and thus not hit the fetch handler. Since "https://www.google.com/maps" redirects to "https://www.google.com/maps/", this isn't a big deal, except that it breaks offline support. If the entire of "https://www.google.com/maps/" works correctly offline, but someone links to "https://www.google.com/maps", the user would need to be online to make a network request to "https://www.google.com/maps" to get a redirect to "https://www.google.com/maps/" which would then be served by the SW.

So essentially, developers are forced to make a decision between two bad choices:

- Put a slash on the end, and show a generic offline page for the slashless version of the path, or
- Don't put a slash on the end, and accidentally capture other paths that start with the same letters.

Is there any practical reason why this algorithm is a string prefix match, rather than a path segment prefix match? In a file system, I don't consider "/foobar/baz" to be inside the directory "/foo". Is it possible to change this behaviour now, or is it too late?

If we can't do a path segment match, as a secondary measure, can we add a rule saying if the scope ends with '/', it matches that path without scope. So scope "https://www.google.com/maps/" matches "https://www.google.com/maps" and "https://www.google.com/maps/anything", but not "https://www.google.com/mapsearch".

Note that the Web App Manifest spec has the same algorithm, and it was deliberately chosen for compatibility with the Service Worker algorithm. This is causing similar problems over there; see w3c/manifest#554; particularly [this comment](https://github.com/w3c/manifest/issues/554#issuecomment-362441928). I am making a similar proposal there.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/ServiceWorker/issues/1272

Received on Friday, 2 February 2018 02:20:39 UTC