From ef2959c8858576e6b3f5441dd97b56ee4081e41a Mon Sep 17 00:00:00 2001 From: sisidovski Date: Mon, 20 Nov 2023 17:50:38 -0800 Subject: [PATCH] Add baseURL to the URLPattern condition while routing rule regustration This CL changes the behavior of the router rule registration in the ServiceWorker Static Routing API, especially when the |urlPattern| condition accepts the URLPatternInit object. Before this CL, the URLPatternInit input is accepted as it is, that means any unspecified fields are resulted in the wildcards (*). This behavior is inconsistent with the case when |urlPattern| accepts a string. When a string is passed, missing fields are complemented by baseURL, which the SW script URL is internally used when adding the new router rule. So some missing components will use the values inherited from the baseURL in that case. After this CL, the URLPatternInit input also internally uses the SW script URL as a baseURL if not provided. It typically happens when the input is an object that complies with the URLPatterInit interface. On the other hand, it wouldn't affect the case when URLPatternInit is the output of `new URLPattern()`. Thank to the recent change on the URLPattern[1], the constructed components in URLPatternInit will inherit or wildcarded. baseURL won't override components if those are not empty. This behavior change is based on the discussion in https://github.com/whatwg/urlpattern/issues/182. [1] https://github.com/whatwg/urlpattern/pull/198 Bug: 1371756 Change-Id: I5cce80fde05cf18237c8b6412b00e017ff5aad5b --- .../static-router/resources/router-rules.js | 57 +++++-------- .../static-router-main-resource.https.html | 2 +- .../static-router-subresource.https.html | 82 +++++++++++++++++-- 3 files changed, 99 insertions(+), 42 deletions(-) diff --git a/service-workers/service-worker/tentative/static-router/resources/router-rules.js b/service-workers/service-worker/tentative/static-router/resources/router-rules.js index 7b6a7144dbb655..67871a2d4fd3fe 100644 --- a/service-workers/service-worker/tentative/static-router/resources/router-rules.js +++ b/service-workers/service-worker/tentative/static-router/resources/router-rules.js @@ -2,44 +2,29 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. const routerRules = { - 'condition-url-pattern-source-network': [ - { - condition: { - urlPattern: new URLPattern({ - pathname: '/**/direct.txt' - }) - }, - source: 'network' - }, - + 'condition-urlpattern-constructed-source-network': [{ + condition: {urlPattern: new URLPattern({pathname: '/**/direct.txt'})}, + source: 'network' + }], + 'condition-urlpattern-urlpatterninit-source-network': [ + {condition: {urlPattern: {pathname: '/**/direct.txt'}}, source: 'network'}, ], - 'condition-request-source-network': [ - { - condition: { - requestMode: 'no-cors' - }, - source: 'network' - } - ], - 'condition-or-source-network': [ - { - condition: { - or: [ - { - or: [ - { - urlPattern: '/**/or-test/direct1.*??*' - } - ], - }, - { - urlPattern: '/**/or-test/direct2.*??*' - } - ] - }, - source: 'network' - } + 'condition-urlpattern-string-source-network': [ + {condition: {urlPattern: '/**/direct.txt'}, source: 'network'}, ], + 'condition-request-source-network': + [{condition: {requestMode: 'no-cors'}, source: 'network'}], + 'condition-or-source-network': [{ + condition: { + or: [ + { + or: [{urlPattern: '/**/or-test/direct1.*??*'}], + }, + {urlPattern: '/**/or-test/direct2.*??*'} + ] + }, + source: 'network' + }], }; export {routerRules}; diff --git a/service-workers/service-worker/tentative/static-router/static-router-main-resource.https.html b/service-workers/service-worker/tentative/static-router/static-router-main-resource.https.html index dda75f20f4bc4b..523ececf317ede 100644 --- a/service-workers/service-worker/tentative/static-router/static-router-main-resource.https.html +++ b/service-workers/service-worker/tentative/static-router/static-router-main-resource.https.html @@ -10,7 +10,7 @@ +