DockYard / ember-service-worker

A pluggable approach to Service Workers for Ember.js
http://ember-service-worker.com
MIT License
236 stars 63 forks source link

Problem with service worker registration #154

Closed pablogravielseo closed 4 years ago

pablogravielseo commented 5 years ago

Hello when I use the service worker locally works normally, but when the app is deployed I get this error message on the console:

Service Worker registration failed with TypeError: Failed to register a ServiceWorker: ServiceWorker script evaluation failed

Someone had faced something similar??

I'm using:

ember-cli: 3.4.4
node: 8.9.0

Ember service worker and plugins version:

"ember-service-worker": "^0.7.2",
"ember-service-worker-asset-cache": "^0.6.4",
"ember-service-worker-cache-fallback": "^0.6.2",
"ember-service-worker-index": "^0.7.0",
"ember-service-worker-update-notify": "^1.0.2",
eshtadc commented 5 years ago

Do you have any custom service worker code? This error would indicate a runtime error at registration. Also - what browser?

pablogravielseo commented 5 years ago

Hi @eshtadc yes I have, I use Opera and Chrome.

pablogravielseo commented 5 years ago

In development environment I've got the correct file of sw.js, but in staging and production environment I don't know why I'm getting the content of "index.html" file from the sw.js file

eshtadc commented 5 years ago

This seems to be what is causing the JS error and therefore the runtime error. I verified at your login page.

Maybe it is the way things are deployed. Your other JS assets appear to be coming from cloudfront.net rather than the herokuapp where the sw.js is trying to load from. Where is your sw.js file?

pablogravielseo commented 5 years ago

@eshtadc yesterday I realised about this, I'm using "ember-cli-deploy" I'm trying to figuring out where is the sw.js in staging environment. A question, the addon generates dynamically a sw.js file in the current root path right? There is a way to manually change the path?

pablogravielseo commented 5 years ago

@eshtadc my co worker found the file, is on S3 now we are finding a way to set the correct path.

eshtadc commented 5 years ago

Glad you found it. Keep in mind that afaik service workers must be on the same origin. https://w3c.github.io/ServiceWorker/#origin-relativity https://github.com/DockYard/ember-service-worker/issues/97

pablogravielseo commented 5 years ago

@eshtadc sorry, is it possible to remove the links that I shared from you last message? I didn't know that I needed to maintain privately.

eshtadc commented 5 years ago

Message updated :)

pablogravielseo commented 5 years ago

@eshtadc thank you! :)

pablogravielseo commented 5 years ago

@eshtadc is possible: https://github.com/DockYard/ember-service-worker/issues/154#issuecomment-518654969?

eshtadc commented 5 years ago

Yes - it is possible to load from a different path on the same domain. You can pass rootUrl in your configuration. For example in ember-cli-build.js if you are planning to have your sw.js in a myrootfolder directory:

var app = new EmberApp(defaults, {
    'ember-service-worker': {
      rootUrl: ''/myrootfolder/"
    }
  });

Does this help?

pablogravielseo commented 5 years ago

yes helps thank you, and it's possible to set the path of the sw.js file to decide where will be served? or just doing the configuration you had mentioned above the app will serve in that path?

eshtadc commented 5 years ago

ember-service-worker currently always generates the file in the root in order to allow access to the full project. See https://developers.google.com/web/fundamentals/primers/service-workers/#register_a_service_worker

I believe the case that the rootUrl configuration above was created for was someone who had a deployment system that moved files around during deployment and therefore needed the registration to load from a custom location. It's a non-standard option (and that's why its left out of the docs).

eshtadc commented 4 years ago

I'm closing this issue due to inactivity but please comment if you feel that's not correct and we can re-open and continue the conversation.