haepada commited on
Commit
cf8ddef
·
verified ·
1 Parent(s): 74fa578

Create service-worker.js

Browse files
Files changed (1) hide show
  1. static/service-worker.js +84 -0
static/service-worker.js ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const CACHE_NAME = 'digital-gut-v1';
2
+ const ASSETS_TO_CACHE = [
3
+ '/',
4
+ '/static/manifest.json',
5
+ '/assets/main_music.mp3',
6
+ // 아이콘 파일들
7
+ '/static/icons/icon-72x72.png',
8
+ '/static/icons/icon-96x96.png',
9
+ '/static/icons/icon-128x128.png',
10
+ '/static/icons/icon-144x144.png',
11
+ '/static/icons/icon-152x152.png',
12
+ '/static/icons/icon-192x192.png',
13
+ '/static/icons/icon-384x384.png',
14
+ '/static/icons/icon-512x512.png'
15
+ ];
16
+
17
+ // 설치 시 캐시
18
+ self.addEventListener('install', (event) => {
19
+ event.waitUntil(
20
+ caches.open(CACHE_NAME)
21
+ .then((cache) => {
22
+ console.log('Opened cache');
23
+ return cache.addAll(ASSETS_TO_CACHE);
24
+ })
25
+ .then(() => self.skipWaiting())
26
+ );
27
+ });
28
+
29
+ // 활성화 시 이전 캐시 정리
30
+ self.addEventListener('activate', (event) => {
31
+ event.waitUntil(
32
+ caches.keys().then((cacheNames) => {
33
+ return Promise.all(
34
+ cacheNames.map((cacheName) => {
35
+ if (cacheName !== CACHE_NAME) {
36
+ return caches.delete(cacheName);
37
+ }
38
+ })
39
+ );
40
+ })
41
+ );
42
+ });
43
+
44
+ // 네트워크 요청 처리
45
+ self.addEventListener('fetch', (event) => {
46
+ event.respondWith(
47
+ caches.match(event.request)
48
+ .then((response) => {
49
+ // 캐시에 있으면 캐시된 버전 반환
50
+ if (response) {
51
+ return response;
52
+ }
53
+
54
+ // 캐시에 없으면 네트워크 요청
55
+ return fetch(event.request).then(
56
+ (response) => {
57
+ // 유효하지 않은 응답이면 그대로 반환
58
+ if (!response || response.status !== 200 || response.type !== 'basic') {
59
+ return response;
60
+ }
61
+
62
+ // 응답을 캐시에 저장
63
+ const responseToCache = response.clone();
64
+ caches.open(CACHE_NAME)
65
+ .then((cache) => {
66
+ cache.put(event.request, responseToCache);
67
+ });
68
+
69
+ return response;
70
+ }
71
+ );
72
+ })
73
+ );
74
+ });
75
+
76
+ // 백그라운드 동기화
77
+ self.addEventListener('sync', (event) => {
78
+ if (event.tag === 'syncData') {
79
+ event.waitUntil(
80
+ // 오프라인 데이터 동기화 로직
81
+ syncData()
82
+ );
83
+ }
84
+ });