summaryrefslogtreecommitdiff
path: root/src/scripts/service-worker.js
blob: 19f58b8d342a41ce827ea245a5e1db92dd3a5545 (plain)
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
const staticQuran = "quran-all";
const assets = [
    "../../",
    "../../index.html",
    "../src/index.html",
    "../src/sura.html",
    "../src/attribution.html",
    "../src/styles/style.css",
    "../src/styles/css/fontawesome.css",
    "../src/styles/css/brands.css",
    "../src/styles/css/solid.css",
    "../src/styles/webfonts/fa-brands-400.ttf",
    "../src/styles/webfonts/fa-brands-400.woff2",
    "../src/styles/webfonts/fa-solid-900.ttf",
    "../src/styles/webfonts/fa-solid-900.woff2",
    "../src/styles/css/solid.css",
    "../src/data/en.sahih.xml",
    "../src/data/id.indonesian.xml",
    "../src/data/quran-data.xml",
    "../src/data/quran-uthmani.xml",
    "../src/scripts/main.js",
    "../src/scripts/quran.js",
    "../src/scripts/sura-list.js",
];
/*
self.addEventListener("install", installEvent => {
    installEvent.waitUntil(
        caches.open(staticQuran).then(cache => {
            for (var i = 0; i < assets.length; i++) {
                cache.add(assets[i]);
            }
        })
    )
})

self.addEventListener("fetch", fetchEvent => {
    fetchEvent.respondWith(
        caches.match(fetchEvent.request).then(res => {
            return res || fetch(fetchEvent.request);
        })
    )
})
*/

self.addEventListener('install', function (event) {
    console.log('Service worker installed');

    event.waitUntil(
        caches.open('staticQuran')
            .then(function (cache) {
                console.log('Cache opened');
                return cache.addAll(assets);
            })
            .then(function () {
                console.log('Assets cached');
            })
            .catch(function (error) {
                console.error('Error caching assets:', error);
            })
    );
});

self.addEventListener('fetch', function (event) {
    console.log('Fetch event:', event.request.url);

    event.respondWith(
        caches.match(event.request)
            .then(function (response) {
                if (response) {
                    console.log('Cached response found:', response);
                    return response;
                }

                console.log('No cached response found, fetching from network');
                return fetch(event.request);
            })
            .catch(function (error) {
                console.error('Error fetching from cache:', error);
            })
    );
});