【问题标题】:JS service-worker and cache storage API with http 302 - how to cache the redirect?带有 http 302 的 JS 服务工作者和缓存存储 API - 如何缓存重定向?
【发布时间】:2021-07-09 00:17:15
【问题描述】:

我有一个网页,通过 Tomcat 的 https 提供服务,根据示例使用 serviceworker 获取资源并将其存储在缓存中。如果 Tomcat 未运行,则从缓存存储中提供页面 - 到目前为止一切顺利。
我的 Tomcat 配置包含将 http 重定向到 https 的 redirectPort 属性。我遇到的问题:当 Tomcat 未运行并且通过 http 访问我的网页时,浏览器显示:Connection denied,因为 http 302 重定向未存储在缓存中。我怎样才能做到这一点?

【问题讨论】:

    标签: javascript service-worker cachestorage


    【解决方案1】:

    很遗憾,Service Worker 需要 https 以确保安全,因此您不能让 Service Worker 拦截 http 请求。

    如果您想强制浏览器以 https 访问您的页面,您可以启用 HSTS:

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

    可以在浏览器中预加载 HSTS 列表,使其离线工作。但是请注意,启用 HSTS 时需要小心,因为如果您犯了一个错误,可能很难纠正。

    除非用户明确键入 http,否则浏览器会慢慢地默认加载 https。例如:

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

    【讨论】:

    • HSTS 很好,谢谢。我忘记了 service worker 需要 https(127.0.0.1/localhost 除外)。
    猜你喜欢
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    • 2020-03-11
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    相关资源
    最近更新 更多