Skip to content

Commit 674543e

Browse files
Done !!
1 parent 2621aa5 commit 674543e

File tree

5 files changed

+120
-209
lines changed

5 files changed

+120
-209
lines changed

public/polyfill.js

Lines changed: 0 additions & 107 deletions
This file was deleted.

public/sw.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
var CURRENT_CACHES = ["/"]
2+
3+
const self = this
4+
5+
self.addEventListener("activate", (event) => {
6+
// Delete all caches that aren't named in CURRENT_CACHES.
7+
// While there is only one cache in this example, the same logic will handle the case where
8+
// there are multiple versioned caches.
9+
var expectedCacheNamesSet = new Set(Object.values(CURRENT_CACHES))
10+
event.waitUntil(
11+
caches.keys().then((cacheNames) => {
12+
return Promise.all(
13+
cacheNames.map((cacheName) => {
14+
if (!expectedCacheNamesSet.has(cacheName)) {
15+
// If this cache name isn't present in the set of "expected" cache names, then delete it.
16+
console.log("Deleting out of date cache:", cacheName)
17+
return caches.delete(cacheName)
18+
}
19+
})
20+
)
21+
})
22+
)
23+
})
24+
25+
self.addEventListener("fetch", (event) => {
26+
console.log("Handling fetch event for", event.request.url)
27+
28+
event.respondWith(
29+
caches.open(CURRENT_CACHES).then((cache) => {
30+
return cache
31+
.match(event.request)
32+
.then((response) => {
33+
if (response) {
34+
// If there is an entry in the cache for event.request, then response will be defined
35+
// and we can just return it. Note that in this example, only font resources are cached.
36+
console.log(" Found response in cache:", response)
37+
38+
return response
39+
}
40+
41+
// Otherwise, if there is no entry in the cache for event.request, response will be
42+
// undefined, and we need to fetch() the resource.
43+
console.log(
44+
" No response for %s found in cache. About to fetch " +
45+
"from network...",
46+
event.request.url
47+
)
48+
49+
// We call .clone() on the request since we might use it in a call to cache.put() later on.
50+
// Both fetch() and cache.put() "consume" the request, so we need to make a copy.
51+
// (see https://developer.mozilla.org/en-US/docs/Web/API/Request/clone)
52+
return fetch(event.request.clone()).then((response) => {
53+
console.log(
54+
" Response for %s from network is: %O",
55+
event.request.url,
56+
response
57+
)
58+
59+
if (
60+
response.status < 400 &&
61+
response.headers.has("content-type") &&
62+
response.headers.get("content-type").match(event.request)
63+
) {
64+
// This avoids caching responses that we know are errors (i.e. HTTP status code of 4xx or 5xx).
65+
// We also only want to cache responses that correspond to fonts,
66+
// i.e. have a Content-Type response header that starts with "font/".
67+
// Note that for opaque filtered responses (https://fetch.spec.whatwg.org/#concept-filtered-response-opaque)
68+
// we can't access to the response headers, so this check will always fail and the font won't be cached.
69+
// All of the Google Web Fonts are served off of a domain that supports CORS, so that isn't an issue here.
70+
// It is something to keep in mind if you're attempting to cache other resources from a cross-origin
71+
// domain that doesn't support CORS, though!
72+
// We call .clone() on the response to save a copy of it to the cache. By doing so, we get to keep
73+
// the original response object which we will return back to the controlled page.
74+
// (see https://developer.mozilla.org/en-US/docs/Web/API/Request/clone)
75+
console.log(" Caching the response to", event.request.url)
76+
cache.put(event.request, response.clone())
77+
} else {
78+
console.log(" Not caching the response to", event.request.url)
79+
}
80+
81+
// Return the original response object, which will be used to fulfill the resource request.
82+
return response
83+
})
84+
})
85+
.catch((error) => {
86+
// This catch() will handle exceptions that arise from the match() or fetch() operations.
87+
// Note that a HTTP error response (e.g. 404) will NOT trigger an exception.
88+
// It will return a normal response object that has the appropriate error code set.
89+
console.error(" Error in fetch handler:", error)
90+
91+
throw error
92+
})
93+
})
94+
)
95+
})

public/sw3.js

Lines changed: 0 additions & 100 deletions
This file was deleted.

src/components/Content.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const Content = () => {
2424

2525
useEffect(() => {
2626
const fetchData = async () => {
27-
// const dummyResponse = await fetch("https://opentdb.com/api.php?amount=5&category=9&difficulty=easy&type=multiple")
2827
if (sendRequest) {
2928
console.log("getting data ...")
3029
setGettingData(true)

src/swDev.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
11
export const register = () => {
22
if ("serviceWorker" in navigator) {
33
window.addEventListener("load", function () {
4-
navigator.serviceWorker.register("/sw3.js").then(
4+
navigator.serviceWorker.register("/sw.js").then(
55
function (registration) {
66
// Registration was successful
77
console.log(
88
"ServiceWorker registration successful with scope: ",
99
registration.scope
1010
)
11+
12+
// Caching all API Data
13+
let difficulties = ["easy", "medium", "hard"]
14+
let amounts = [5, 10, 15]
15+
let categories = [9, 11, 17, 18, 19, 21]
16+
17+
// loop
18+
categories.map((eachCategory) => {
19+
difficulties.map((eachDifficulty) => {
20+
amounts.map(async (eachAmount) => {
21+
await fetch(
22+
`https://opentdb.com/api.php?amount=${eachAmount}&category=${eachCategory}&difficulty=${eachDifficulty}&type=multiple`
23+
)
24+
.then((success) => {
25+
console.log("success")
26+
})
27+
.catch((err) => {
28+
console.log("error")
29+
})
30+
})
31+
})
32+
})
33+
34+
//
1135
},
1236
function (err) {
1337
// registration failed :(

0 commit comments

Comments
 (0)