-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice_worker.js
165 lines (159 loc) · 2.97 KB
/
service_worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* @fileoverview Defines the service worker to manage app when it's offline.
* @project GitLab - https://obrymec.github.io/gitlab_home_page_clone/
* @author Obrymec - obrymecsprinces@gmail.com
* @supported DESKTOP, MOBILE
* @file service_worker.js
* @created 2023-09-23
* @updated 2023-09-29
* @version 0.0.1
*/
// Attributes.
const catchName = "gitlab-clone-v1";
const staticAssets = [
"./src/home_page/main.js",
"./manifest.json",
"./index.html",
"./style.css",
"./"
];
// Listens `activate` event.
self.addEventListener (
"activate", () => {
// Claims clients.
self.clients.claim ();
}
);
// Listens `install` event.
self.addEventListener (
"install", async () => {
// Creates a cache for
// the project for
// offline case.
const cache = (
await caches.open (
catchName
)
);
// Adds all statics assets
// files path to the
// created cache.
await cache.addAll (
staticAssets
);
// Returns the state.
return self.skipWaiting ();
}
);
// Listens `fetch` event.
self.addEventListener (
"fetch", async event => {
// The request to do.
const req = event.request;
// The request url.
const url = new URL (
req.url
);
// Whether there are
// a match.
if (
url.origin ===
location.origin
) {
// Sends a response.
event.respondWith (
cacheFirst_ (req)
);
// Otherwise.
} else {
// Sends another
// response.
event.respondWith (
networkAndCache_ (req)
);
}
}
);
/**
* @description Checks whether
* our created cache exists.
* @param {Request} req The
* request that is performed.
* @function cacheFirst_
* @private {Function}
* @async
* @returns {
* Response|Promise<Response>
* } Response | Promise
*/
async function cacheFirst_ (
req
) {
// The created cache
// data.
const cache = (
await caches.open (
catchName
)
);
// The cached data.
const cached = (
await cache.match (
req
)
);
// Returns the final
// result that pass
// this test.
return (
cached || fetch (req)
);
}
/**
* @description Returns the data
* defined into the created
* cache.
* @param {Request} req The
* request that is performed.
* @function networkAndCache_
* @private {Function}
* @async
* @returns {Response} Response
*/
async function networkAndCache_ (
req
) {
// The created cache
// data.
const cache = (
await caches.open (
catchName
)
);
// Tries the following
// lines of code.
try {
// The fetched data.
const fresh = (
await fetch (req)
);
// Gets response clone.
await cache.put (
req, fresh.clone ()
);
// Returns the final
// result.
return fresh;
// An error occurred
} catch (exp) {
// The cached data.
const cached = (
await cache.match (
req
)
);
// Returns the final
// result.
return cached;
}
}