Skip to content

Instantly share code, notes, and snippets.

@goofmint

goofmint/index.js

Created Jun 19, 2018
Embed
What would you like to do?
self.addEventListener('fetch', e => {
// 外部リソース取得時
const url = new URL(e.request.url);
// オフラインの時にはfail.svgを表示する
if (url.origin == location.origin && url.pathname == '/ok.svg' && !navigator.onLine) {
return e.respondWith(caches.match('/fail.svg'));
}
// それ以外の場合は同じ
e.respondWith(
caches.match(e.request, {
ignoreSearch: true
})
.then(response => {
return response || fetch(e.request);
})
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment