-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
151 lines (134 loc) · 3.81 KB
/
sw.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const CACHE_NAME = 'fake-news-checker-v2'
const OFFLINE_URL = '/pages/offline.html'
const CRITICAL_ASSETS = [
'/',
'/index.html',
'/assets/css/styles.css',
'/manifest.json',
'/favicon.ico',
OFFLINE_URL
]
const SECONDARY_ASSETS = [
'/assets/js/script.js',
'https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css',
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css'
]
const DEFERRED_ASSETS = [
'https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js'
]
self.addEventListener('install', event => {
event.waitUntil(
Promise.all([
caches.open(`${CACHE_NAME}-critical`).then(cache => {
console.log('Cacheando recursos críticos')
return cache.addAll(CRITICAL_ASSETS)
}),
caches.open(`${CACHE_NAME}-secondary`).then(cache => {
console.log('Cacheando recursos secundários')
return cache.addAll(SECONDARY_ASSETS)
}),
caches.open(`${CACHE_NAME}-deferred`).then(cache => {
console.log('Cacheando recursos adiados')
return cache.addAll(DEFERRED_ASSETS)
})
]).catch(error => {
console.error('Erro ao cachear assets:', error)
})
)
})
self.addEventListener('activate', event => {
event.waitUntil(
Promise.all([
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (!cacheName.startsWith(CACHE_NAME)) {
console.log('Removendo cache antigo:', cacheName)
return caches.delete(cacheName)
}
})
)
}),
self.clients.claim(),
fetch('/assets/js/script.js'),
fetch('/assets/css/styles.css')
])
)
})
self.addEventListener('fetch', event => {
const url = new URL(event.request.url)
// Handle navigation requests
if (event.request.mode === 'navigate') {
event.respondWith(
fetch(event.request).catch(() => {
return caches.match(OFFLINE_URL) || caches.match('/')
})
)
return
}
// Critical assets strategy
if (CRITICAL_ASSETS.includes(url.pathname)) {
event.respondWith(
caches
.match(event.request, { cacheName: `${CACHE_NAME}-critical` })
.then(
response =>
response || fetchAndCache(event.request, `${CACHE_NAME}-critical`)
)
)
return
}
// Secondary assets strategy
if (SECONDARY_ASSETS.some(asset => url.href.includes(asset))) {
event.respondWith(
caches
.match(event.request)
.then(
response =>
response || fetchAndCache(event.request, `${CACHE_NAME}-secondary`)
)
)
return
}
// Default strategy
event.respondWith(
caches.match(event.request).then(response => {
if (response) return response
return fetch(event.request)
.then(response => {
if (
!response ||
response.status !== 200 ||
response.type !== 'basic'
) {
return response
}
const responseToCache = response.clone()
caches
.open(`${CACHE_NAME}-deferred`)
.then(cache => cache.put(event.request, responseToCache))
return response
})
.catch(() => {
if (event.request.mode === 'navigate') {
return caches.match(OFFLINE_URL)
}
})
})
)
})
function fetchAndCache(request, cacheName) {
return fetch(request).then(response => {
if (!response || response.status !== 200 || response.type !== 'basic') {
return response
}
const responseToCache = response.clone()
caches.open(cacheName).then(cache => cache.put(request, responseToCache))
return response
})
}
self.addEventListener('message', event => {
if (event.data.action === 'skipWaiting') {
self.skipWaiting()
}
})