简体   繁体   English

如何使用 Service Workers 和 Cache Api 从基本 url 离线加载网站?

[英]How to load website offline from base url using Service Workers and Cache Api?

When you visit this web site https://bugs.stringmanolo.ga/index.html , while navigating around the main.js file is calling a method from ff.js file to cache a good amount of the resources.当您访问此网站https://bugs.stringmanolo.ga/index.html 时,在浏览 main.js 文件时会调用 ff.js 文件中的方法来缓存大量资源。 So next time you land to the web the files are directly taken from your browser cache without make any request.因此,下次您登陆 Web 时,文件会直接从浏览器缓存中获取,而无需提出任何请求。

This means if you visited my website before, you can load it again without internet conection.这意味着如果您之前访问过我的网站,您可以在没有互联网连接的情况下再次加载它。

The problem is, this only works if you directly time the index.html file in the addressbar.问题是,这仅在您直接对地址栏中的 index.html 文件计时时才有效。 Lame.瘸。

Thid url dosn't work loaded offline.这个网址在离线加载时不起作用。 https://bugs.stringmanolo.ga https://bugs.stringmanolo.ga

This other one works fine.另一个工作正常。 https://bugs.stringmanolo.ga/index.html https://bugs.stringmanolo.ga/index.html

How can i make the web cache also loads the index.html when the base url is requested?当请求基本 url 时,如何使 Web 缓存也加载 index.html?

In your cache array list add a root entry.在您的缓存数组列表中添加一个根条目。 That's it.就是这样。

var CACHELIST = [
    "/",
    // rest of resource list
];

self.addEventListener("install", function (event) {
    console.log("Installing the Service Worker!");
    caches.open(CACHENAME)
        .then(cache => {
            cache.addAll(CACHELIST);
        });
});

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

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