Skip to content

Commit a5dd4ed

Browse files
H--o-lkara
authored andcommitted
fix(service-worker): registration failed on Safari (#31140)
Since Angular v8, and commit b3dda0e, `parseUrl()` can be called without `relativeTo`, thus `new URL()` can be called with `relativeTo = undefined`. Safari does not like it and the service worker registration fails: ```js new URL('https://angular.io/') // OK new URL('https://angular.io/', undefined) // TypeError ``` Closes #31061 PR Close #31140
1 parent e83667a commit a5dd4ed

2 files changed

Lines changed: 4 additions & 2 deletions

File tree

‎packages/service-worker/worker/src/adapter.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ export class Adapter {
5353
* Extract the pathname of a URL.
5454
*/
5555
parseUrl(url: string, relativeTo?: string): {origin: string, path: string, search: string} {
56-
const parsed = new URL(url, relativeTo);
56+
// Workaround a Safari bug, see
57+
// https://github.com/angular/angular/issues/31061#issuecomment-503637978
58+
const parsed = !relativeTo ? new URL(url) : new URL(url, relativeTo);
5759
return {origin: parsed.origin, path: parsed.pathname, search: parsed.search};
5860
}
5961

‎packages/service-worker/worker/testing/scope.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export class SwTestHarness implements ServiceWorkerGlobalScope, Adapter, Context
186186

187187
parseUrl(url: string, relativeTo?: string): {origin: string, path: string, search: string} {
188188
const parsedUrl: URL = (typeof URL === 'function') ?
189-
new URL(url, relativeTo) :
189+
(!relativeTo ? new URL(url) : new URL(url, relativeTo)) :
190190
require('url').parse(require('url').resolve(relativeTo || '', url));
191191

192192
return {

0 commit comments

Comments
 (0)