Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const CACHE_NAME = 'static';
- self.addEventListener('install', event => {
- async function onInstall() {
- const cache = await caches.open(CACHE_NAME);
- await cache.addAll([
- 'images/Dog.jpg'
- ]);
- return cache;
- }
- event.waitUntil(onInstall());
- });
- self.addEventListener('fetch', event => {
- const request = event.request;
- const url = new URL(request.url);
- if (url.pathname.endsWith('Cat.jpg')) {
- event.respondWith(async function () {
- const cache = await caches.open('static');
- url.pathname = 'images/Dog.jpg'; // Dogs are better than cats
- event.request.URL = url;
- const cachedResponse = await cache.match(url);
- if (cachedResponse) {
- return cachedResponse;
- }
- return fetch(event.request);
- }());
- }
- });
Add Comment
Please, Sign In to add comment