0
私はindex.html
ファイルにアプリケーションシェルを持っています。 /api/
(AJAX呼び出し)で始まるURLを除くすべてのURLにこのアプリケーションシェルをロードします。これを達成するために私の設定をどのようにして設定しますか?あなたがやりたいだろう何すべてのページに同じHTMLを読み込みます
service-worker.js:
(function() {
'use strict';
var filesToCache = [
'.',
'index.html',
'pwa-stylesheets/css/styles-new.css'
];
var staticCacheName = 'cache-v3';
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(staticCacheName)
.then(function(cache) {
return cache.addAll(filesToCache);
}).then(function() {
self.skipWaiting();
})
);
});
self.addEventListener('fetch', function(event) {
var url = event.request.url;
if(url.indexOf("http://localhost:8080") > -1
&& url.indexOf("http://localhost:8080/api/") == -1
&& url.indexOf("http://localhost:8080/pwa-") == -1) {
// event.request.url = "http://localhost:8080/"; // not allowed
event.respondWith(
caches.match(event.request).then(function(response) {
if(response) {
console.log('Found ', url, ' in cache');
return response;
}
console.log('Network request for ', url);
return fetch(event.request).then(function(response) {
if(! response.ok) {
console.log("Network request failed.");
return null;
}
return caches.open(staticCacheName).then(function(cache) {
cache.put(event.request.url, response.clone());
return response;
});
});
})
);
}
});
self.addEventListener('activate', function(event) {
var cacheWhitelist = [staticCacheName];
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.map(function(cacheName) {
if(cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
);
})
);
});
})();
ええ、すべてのapiリクエストにフィルタを追加できますが、問題はありません。しかし、cache apiはevent.requestオブジェクトで動作するgetメソッドしかありません。そして、どうにかして/、/ link1、/ link2を同じindex.htmlファイルにマッピングしなければなりません。 –
これらの要求URLをキャッチし、サーバーワーカーで独自の応答オブジェクトを作成する必要があります。これは、フォールバック応答ロジックを持つことに似ています。 一般的なフォールバック戦略をチェックしてください - https://jakearchibald.com/2014/offline-cookbook/#generic-fallback 必要に応じて少し変更する必要があります。 –