简体   繁体   English

缓存 API 在 safari ios 模拟器和 mac 上不起作用

[英]Cache API doesn't work on safari ios simulator and on mac

I am trying to use the following snippet code in my index.html .我正在尝试在我的index.html使用以下代码片段。

<script>
        fetch('index1.js').then(response => {
            caches.open('test-cache').then(cache => {
                cache.put('index1.js', response);
            })
        })
    </script>

I have a custom domain on localhost called test-website.com .我在本地主机上有一个名为test-website.com的自定义域。 When I access this page through chrome, all is good and index1.js gets stored in cache.当我通过 chrome 访问这个页面时,一切都很好, index1.js被存储在缓存中。 Now, I turn on ios Simulator and open safari in there and run the same website again.现在,我打开ios Simulator并在其中打开 safari 并再次运行相同的网站。 I also check this website on mac safari, but In the console, this is what I get:我也在 mac safari 上查看了这个网站,但是在控制台中,这是我得到的:

Unhandled Promise Rejection: ReferenceError: Can't find variable: caches未处理的承诺拒绝:ReferenceError:找不到变量:缓存

Question 1) Why do I get this error?问题 1)为什么会出现此错误?

Question 2) I checked the compatibility: Cache Storage API and Cache .问题 2)我检查了兼容性: Cache Storage APICache The first one has compatibility with ios safari , second one doesn't.第一个与ios safari兼容,第二个没有。 What's the difference between these 2?这2个有什么区别? and could this be the reason why it can't find the cache?这可能是它找不到缓存的原因吗?

Question 3) Apple supports service worker and it should be supporting Cache API .问题 3) Apple 支持 service worker,它应该支持Cache API Otherwise it has no worth .否则没有任何价值。 Any idea?任何的想法?

The documentation for Cache Storage API states the following: 缓存存储 API的文档说明如下:

Note: Chrome and Safari only expose CacheStorage to the windowed context over HTTPS.注意:Chrome 和 Safari 仅通过 HTTPS 向窗口上下文公开CacheStorage window.caches will be undefined unless an SSL certificate is configured.除非配置了 SSL 证书,否则 window.caches 将是未定义的。

Ensure that you are serving and accessing the webpage over HTTPS确保您通过 HTTPS 提供和访问网页

As far as I know, the browser will still allow the caches variable to be accessible when serving pages from localhost as a means to do testing, but this rule should not apply if you're accessing it over a custom domain test-website.com据我所知,当从localhost提供页面作为测试手段时,浏览器仍将允许访问caches变量,但是如果您通过自定义域test-website.com访问它,则此规则不适用

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

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