Skip to content

Commit 762e9f8

Browse files
authored
feat(service-worker): enhance caching and activation logic (#1329)
1 parent e4d6582 commit 762e9f8

16 files changed

Lines changed: 4037 additions & 2133 deletions

public/service-worker.js

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,52 @@
1-
const CACHE_NAME = 'get-requests-cache-v1'
2-
const CACHE_URLS = []
3-
4-
const urlsToFetch = ['open-bus-stride-api', 'fonts.googleapis.com']
1+
const CACHE_NAME = 'get-requests-cache-v2'
2+
const CACHE_URLS = ['open-bus-stride-api', 'fonts.googleapis.com']
53

64
// Install event: cache basic URLs (optional)
75
self.addEventListener('install', (event) => {
86
event.waitUntil(caches.open(CACHE_NAME).then((cache) => cache.addAll(CACHE_URLS)))
7+
self.skipWaiting()
8+
})
9+
10+
// Activate event: clear old caches
11+
self.addEventListener('activate', (event) => {
12+
event.waitUntil(
13+
caches.keys().then((cacheNames) =>
14+
Promise.all(
15+
cacheNames.map((cacheName) => {
16+
if (cacheName !== CACHE_NAME) {
17+
return caches.delete(cacheName)
18+
}
19+
}),
20+
),
21+
),
22+
)
23+
self.clients.claim()
924
})
1025

11-
// Fetch event: cache GET requests
26+
// Fetch event: cache GET requests except when query includes today's date
1227
self.addEventListener('fetch', (event) => {
28+
const today = new Date().toISOString().slice(0, 10) // e.g. "2025-10-28"
29+
1330
if (
1431
event.request.method === 'GET' &&
15-
urlsToFetch.some((url) => event.request.url.includes(url))
32+
!event.request.url.includes(today) &&
33+
CACHE_URLS.some((url) => event.request.url.includes(url))
1634
) {
1735
event.respondWith(
1836
caches.match(event.request).then((cachedResponse) => {
19-
// Return cached response if available, else fetch from network
20-
return (
21-
cachedResponse ||
22-
fetch(event.request).then((response) => {
23-
// Clone the response to save it in the cache
37+
// Return cached response if available
38+
if (cachedResponse) return cachedResponse
39+
40+
// Otherwise, fetch from network and cache the response
41+
return fetch(event.request).then((response) => {
42+
if (response.ok) {
2443
const responseClone = response.clone()
2544
caches.open(CACHE_NAME).then((cache) => {
2645
cache.put(event.request, responseClone)
2746
})
28-
console.log('cached ', event.request)
29-
return response
30-
})
31-
)
47+
}
48+
return response
49+
})
3250
}),
3351
)
3452
}

tests/HAR/clearbutton.har

Lines changed: 2575 additions & 210 deletions
Large diffs are not rendered by default.

tests/HAR/dashboard.har

Lines changed: 107 additions & 201 deletions
Large diffs are not rendered by default.

tests/HAR/missing.har

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,28 @@
33
"version": "1.2",
44
"creator": {
55
"name": "Playwright",
6-
"version": "1.56.1"
6+
"version": "1.55.0"
77
},
88
"browser": {
99
"name": "chromium",
10-
"version": "141.0.7390.37"
10+
"version": "140.0.7339.16"
1111
},
1212
"entries": [
1313
{
14-
"startedDateTime": "2025-10-30T15:51:00.240Z",
15-
"time": 3.674,
14+
"startedDateTime": "2025-11-10T18:10:27.893Z",
15+
"time": 16.209,
1616
"request": {
1717
"method": "GET",
1818
"url": "https://open-bus-stride-api.hasadna.org.il/gtfs_agencies/list?date_from=2024-02-11",
1919
"httpVersion": "HTTP/2.0",
2020
"cookies": [],
2121
"headers": [
22-
{ "name": "Accept", "value": "*/*" },
23-
{ "name": "Accept-Language", "value": "en-US" },
24-
{ "name": "Origin", "value": "http://localhost:3000" },
22+
{ "name": "sec-ch-ua-platform", "value": "\"Windows\"" },
2523
{ "name": "Referer", "value": "http://localhost:3000/" },
26-
{ "name": "User-Agent", "value": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.7390.37 Safari/537.36" },
27-
{ "name": "sec-ch-ua", "value": "\"HeadlessChrome\";v=\"141\", \"Not?A_Brand\";v=\"8\", \"Chromium\";v=\"141\"" },
28-
{ "name": "sec-ch-ua-mobile", "value": "?0" },
29-
{ "name": "sec-ch-ua-platform", "value": "\"Windows\"" }
24+
{ "name": "User-Agent", "value": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.7339.16 Safari/537.36" },
25+
{ "name": "sec-ch-ua", "value": "\"Chromium\";v=\"140\", \"Not=A?Brand\";v=\"24\", \"HeadlessChrome\";v=\"140\"" },
26+
{ "name": "Accept-Language", "value": "en-US" },
27+
{ "name": "sec-ch-ua-mobile", "value": "?0" }
3028
],
3129
"queryString": [
3230
{
@@ -43,11 +41,11 @@
4341
"httpVersion": "HTTP/2.0",
4442
"cookies": [],
4543
"headers": [
44+
{ "name": "strict-transport-security", "value": "max-age=31536000; includeSubDomains" },
4645
{ "name": "access-control-allow-origin", "value": "*" },
4746
{ "name": "content-length", "value": "8145" },
48-
{ "name": "content-type", "value": "application/json" },
49-
{ "name": "date", "value": "Thu, 30 Oct 2025 15:51:00 GMT" },
50-
{ "name": "strict-transport-security", "value": "max-age=31536000; includeSubDomains" }
47+
{ "name": "date", "value": "Mon, 10 Nov 2025 18:10:27 GMT" },
48+
{ "name": "content-type", "value": "application/json" }
5149
],
5250
"content": {
5351
"size": -1,
@@ -59,7 +57,7 @@
5957
"redirectURL": ""
6058
},
6159
"cache": {},
62-
"timings": { "send": -1, "wait": -1, "receive": 3.674 }
60+
"timings": { "send": -1, "wait": -1, "receive": 16.209 }
6361
}
6462
]
6563
}

0 commit comments

Comments
 (0)