Reading time: 2 minutes
Naujas, pagerintas sw.js kodas
Good morning, taip sakant. Ilgai nepildžiau šio tinklaraščio, bet atėjo metas lengvoms permainoms. Šis įrašas skirtas visiems naudojantiems hugo altius temą. Bloga žinia ta, kad jau metas atnaujinti savo sw.js failų kodą į pateiktą apačioje:
const PRECACHE = "pwa-cache-v17";
const RUNTIME = "runtime-17";
const PRECACHE_URLS = [""];
self.addEventListener("install", (event) => {
self.skipWaiting();
event.waitUntil(
caches
.open(PRECACHE)
.then((cache) => cache.addAll(PRECACHE_URLS))
.then(self.skipWaiting())
);
});
self.addEventListener("activate", (event) => {
const currentCaches = [PRECACHE, RUNTIME];
event.waitUntil(
caches
.keys()
.then((cacheNames) => {
return cacheNames.filter(
(cacheName) => !currentCaches.includes(cacheName)
);
})
.then((cachesToDelete) => {
return Promise.all(
cachesToDelete.map((cacheToDelete) => {
return caches.delete(cacheToDelete);
})
);
})
.then(() => self.clients.claim())
);
});
self.addEventListener("fetch", (event) => {
if (event.request.url.startsWith(self.location.origin)) {
event.respondWith(
caches.match(event.request).then((cacheResponse) => {
if (cacheResponse) {
fetch(event.request).then((networkResponse) => {
return caches.open(RUNTIME).then((cache) => {
if (event.request.method !== "POST") {
cache.put(event.request, networkResponse.clone());
}
return networkResponse;
});
});
return cacheResponse;
} else {
return fetch(event.request).then((networkResponse) => {
return caches.open(RUNTIME).then((cache) => {
if (event.request.method !== "POST") {
cache.put(event.request, networkResponse.clone());
}
return networkResponse;
});
});
}
})
);
}
});
Gera žinia ta, jog atnaujinti kodą labai lengva. Ctrl+C ir Ctrl+V.
Kodėl atnaujinti ir dar patiems?
Šis kodas nesuteikia fallbak offline automatinio nukreipimo į nurodytą puslapį pagal nutylėjimą, bet jis automatiškai išsaugo kiekvieną naujai aplankytą puslapį cache atmintyje. Patiems siūlau atnaujinti kodą dėl to, jog šiuo metu neturiu menkiausio supratimo kaip smarkiai modifikavote originaliąją hugoaltius temą (Įprastai naujinį įkelčiau į GitHub). Tiesiog nenoriu netyčia sulaužyti jūsų turimo funkcionalumo.
Kur rasti sw.js failą, kurio kodą reikia pakeisti
Failas yra /static/ direktorijoje.
Visa pateikiama informacija - asmeninė autoriaus nuomonė. Kilus naiškumams rekomenduojama susisiekti elektroniniu paštu: admin@artefaktas.eu
Artefaktas.eu is licensed under CC BY-NC-ND 4.0