vite-plugin-pwa icon indicating copy to clipboard operation
vite-plugin-pwa copied to clipboard

White page in production : the server responded with a MIME type of "text/html"

Open emmanuelgeoffray opened this issue 2 years ago • 8 comments

I have faced this issue in pre-production: users have a white page and an error in console Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

Screenshot from 2023-02-07 10-28-41

I think this issue may take different form, depending on server response. https://github.com/vite-pwa/vite-plugin-pwa/issues/457 may be related.

To reproduce:

  1. clear the browser cache on the index.88888.js file Screenshot from 2023-02-07 10-16-37

  2. do a commit/push/deploy

  3. return to the page

Explanation

What happens in the browser to generate this error:

  1. When the url is first loaded, the service worker is installed, and the files served are put in the service worker cache to be available offline: index.html, index.8888.js, *.css, etc.
  2. The problem is that the index.8888.js is not actually put in the service worker cache. At the app build, there is a warning.

Screenshot from 2023-02-07 10-19-46

  1. So the index.8888.js file is in the "normal" cache of the browser, which can decide at any time to clean up.
  2. The user has not been on the site for a few days and the browser has cleaned up.
  3. A new version is deployed, so the hashes change, the index.8888.js file doesn't exist anymore, it is replaced by an index.9999.js file which is correctly imported by the new index.html.
  4. The user connects to the site, he still has the old index.html which is in the cache of the service worker. This old index.html imports index.8888.js which does not exist anymore on the server, which will return a 404 or a redirect according to the server configuration, generally in the form of an html page.
  5. This triggers a browser error, it does not allow html to be imported in the form of a script, it requires Javascript.

Screenshot from 2023-02-07 10-26-56

The user ends up with a blank page To fix the problem, the user can do a clear refresh (Ctrl+Shift+R) to load the new index.html which calls index.9999.js which exists on the server.

Solution

To fix this on our side, we need to change the configuration so that index.9999.js and the following are always stored in the service worker cache.

Increase the size of maximumFileSizeToCacheInBytes to 3 MiB so that it allows caching of this file, according to the information in the doc.

Suggestion

This bug can be very painful in production with users facing a 100% broken website. This bug can be silent when doing tests and QA. Once it is live, there is no way to fix it. The user is facing a white page and we can't tell him to clear refresh.

For me, I use netlify to build and deploy, and I did not check the warning in the build logs on netlify's website.

I'd suggest that this warning should be changed to an error, this would explicitly stop the build process, requiring developer to fix.

emmanuelgeoffray avatar Feb 07 '23 09:02 emmanuelgeoffray

@emmanuelgeoffray

You must deal with this, you have 2 solutions:

  • change your build pipeline to keep also on server old assets and remove clear old cached assets from the sw (beware with quota error) or
  • keep current behavior and assume the user can do a hard refresh on browser

There is nothing this plugin can do to resolve this issue, the decision is up to you.

EDIT: you can also switch to custom service worker and use NetworkFirst strategy instead using StaleWhileRevalidate strategy.

userquin avatar Feb 07 '23 10:02 userquin

@emmanuelgeoffray you should check the cache headers you're using, just allow browser use it: https://vite-pwa-org.netlify.app/deployment/#cache-control

userquin avatar Feb 07 '23 10:02 userquin

I use the exact netlify.toml config mentioned in the doc https://vite-pwa-org.netlify.app/deployment/netlify.html#basic-deployment-example

emmanuelgeoffray avatar Feb 07 '23 10:02 emmanuelgeoffray

@emmanuelgeoffray upps,sorry man, the problem was about maximumFileSizeToCacheInBytes, you should check your app is working properly, on build you should see workbox-build warnings.

userquin avatar Feb 07 '23 10:02 userquin

I'd suggest that this warning should be changed to an error, this would explicitly stop the build process, requiring developer to fix

I'd agree with this change. I saw my users run into the same issue.

andr-ec avatar Mar 03 '23 22:03 andr-ec

@andr-ec we cannot change it, it is workbox internal, I'll check if we can detect it and throw an error if some variable enable (by default will be enabled, will throw if maximumFileSizeToCacheInBytes warning found, and so, anyone can turn off)

userquin avatar Mar 03 '23 22:03 userquin

I'd suggest that this warning should be changed to an error, this would explicitly stop the build process, requiring developer to fix.

First of all, thank you for your detailed description of what happens here 🙏 I was getting closer to the same conclusion, but this confirms my suspicions.

I'd agree with this change too. If this had thrown an error I would easily catch it and either reduce bundle size or increase the maximumFileSizeToCacheInBytes setting.

This is what the warning looks like in the production build log:
image

This should at least be a big fat warning box in the vite-pwa documentation. It's not apparent to me that an asset not being preached will result in a "white screen of 404 update issue" in some cases where the browser cache is cleared between updates.

thomastvedt avatar Aug 18 '23 07:08 thomastvedt

i saw this warning but i did nothing for it, i released version and now it's in production,and now i want to release new version , if i release new version i, my users will same the white screen because i face white screen in evaluation , how can i fix this problem and release new version? @emmanuelgeoffray

erfanezk avatar May 29 '24 12:05 erfanezk