From b4e1417d769c5604937b900e6087461f01e8e042 Mon Sep 17 00:00:00 2001 From: sisidovski Date: Mon, 27 Nov 2023 04:36:32 -0800 Subject: [PATCH] Add baseURL to the URLPattern condition while routing rule registration This behavior change was originally started in https://github.com/whatwg/urlpattern/issues/182, and follows the spec change in https://github.com/whatwg/urlpattern/pull/199. This CL changes the behavior of the router rule registration in the ServiceWorker Static Routing API, especially when the |urlPattern| condition receives URLPatternInit or USVString. Before this CL, the URLPatternInit input was 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, the SW script URL is internally treated as baseURL. After this CL, the URLPatternInit input also internally uses the SW script URL as a baseURL if it's not explicitly provided. This is achieved by the helper method `URLPattern::From()`, which was added in [1]. This change doesn't affect the case when the input is URLPattern, which means the input is the object constructed via `new URLPattern()`. [1] crrev.com/c/5053645 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 7b6a7144dbb6557..67871a2d4fd3fe2 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 dda75f20f4bc4be..523ececf317edee 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 @@ +