-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkbox-runtime-caching.js
More file actions
167 lines (158 loc) · 3.73 KB
/
workbox-runtime-caching.js
File metadata and controls
167 lines (158 loc) · 3.73 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
module.exports = [
// 1) Next.js static assets (JS/CSS/fonts) - Critical for offline functionality
{
urlPattern: /^\/_next\/static\/.*/i,
handler: 'CacheFirst',
options: {
cacheName: 'next-static-assets',
expiration: {
maxEntries: 200,
maxAgeSeconds: 60 * 60 * 24 * 30, // 30 days
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
// 2) Next.js chunks and app bundles - Essential for app functionality
{
urlPattern: /^\/_next\/static\/chunks\/.*/i,
handler: 'CacheFirst',
options: {
cacheName: 'next-chunks',
expiration: {
maxEntries: 100,
maxAgeSeconds: 60 * 60 * 24 * 30, // 30 days
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
// 3) Navigation requests (page loads) - NetworkFirst with offline fallback
{
urlPattern: ({ request }) => request.mode === 'navigate',
handler: 'NetworkFirst',
options: {
cacheName: 'pages-cache',
networkTimeoutSeconds: 3,
expiration: {
maxEntries: 10,
maxAgeSeconds: 60 * 60 * 24, // 24 hours
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
// 4) Public assets (images, icons, etc.)
{
urlPattern: ({ url }) =>
url.pathname.startsWith('/images/') ||
url.pathname.startsWith('/icons/') ||
url.pathname.startsWith('/icon-') ||
url.pathname.match(/\.(png|jpg|jpeg|svg|gif|webp|ico)$/i),
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'public-assets',
expiration: {
maxEntries: 100,
maxAgeSeconds: 60 * 60 * 24 * 7, // 7 days
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
// 5) WASM files for PDF processing
{
urlPattern: /\.(?:wasm)$/i,
handler: 'CacheFirst',
options: {
cacheName: 'wasm-cache',
expiration: {
maxEntries: 10,
maxAgeSeconds: 365 * 24 * 60 * 60, // 1 year
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
// 6) PDF worker files
{
urlPattern: /pdf\.worker\.min\.js$/i,
handler: 'CacheFirst',
options: {
cacheName: 'pdf-worker',
expiration: {
maxEntries: 5,
maxAgeSeconds: 30 * 24 * 60 * 60, // 30 days
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
// 7) Google Fonts
{
urlPattern: /^https:\/\/fonts\.(?:googleapis|gstatic)\.com\/.*/i,
handler: 'CacheFirst',
options: {
cacheName: 'google-fonts',
expiration: {
maxEntries: 10,
maxAgeSeconds: 365 * 24 * 60 * 60, // 1 year
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
// 8) CSS files
{
urlPattern: /\.(?:css)$/i,
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'css-cache',
expiration: {
maxEntries: 50,
maxAgeSeconds: 60 * 60 * 24 * 7, // 7 days
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
// 9) JavaScript files (catch-all for any missed JS)
{
urlPattern: /\.(?:js)$/i,
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'js-cache',
expiration: {
maxEntries: 100,
maxAgeSeconds: 60 * 60 * 24 * 7, // 7 days
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
// 10) Manifest and other JSON files
{
urlPattern: /\.(?:json|xml)$/i,
handler: 'NetworkFirst',
options: {
cacheName: 'json-cache',
networkTimeoutSeconds: 3,
expiration: {
maxEntries: 20,
maxAgeSeconds: 60 * 60 * 24, // 24 hours
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
];