Skip to content

Commit

Permalink
fix(service-worker): registration failed on Safari (#31140)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
H--o-l authored and kara committed Jun 24, 2019
1 parent e83667a commit a5dd4ed
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/service-worker/worker/src/adapter.ts
Expand Up @@ -53,7 +53,9 @@ export class Adapter {
* Extract the pathname of a URL.
*/
parseUrl(url: string, relativeTo?: string): {origin: string, path: string, search: string} {
const parsed = new URL(url, relativeTo);
// Workaround a Safari bug, see
// https://github.com/angular/angular/issues/31061#issuecomment-503637978
const parsed = !relativeTo ? new URL(url) : new URL(url, relativeTo);
return {origin: parsed.origin, path: parsed.pathname, search: parsed.search};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/service-worker/worker/testing/scope.ts
Expand Up @@ -186,7 +186,7 @@ export class SwTestHarness implements ServiceWorkerGlobalScope, Adapter, Context

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

return {
Expand Down

0 comments on commit a5dd4ed

Please sign in to comment.