Since Service Worker registration requires website to be served over HTTPS, Google Chrome throws an error when I try to add HTTP resource to Cache:
Mixed Content: The page at 'https://example.com/sw.js' was loaded over HTTPS, but requested an insecure resource 'http://example.com/'. This request has been blocked; the content must be served over HTTPS.
I want to cache main HTTP page (or at least 301 redirection to HTTPS) because for users it's difficult to type https://
part, especially on mobile devices, to get access to cached version of my website, when offline.
Are there any workarounds to achieve this?
I don't think this problem can be solved via a service worker because there is no way to intercept requests to the http scheme. However, it looks like the Strict-Transport-Security
header could work.
If provided on a https
response, it forces the browser to always use the https
scheme to load the page--which should hit the service worker, if it's been correctly installed. This applies both in the case where the user does not type in a scheme (e.g. example.com
) or they type in the http
scheme (e.g. http://example.com
).