yyx990803 / register-service-worker

A script to simplify service worker registration with hooks for common events.
MIT License
638 stars 58 forks source link

misleading error when wrong MIME type returned #21

Closed syonip closed 5 years ago

syonip commented 5 years ago

I created a Vue CLI 3 app with the PWA plugin, and followed instructions to test locally by serving the dist directory with a simple http server. When I used the python's SimpleHTTPServer, I received the error: Error during service worker registration: Error: Service worker not found at ./service-worker.js. This was misleading because the file was indeed served successfully. Had to debug the module to find that the content type wasn't returned as text/javascript by the python server.

I suggest emitting a more precise error in this case. I'm happy to PR.

yyx990803 commented 5 years ago

If the client-side thrown error actually contains information indicating it's about wrong MIME type, then yes. But I doubt it.

syonip commented 5 years ago

I think the culprit is this line: if ( response.status === 404 || response.headers.get('content-type').indexOf('javascript') === -1 ) I saw that indeed response.status is 200, so we can split the conditions and in the second case emit another error. What do you think?

yyx990803 commented 5 years ago

Yeah, that makes sense

syonip commented 5 years ago

Great, I will make a PR soon