Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CacheStaticName data returning empty in fetch event #9

Open
imamhsn195 opened this issue Sep 25, 2020 · 0 comments
Open

CacheStaticName data returning empty in fetch event #9

imamhsn195 opened this issue Sep 25, 2020 · 0 comments

Comments

@imamhsn195
Copy link

This is my sw.js

const staticDevCoffee = "dev-coffee-site-v1";
const dynamicCacheName = "dev-coffee-dynamic-v4";
const assets = [
    "/",
    "/index.html",
    "/fallback.html",
    "/about.html",
    "/contact.html",
    "/blog.html",
    "/css/style.css",
    "/js/app.js",
    "/images/coffee1.jpg",
    "/images/coffee2.jpg",
    "/images/coffee3.jpg",
    "/images/coffee4.jpg",
    "/images/coffee5.jpg",
    "/images/coffee6.jpg",
    "/images/coffee7.jpg",
    "/images/coffee8.jpg",
    "/images/coffee9.jpg",
]
self.addEventListener("install", installEvent => {
    installEvent.waitUntil(
        caches.open(staticDevCoffee).then(cache => {
            cache.addAll(assets)
        })
    );
});
self.addEventListener('activate', activateEvent => {
    activateEvent.waitUntil(
        caches.keys().then(keys => {
         return Promise.all(keys
                .filter(key => key !== staticDevCoffee && key !== dynamicCacheName)
                .map(key => caches.delete(key))
            );
        })
    );
});

self.addEventListener('fetch', fetchEvent => {
    if(!(fetchEvent.request.url.indexOf("http") === 0)) return
    fetchEvent.respondWith(
        caches.match(fetchEvent.request).then(cacheRes => {
            return cacheRes || fetch(fetchEvent.request).then(fetchRes => {
                return caches.open(staticDevCoffee).then(cache => {
                    cache.put(fetchEvent.request.url, fetchRes.clone());
                    return fetchRes;
                })
            });
        }).catch(()=> caches.match("/fallback.html"))
    );
});

After adding cache delete method in fetch method sw returning staticCacheName but empty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant