angular: Service worker registration failed in Safari angular 8

I updated my application from angular v7 to angular v8 and I have some problems with service workers with Safari. I have same error when enter in www.angular.io

[Error] Service worker registration failed with: TypeError: TypeError: Type error run (polyfills-es2015.75c15c263061f321fee9.js:1:1925) (anonymous function) (polyfills-es2015.75c15c263061f321fee9.js:1:12750) runTask (polyfills-es2015.75c15c263061f321fee9.js:1:2544) _ (polyfills-es2015.75c15c263061f321fee9.js:1:8987) promiseReactionJob

Safari Version 12.1.1 (14607.2.6.1.1) MacOs Mojave v10.14.5 Node v10.16.0

My package.json

{ “dependencies”: { “@angular/animations”: “^8.0.1”, “@angular/cdk”: “^8.0.1”, “@angular/common”: “^8.0.1”, “@angular/compiler”: “^8.0.1”, “@angular/core”: “^8.0.1”, “@angular/forms”: “^8.0.1”, “@angular/material”: “^8.0.1”, “@angular/platform-browser”: “^8.0.1”, “@angular/platform-browser-dynamic”: “^8.0.1”, “@angular/platform-server”: “^8.0.1”, “@angular/pwa”: “^0.800.3”, “@angular/router”: “^8.0.1”, “@angular/service-worker”: “^8.0.1”, “@nguniversal/express-engine”: “^8.0.0-rc.1”, “@nguniversal/module-map-ngfactory-loader”: “^8.0.0-rc.1”, “compression”: “^1.7.4”, “tslib”: “^1.10.0”, “rxjs”: “~6.4.0”, “zone.js”: “~0.9.1” }, “devDependencies”: { “@angular-devkit/build-angular”: “^0.800.3”, “@angular/cli”: “^8.0.3”, “@angular/compiler-cli”: “^8.0.1”, “@angular/language-service”: “^8.0.1”, “@types/express-useragent”: “^0.2.21”, “@types/jasmine”: “^3.3.13”, “@types/jasminewd2”: “^2.0.6”, “@types/node”: “^12.0.8”, “codelyzer”: “^5.1.0”, “express”: “^4.17.1”, “jasmine-core”: “~3.4.0”, “jasmine-spec-reporter”: “~4.2.1”, “karma”: “~4.1.0”, “karma-chrome-launcher”: “~2.2.0”, “karma-coverage-istanbul-reporter”: “~2.0.1”, “karma-jasmine”: “~2.0.1”, “karma-jasmine-html-reporter”: “^1.4.0”, “protractor”: “^5.4.2”, “ts-loader”: “^6.0.2”, “ts-node”: “^7.0.1”, “tslint”: “^5.17.0”, “typescript”: “~3.4.5”, “webpack-cli”: “^3.3.4” } }

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 18 (11 by maintainers)

Commits related to this issue

Most upvoted comments

@H--o-l Thanks for your thoughtful reply. Yes you are right. We have found the issue and resolved it and initially it felt like a regression but after doing intense debugging it appears to have been an error occurring due to some library updates on Server Side.

It was indeed a bug that was only showing up in Safari when the ServiceWorker was trying to send a POST request to server and being denied access. Everything was working fine on Chrome and Firefox. Safari was adding Pragma and Cache-control headers with the service worker request which were being denied on the server because they weren’t in the allowed headers list. I cannot seem to understand why we are unable to reproduce this error using Postman. Anyways - Yes, what appeared to have been a regression was NOT actually a regression (but it was indeed quite weird issue to debug)

@kara Service worker using Safari is not working in Angular 8.2.0 anymore. The POST requests are giving Gateway 504 errors.

UPDATE: This is the error i am getting

[Error] Fetch API cannot load MY_API_URL due to access control checks.
[Error] Failed to load resource: Request header field Cache-Control is not allowed by Access-Control-Allow-Headers.

2nd UPDATE: Turns out the error was coming in Safari due to some server side configuration. Its not a regression in Angular 8.2.0

Wow, that’s awesome. Great sleuthing, @H--o-l 👏 It seems indeed to be the case that Safari chokes when the second argument is undefined (or even ''), even though according to MDN:

A USVString representing the base URL to use in case url is a relative URL. If not specified, it defaults to ''.

But it is fine, when the second argument is not provided at all:

new URL('https://angular.io/')  // OK
new URL('https://angular.io/', undefined)  // TypeError

So, I guess we could work-around that by changing Adapter#parseUrl() like this:

 parseUrl(url: string, relativeTo?: string): {origin: string, path: string, search: string} {
-  const parsed = new URL(url, relativeTo);
+  const parsed = !relativeTo ? new URL(url) : new URL(url, relativeTo);
   return {origin: parsed.origin, path: parsed.pathname, search: parsed.search};
 }

@H--o-l, would you like to submit a PR with that fix (after verifying that it does indeed work 😁)?

Could you try (a) to replace / with _ and (2) log more info about the error?

Ok, I change / and _ and log all the /ngsw/state trace :

NGSW Debug Info:

Driver state: SAFE_MODE (Initialization failed due to error: Type error
[native code]
parseUrl@https://toto/ngsw-worker.js:51:35
AssetGroup@https://toto/ngsw-worker.js:360:48
PrefetchAssetGroup
https://toto/ngsw-worker.js:1377:54
map@[native code]
AppVersion@https://toto/ngsw-worker.js:1370:64
https://toto/ngsw-worker.js:2234:63
forEach@[native code]
https://toto/ngsw-worker.js:2229:47
generatorResume@[native code]
fulfilled@https://toto/ngsw-worker.js:1786:66
promiseReactionJob@[native code])
Latest manifest hash: none
Last update check: 29s762u



=== Idle Task Queue ===
Last update tick: 5s245u
Last update run: 14s696u
Task queue:


Debug log:

Can you tell me if https://github.com/angular/angular/issues/31061#issuecomment-503631576 seems like a good lead to you ?

No, I tested it in new project with enabled service workers and I faced same issue. But safari loads both module and nomodule scripts.

bug