oliviertassinari / serviceworker-webpack-plugin

Simplifies creation of a service worker to serve your webpack bundles. :recycle:
https://oliviertassinari.github.io/serviceworker-webpack-plugin/
MIT License
458 stars 76 forks source link

The script has an unsupported MIME type ('text/html') #79

Closed sibelius closed 6 years ago

sibelius commented 6 years ago

I've got this error using webpack 4 and serviceworker-webpack-plugin alpha 2

The script has an unsupported MIME type ('text/html'). Failed to load resource: net::ERR_INSECURE_RESPONSE

{
  "code": "messaging/failed-serviceworker-registration",
  "message": "Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker: The script has an unsupported MIME type ('text/html'). (messaging/failed-serviceworker-registration).",
  "browserErrorMessage": "Failed to register a ServiceWorker: The script has an unsupported MIME type ('text/html')."
}
devCrossNet commented 6 years ago

did you check the response? usually, it's an indicator that you entered a wrong path and the web-server responded with the index.html

sibelius commented 6 years ago

it is not finding my service worker file: http://localhost:7001/firebase-messaging-sw.js

how can I expose it on webpack 4?

devCrossNet commented 6 years ago

if you have a look at the readme here: https://github.com/oliviertassinari/serviceworker-webpack-plugin#api

it is mentioned that the default filename of the service-worker is sw.js so you can either rename your firebase-messaging-sw.js to sw.js or set the filename option to firebase-messaging-sw.js

I hope that helps.

sibelius commented 6 years ago

I've used filename, tks

rahul7p commented 5 years ago

I am facing same issue in vuejs

Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker: The script has an unsupported MIME type ('text/html'). (messaging/failed-serviceworker-registration).

sibelius commented 5 years ago

I recommend using workbox webpack, it solves the same problem with more tools

mend3 commented 5 years ago

I've got this error using webpack 4 and serviceworker-webpack-plugin alpha 2

The script has an unsupported MIME type ('text/html'). Failed to load resource: net::ERR_INSECURE_RESPONSE

{
  "code": "messaging/failed-serviceworker-registration",
  "message": "Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker: The script has an unsupported MIME type ('text/html'). (messaging/failed-serviceworker-registration).",
  "browserErrorMessage": "Failed to register a ServiceWorker: The script has an unsupported MIME type ('text/html')."
}

Got this working by using middleware that works when not in production mode:

app.use(({ url }: Request, res: Response, next: NextFunction) =>
      url.match(/ngsw-worker.js/) && NODE_ENV !== 'production' ? res.status(200) : next())

Just send status OK to response and that's all. Got it working on ng serve|npm start|nodemon|pm2