简体   繁体   English

.html 不会从服务人员获取

[英].html won't fetch from service worker

I am struggling with making a simple service worker to work offline.我正在努力让一个简单的服务人员离线工作。 I tried a lot of examples without success.我尝试了很多例子都没有成功。 First I thought it was because I am using a dynamic php based website or maybe it was my server or my htaccess.首先我认为这是因为我使用的是基于动态 php 的网站,或者它可能是我的服务器或我的 htaccess。 It is a mystery for me.这对我来说是个谜。 Everything work on my localhost while fetching the index.html, but on a server the page can't load offline even if everything else is loading while analysing the chrome devtools network tab.获取 index.html 时,一切都在我的本地主机上运行,但在服务器上,即使在分析 chrome devtools 网络选项卡时正在加载其他所有内容,该页面也无法离线加载。

So I am trying to make this simple html offline PWA on my server and I create the same source on another server.所以我试图在我的服务器上制作这个简单的 html 离线 PWA,并在另一台服务器上创建相同的源。

https://sw.punchunique.com https://sw.punchunique.com
https://punchunique.neocities.org/test.html or https://punchunique.neocities.org/test.html
https://punchunique.neocities.org/ https://punchunique.neocities.org/

Maybe because I don't have a fallback.html but why would I need one if it's already loaded in the cache也许是因为我没有后备。html 但是如果它已经加载到缓存中,为什么我需要一个

self.addEventListener('install', function(event) {
    event.waitUntil(
        caches.open('mysite-static-v3').then(function(cache) {
        return cache.addAll([
            'index.html',
            'punch-fixed.css',
            'general.css',
            'punch-homepage.css',


            'thesansextralight_plain-webfont.woff',
            'thesansextralight_plain-webfont.woff2',
            'TweenMax.min.js',
            'PLUGINS.js',
            'punch.webmanifest',

            'sw-demo.js',




            'favicon.ico',
            'favicon-16x16.png',
            'favicon-32x32.png',
            'favicon-194x194.png',
            'apple-touch-icon.png',
            'apple-touch-icon-72x72.png',
            'apple-touch-icon-120x120.png',
            'apple-touch-icon-144x144.png',
            'apple-touch-icon-152x152.png',
            'android-chrome-96x96.png',
            'android-chrome-192x192.png',
            'android-chrome-512x512.png',
            'mstile-48x48.png',
            'mstile-144x144.png',
            'mstile-270x270.png',
            'mstile-558x558.png',
            'mstile-558x270.png',

            'bcg-img-sect1.jpg',
            'green-hook.png',
        ]);
        })
    );
});

self.addEventListener('activate', function(event) {
    event.waitUntil(
    caches.keys().then(function(cacheNames) {
        return Promise.all(
        cacheNames.filter(function(cacheName) {
            // Return true if you want to remove this cache,
            // but remember that caches are shared across
            // the whole origin
        }).map(function(cacheName) {
            return caches.delete(cacheName);
        })
        );
    })
    );
});

self.addEventListener('fetch', function(event) {

    // Cache only
    // If a match isn't found in the cache, the response
    // will look like a connection error
    // event.respondWith(caches.match(event.request));


    // Network only
    // event.respondWith(fetch(event.request));
    // or simply don't call event.respondWith, which
    // will result in default browser behaviour


    // CACHE then NETWORK
    event.respondWith(
    caches.open('mysite-dynamic').then(function(cache) {
        return cache.match(event.request).then(function (response) {
        return response || fetch(event.request).then(function(response) {
            cache.put(event.request, response.clone());
            return response;
        });
        });
    })
    );

});

I have this when offline An unknown error occurred when fetching the script.离线时我有这个 获取脚本时发生未知错误。 and Uncaught (in promise) TypeError: Failed to fetch but All the caches files are present on server and nothing is missing.Uncaught (in promise) TypeError: Failed to fetch but all the caches files are present on server and nothing is missing。

Here is the perfect working example of service worker with static and dynamic caching这是具有 static 和动态缓存的服务工作者的完美工作示例

var CACHE_STATIC_NAME = 'static-v4';
var CACHE_DYNAMIC_NAME = 'dynamic-v2';

self.addEventListener('install', function(event) {
  console.log('[Service Worker] Installing Service Worker ...', event);
  event.waitUntil(
    caches.open(CACHE_STATIC_NAME)
      .then(function(cache) {
        console.log('[Service Worker] Precaching App Shell');
        cache.addAll([
          '/',
          '/index.html',
          '/src/js/app.js',
          '/src/js/feed.js',
          '/src/js/promise.js',
          '/src/js/fetch.js',
          '/src/js/material.min.js',
          '/src/css/app.css',
          '/src/css/feed.css',
          '/src/images/main-image.jpg',
          'https://fonts.googleapis.com/css?family=Roboto:400,700',
          'https://fonts.googleapis.com/icon?family=Material+Icons',
          'https://cdnjs.cloudflare.com/ajax/libs/material-design-lite/1.3.0/material.indigo-pink.min.css'
        ]);
      })
  )
});

self.addEventListener('activate', function(event) {
  console.log('[Service Worker] Activating Service Worker ....', event);
  event.waitUntil(
    caches.keys()
      .then(function(keyList) {
        return Promise.all(keyList.map(function(key) {
          if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME) {
            console.log('[Service Worker] Removing old cache.', key);
            return caches.delete(key);
          }
        }));
      })
  );
  return self.clients.claim();
});

self.addEventListener('fetch', function(event) {
  event.respondWith(
    caches.match(event.request)
      .then(function(response) {
        if (response) {
          return response;
        } else {
          return fetch(event.request)
            .then(function(res) {
              return caches.open(CACHE_DYNAMIC_NAME)
                .then(function(cache) {
                  cache.put(event.request.url, res.clone());
                  return res;
                })
            })
            .catch(function(err) {

            });
        }
      })
  );
});

In fetch Event use match() function with {ignoreVary:true} as second parameter在 fetch 事件中使用 match() function 和 {ignoreVary:true} 作为第二个参数

caches.match(event.request,{ignoreVary:true})
  .then(function(response) {....}

What it does is avoid matching the headers of the request.它所做的是避免匹配请求的标头。 Due to header matching, your application will work on localhost and not in live environment.由于 header 匹配,您的应用程序将在本地主机上运行,而不是在实时环境中运行。

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

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