diwash-barla commited on
Commit
da8203e
·
verified ·
1 Parent(s): 45e3d7c

Create static/service-worker.js

Browse files
Files changed (1) hide show
  1. static/service-worker.js +52 -0
static/service-worker.js ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const CACHE_NAME = 'ai-video-generator-v1';
2
+ // The app shell files that should be cached
3
+ const urlsToCache = [
4
+ '/',
5
+ 'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css'
6
+ ];
7
+
8
+ // Install event: opens a cache and adds the app shell files to it.
9
+ self.addEventListener('install', event => {
10
+ console.log('Service Worker: Installing...');
11
+ event.waitUntil(
12
+ caches.open(CACHE_NAME)
13
+ .then(cache => {
14
+ console.log('Service Worker: Caching app shell');
15
+ return cache.addAll(urlsToCache);
16
+ })
17
+ .then(() => self.skipWaiting())
18
+ );
19
+ });
20
+
21
+ // Activate event: removes old caches.
22
+ self.addEventListener('activate', event => {
23
+ console.log('Service Worker: Activating...');
24
+ event.waitUntil(
25
+ caches.keys().then(cacheNames => {
26
+ return Promise.all(
27
+ cacheNames.map(cache => {
28
+ if (cache !== CACHE_NAME) {
29
+ console.log('Service Worker: Clearing old cache');
30
+ return caches.delete(cache);
31
+ }
32
+ })
33
+ );
34
+ })
35
+ );
36
+ });
37
+
38
+ // Fetch event: serves cached content when offline.
39
+ self.addEventListener('fetch', event => {
40
+ event.respondWith(
41
+ caches.match(event.request)
42
+ .then(response => {
43
+ // Cache hit - return response
44
+ if (response) {
45
+ return response;
46
+ }
47
+ // Not in cache - fetch from network
48
+ return fetch(event.request);
49
+ }
50
+ )
51
+ );
52
+ });