1- {
2- "name": "Laravel PWA APP",
3- "short_name": "PWA",
4- "start_url": "/index.php",
5- "background_color": "#6777ef",
6- "description": "Laravel PWA APP",
7- "display": "fullscreen",
8- "theme_color": "#6777ef",
9- "icons": [
10- {
11- "src": "logo.PNG",
12- "sizes": "512x512",
13- "type": "image/png",
14- "purpose": "any maskable"
15- }
16- ]
17- }
1+ const preLoad = function () {
2+ return caches.open("offline").then(function (cache) {
3+ // caching index and important routes
4+ return cache.addAll(filesToCache);
5+ });
6+ };
7+
8+ self.addEventListener("install", function (event) {
9+ event.waitUntil(preLoad());
10+ });
11+
12+ const filesToCache = [
13+ '/',
14+ '/offline.html'
15+ ];
16+
17+ const checkResponse = function (request) {
18+ return new Promise(function (fulfill, reject) {
19+ fetch(request).then(function (response) {
20+ if (response.status !== 404) {
21+ fulfill(response);
22+ } else {
23+ reject();
24+ }
25+ }, reject);
26+ });
27+ };
28+
29+ const addToCache = function (request) {
30+ return caches.open("offline").then(function (cache) {
31+ return fetch(request).then(function (response) {
32+ return cache.put(request, response);
33+ });
34+ });
35+ };
36+
37+ const returnFromCache = function (request) {
38+ return caches.open("offline").then(function (cache) {
39+ return cache.match(request).then(function (matching) {
40+ if (!matching || matching.status === 404) {
41+ return cache.match("offline.html");
42+ } else {
43+ return matching;
44+ }
45+ });
46+ });
47+ };
48+
49+ self.addEventListener("fetch", function (event) {
50+ event.respondWith(checkResponse(event.request).catch(function () {
51+ return returnFromCache(event.request);
52+ }));
53+ if(!event.request.url.startsWith('http')){
54+ event.waitUntil(addToCache(event.request));
55+ }
56+ });
0 commit comments