。html不会从服务工作者那里获取

问题描述 投票:0回答:1

我正在努力使一个简单的服务人员脱机工作。我尝试了很多例子,但没有成功。首先,我认为这是因为我正在使用基于动态php的网站,或者可能是我的服务器或htaccess。对我来说是个谜。在获取index.html时,所有内容都可以在我的本地主机上运行,​​但是在服务器上,即使在分析chrome devtools网络选项卡时正在加载其他所有内容,该页面也无法脱机加载。

因此,我试图在服务器上创建此简单的html脱机pwa,并在另一台服务器上创建相同的源。

https://sw.punchunique.comhttps://punchunique.neocities.org/test.htmlhttps://punchunique.neocities.org/

也许是因为我没有fallback.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;
        });
        });
    })
    );

});

离线时我有这个提取脚本时发生未知错误。和未捕获(承诺)TypeError:无法获取,但是服务器上存在所有缓存文件,并且没有丢失。谢谢你给我的帮助

service-worker progressive-web-apps
1个回答
0
投票

这是具有静态和动态缓存的服务工作者的完美工作示例

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) {

            });
        }
      })
  );
});
© www.soinside.com 2019 - 2024. All rights reserved.