Description
We're seeing URLPattern being used in several proposals now:
- Speculation rules
- Service worker static routing
- Hopefully, compression dictionaries
- Potentially in the future, its original motivation, service worker scopes or web app manifest scopes.
I think we need clearer guidance on how to design APIs that accept URL patterns, as we've started to see some slight divergence.
Scenarios to consider:
- JS APIs:
- Should accept
URLPattern
objects - What "shorthands" / "raw inputs" can they accept? Keep in mind that
URLPattern
has multiple constructor forms:new URLPattern({ ...componentsDictionary, baseURL })
,new URLPattern(patternString, baseURL)
, and then variants of each of those with another options argument. - My suggestion:
- Such APIs should have a default base URL, and then accept either:
{ ...componentsDictionary }
(which automatically gets the base URL),{ ...componentsDictionary, baseURL }
(overriding the base URL), orpatternString
(which automatically gets the base URL).
- Downsides of my suggestion:
- The shorthand form can't set the options (i.e.
ignoreCase
) - The shorthand form can't combine a string with an overridden base URL (i.e., there is no counterpart to the
new URLPattern(patternString, baseURL)
constructor). - The shorthand form depends on fixing Base URL inheritance gives unintuitive results #179 to give intuitive results for this kind of implicit base URL inheritance
- The shorthand form can't set the options (i.e.
- Should accept
- HTTP headers or other string-accepting APIs: should accept input strings, with some natural base URL automatically applied. (This is what Switched the match param to use a URLPattern WICG/compression-dictionary-transport#51 proposes.)
- JSON APIs:
- My suggestion (and what we implemented for speculation rules): the same as the "shorthands" sub-question for JS APIs.
- Some of the downsides above become more serious here, because there's no
new URLPattern()
escape hatch; the only possible form is the "shorthand" form.
- Some of the downsides above become more serious here, because there's no
- My suggestion (and what we implemented for speculation rules): the same as the "shorthands" sub-question for JS APIs.
Other considerations: is there any place for pathname-only patterns? IMO probably not because anytime you match on pathname you should also allow matching on search, i.e. discriminating between URL patterns like /products/*
and /product?id=*
is not good. But it's come up a few times.
My proposal is to use the above rules for such situations, and enforce "pathname + search only" by checking that the resulting URL pattern's protocol/username/password/hostname/port are set to match the base URL's origin. Providing a spec helper for such a check would be a good idea, I think.