简体   繁体   English

JS service-worker 和缓存存储 API 和 http 302 - 如何缓存重定向?

[英]JS service-worker and cache storage API with http 302 - how to cache the redirect?

I have a webpage, serverd via https from Tomcat, with serviceworker according to examples for fetching a resource and store it in the cache.我有一个网页,通过 Tomcat 的 https 提供服务,根据示例使用 serviceworker 获取资源并将其存储在缓存中。 If Tomcat is not running, the page is served from the cache storage - so far so good.如果 Tomcat 未运行,则从缓存存储提供页面 - 到目前为止一切顺利。
My Tomcat configuration contains the redirectPort attribute to redirect http to https.我的 Tomcat 配置包含将 http 重定向到 https 的 redirectPort 属性。 The problem I have: When Tomcat is not running and my webpage is accessed via http, browser shows: Connection refused since the http 302 redirect is not stored in the cache.我遇到的问题:当 Tomcat 未运行并且通过 http 访问我的网页时,浏览器显示:连接被拒绝,因为 http 302 重定向未存储在缓存中。 How I could achieve that?我怎么能做到这一点?

Unfortunately service workers require https for safety, so you cannot have a service worker intercept an http request.不幸的是,服务人员需要 https 以确保安全,因此您不能让服务人员拦截 http 请求。

If you would like to force browsers to visit your page in https you could enable HSTS:如果您想强制浏览器访问您在 https 中的页面,您可以启用 HSTS:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security

The HSTS list can be preloaded in the browser allowing it to work offline.可以在浏览器中预加载 HSTS 列表,使其可以脱机工作。 Note, however, you need to be careful when enabling HSTS as if you make a mistake it can be difficult to correct.但是请注意,启用 HSTS 时需要小心,因为如果您犯了一个错误,可能很难纠正。

Alternatively browsers are slowly moving towards loading https by default unless the user explicitly types http.或者,浏览器正在缓慢地默认加载 https,除非用户明确键入 http。 For example:例如:

https://blog.chromium.org/2021/03/a-safer-default-for-navigation-https.html https://blog.chromium.org/2021/03/a-safer-default-for-navigation-https.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM