fix: enforce service worker cache allowlist
This commit is contained in:
@@ -286,7 +286,12 @@ async function handleRequest(request) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Strategy 1: Cache First (only for essential PWA assets)
|
// Strategy 1: Cache First (only for essential PWA assets)
|
||||||
if (CACHE_FIRST_PATTERNS.some(pattern => pattern.test(url.pathname))) {
|
if (
|
||||||
|
url.origin === self.location.origin &&
|
||||||
|
isCacheableStaticPath(url.pathname) &&
|
||||||
|
!isSensitivePath(url.pathname) &&
|
||||||
|
CACHE_FIRST_PATTERNS.some(pattern => pattern.test(url.pathname))
|
||||||
|
) {
|
||||||
return await cacheFirst(request);
|
return await cacheFirst(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -306,6 +311,7 @@ async function handleRequest(request) {
|
|||||||
|
|
||||||
// Cache First strategy with Response cloning fix
|
// Cache First strategy with Response cloning fix
|
||||||
async function cacheFirst(request) {
|
async function cacheFirst(request) {
|
||||||
|
const url = new URL(request.url);
|
||||||
const cachedResponse = await caches.match(request);
|
const cachedResponse = await caches.match(request);
|
||||||
if (cachedResponse) {
|
if (cachedResponse) {
|
||||||
return cachedResponse;
|
return cachedResponse;
|
||||||
@@ -313,7 +319,13 @@ async function cacheFirst(request) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const networkResponse = await fetch(request);
|
const networkResponse = await fetch(request);
|
||||||
if (networkResponse && networkResponse.ok) {
|
if (
|
||||||
|
networkResponse &&
|
||||||
|
networkResponse.ok &&
|
||||||
|
url.origin === self.location.origin &&
|
||||||
|
isCacheableStaticPath(url.pathname) &&
|
||||||
|
!isSensitivePath(url.pathname)
|
||||||
|
) {
|
||||||
// Clone the response before using it
|
// Clone the response before using it
|
||||||
const responseToCache = networkResponse.clone();
|
const responseToCache = networkResponse.clone();
|
||||||
const cache = await caches.open(STATIC_CACHE);
|
const cache = await caches.open(STATIC_CACHE);
|
||||||
|
|||||||
Reference in New Issue
Block a user