PageSourceSearch

https://www.comptoirdelor.be/wp-content/themes/goudwisselkantoor/f…/sw.js?v=1790925496-1791493811

js comptoirdelor.be collected 2026-10-08 21:10:47 UTC 6,885 bytes, 153 lines download raw bytes

1// ---------------------------------------------------------------------------
2// CONFIGURATIE
3// ---------------------------------------------------------------------------
4
5// Haal versie op uit de URL (die we via PHP meegaven)
6const params = new URL(location.href).searchParams;
7const VERSION = params.get('v') || '1.0.0';
8
9const CACHE_NAMES = {
10    static: `gwk-static-${VERSION}`,
11    api: `gwk-api-${VERSION}`,
12    pages: `gwk-pages-offline` // Aparte cache voor HTML pagina's
13};
14
15// Bestanden die ALTIJD direct beschikbaar moeten zijn (Precache)
16const OFFLINE_URL = '/offline.html'; // Zorg dat je deze pagina maakt!
17const PRECACHE_ASSETS = [
18    OFFLINE_URL,
19    '/wp-content/themes/goudwisselkantoor/ferrari/gwk-styleguide/css/grfx/arrow-right-gold.svg',
20    // Voeg hier je belangrijkste fonts/logo's toe
21];
22
23// ---------------------------------------------------------------------------
24// 1. INSTALLATIE (Pre-caching)
25// ---------------------------------------------------------------------------
26self.addEventListener('install', (event) => {
27    // Forceer de nieuwe worker om direct actief te worden
28    self.skipWaiting();
29
30    event.waitUntil(
31        caches.open(CACHE_NAMES.static).then((cache) => {
32            return cache.addAll(PRECACHE_ASSETS);
33        })
34    );
35});
36
37// ---------------------------------------------------------------------------
38// 2. ACTIVATIE (Oude caches opruimen)
39// ---------------------------------------------------------------------------
40self.addEventListener('activate', (event) => {
41    // Zorg dat de worker direct controle neemt over openstaande pagina's
42    event.waitUntil(clients.claim());
43
44    // Verwijder oude caches die niet matchen met de huidige VERSION
45    event.waitUntil(
46        caches.keys().then((keys) => {
47            return Promise.all(
48                keys.map((key) => {
49                    // Als cache naam start met 'gwk-' maar niet de huidige versie is: weg ermee
50                    if (key.startsWith('gwk-') && 
51                        key !== CACHE_NAMES.static && 
52                        key !== CACHE_NAMES.api && 
53                        key !== CACHE_NAMES.pages) {
54                        return caches.delete(key);
55                    }
56                })
57            );
58        })
59    );
60});
61
62// ---------------------------------------------------------------------------
63// 3. FETCH STRATEGIES (Het echte werk)
64// ---------------------------------------------------------------------------
65self.addEventListener('fetch', (event) => {
66    const url = new URL(event.request.url);
67
68    // -----------------------------------------------------------------------
69    // A. AJAX BRIDGE STRATEGIE: Stale-While-Revalidate
70    // -----------------------------------------------------------------------
71    // Specifiek voor jouw get_coin_data en get_chart_data calls
72    if (url.pathname.includes('admin-ajax.php') || url.pathname.includes('ajax-bridge.php')) {
73        if (url.searchParams.get('action') === 'get_coin_data' || 
74            url.searchParams.get('action') === 'get_chart_data') {
75            
76            event.respondWith(
77                caches.open(CACHE_NAMES.api).then((cache) => {
78                    return cache.match(event.request).then((cachedResponse) => {
79                        // 1. Haal verse data op van het netwerk
80                        const fetchPromise = fetch(event.request).then((networkResponse) => {
81                            // Update de cache met de nieuwe data
82                            cache.put(event.request, networkResponse.clone());
83                            return networkResponse;
84                        }).catch(() => {
85                            // Als netwerk faalt (server down), hebben we hopelijk de cache
86                        });
87
88                        // 2. Als we cache hebben, geef die DIRECT terug (snelheid!)
89                        // De fetchPromise draait op de achtergrond en update de cache voor de VOLGENDE keer.
90                        return cachedResponse || fetchPromise;
91                    });
92                })
93            );
94            return; // Stop hier, we hebben dit afgehandeld
95        }
96    }
97
98    // -----------------------------------------------------------------------
99    // B. STATIC ASSETS STRATEGIE: Cache First, Network Fallback
100    // -----------------------------------------------------------------------
101    // Voor plaatjes, CSS, JS, Fonts (ook die van de static server!)
102    if (event.request.destination === 'image' || 
103        event.request.destination === 'style' || 
104        event.request.destination === 'script' || 
105        event.request.destination === 'font') {
106
107        event.respondWith(
108            caches.open(CACHE_NAMES.static).then((cache) => {
109                return cache.match(event.request).then((response) => {
110                    // Als in cache: return cache. Anders: haal op van netwerk en stop in cache.
111                    return response || fetch(event.request).then((networkResponse) => {
112                        // Check of respons geldig is voordat we cachen
113                        if(networkResponse && networkResponse.status === 200) {
114                            cache.put(event.request, networkResponse.clone());
115                        }
116                        return networkResponse;
117                    });
118                });
119            })
120        );
121        return;
122    }
123
124    // -----------------------------------------------------------------------
125    // C. HTML PAGINA'S STRATEGIE: Network First, Fallback to Offline Page
126    // -----------------------------------------------------------------------
127    if (event.request.mode === 'navigate') {
128        event.respondWith(
129            fetch(event.request)
130                .then((response) => {
131                    // Als pagina succesvol laadt, stop hem in de 'pages' cache
132                    // Zodat we hem later offline kunnen tonen
133                    const resClone = response.clone();
134                    caches.open(CACHE_NAMES.pages).then((cache) => {
135                        cache.put(event.request, resClone);
136                    });
137                    return response;
138                })
139                .catch(() => {
140                    // NETWERK STUK OF SERVER 500 ERROR?
141                    return caches.open(CACHE_NAMES.pages).then((cache) => {
142                        return cache.match(event.request).then((cachedPage) => {
143                            // 1. Probeer de pagina uit cache te halen (als je er eerder bent geweest)
144                            if (cachedPage) return cachedPage;
145                            
146                            // 2. Als pagina niet in cache zit, toon de generieke offline pagina
147                            return caches.match(OFFLINE_URL);
148                        });
149                    });
150                })
151        );
152    }
153});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.