@julien-lin/universal-pwa-templates 1.2.4 → 1.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -34,497 +34,507 @@ module.exports = __toCommonJS(index_exports);
34
34
 
35
35
  // src/service-worker/static.ts
36
36
  var staticServiceWorkerTemplate = `
37
- import { precacheAndRoute } from 'workbox-precaching'
38
- import { registerRoute } from 'workbox-routing'
39
- import { CacheFirst, NetworkFirst } from 'workbox-strategies'
40
- import { CacheableResponsePlugin } from 'workbox-cacheable-response'
41
- import { ExpirationPlugin } from 'workbox-expiration'
42
-
43
- // Precache des assets statiques
44
- precacheAndRoute(self.__WB_MANIFEST)
45
-
46
- // CacheFirst pour images, fonts, CSS, JS
47
- registerRoute(
48
- ({ request }) => request.destination === 'image',
49
- new CacheFirst({
50
- cacheName: 'images-cache',
51
- plugins: [
52
- new CacheableResponsePlugin({
53
- statuses: [0, 200],
54
- }),
55
- new ExpirationPlugin({
56
- maxEntries: 60,
57
- maxAgeSeconds: 30 * 24 * 60 * 60, // 30 jours
58
- }),
59
- ],
60
- })
61
- )
62
-
63
- registerRoute(
64
- ({ request }) => request.destination === 'font',
65
- new CacheFirst({
66
- cacheName: 'fonts-cache',
67
- plugins: [
68
- new CacheableResponsePlugin({
69
- statuses: [0, 200],
70
- }),
71
- new ExpirationPlugin({
72
- maxEntries: 30,
73
- maxAgeSeconds: 365 * 24 * 60 * 60, // 1 an
74
- }),
75
- ],
76
- })
77
- )
78
-
79
- registerRoute(
80
- ({ request }) => request.destination === 'style' || request.destination === 'script',
81
- new CacheFirst({
82
- cacheName: 'assets-cache',
83
- plugins: [
84
- new CacheableResponsePlugin({
85
- statuses: [0, 200],
86
- }),
87
- new ExpirationPlugin({
88
- maxEntries: 50,
89
- maxAgeSeconds: 7 * 24 * 60 * 60, // 7 jours
90
- }),
91
- ],
92
- })
93
- )
94
-
95
- // NetworkFirst pour les pages HTML
96
- registerRoute(
97
- ({ request }) => request.mode === 'navigate',
98
- new NetworkFirst({
99
- cacheName: 'pages-cache',
100
- plugins: [
101
- new CacheableResponsePlugin({
102
- statuses: [0, 200],
103
- }),
104
- new ExpirationPlugin({
105
- maxEntries: 50,
106
- maxAgeSeconds: 24 * 60 * 60, // 24 heures
107
- }),
108
- ],
109
- })
110
- )
37
+ // Load Workbox from CDN
38
+ importScripts('https://storage.googleapis.com/workbox-cdn/releases/7.4.0/workbox-sw.js')
39
+
40
+ // Ensure Workbox is loaded
41
+ if (typeof workbox !== 'undefined') {
42
+ // Precache des assets statiques
43
+ workbox.precaching.precacheAndRoute(self.__WB_MANIFEST)
44
+
45
+ // CacheFirst pour images, fonts, CSS, JS
46
+ workbox.routing.registerRoute(
47
+ ({ request }) => request.destination === 'image',
48
+ new workbox.strategies.CacheFirst({
49
+ cacheName: 'images-cache',
50
+ plugins: [
51
+ new workbox.cacheableResponse.CacheableResponsePlugin({
52
+ statuses: [0, 200],
53
+ }),
54
+ new workbox.expiration.ExpirationPlugin({
55
+ maxEntries: 60,
56
+ maxAgeSeconds: 30 * 24 * 60 * 60, // 30 jours
57
+ }),
58
+ ],
59
+ })
60
+ )
61
+
62
+ workbox.routing.registerRoute(
63
+ ({ request }) => request.destination === 'font',
64
+ new workbox.strategies.CacheFirst({
65
+ cacheName: 'fonts-cache',
66
+ plugins: [
67
+ new workbox.cacheableResponse.CacheableResponsePlugin({
68
+ statuses: [0, 200],
69
+ }),
70
+ new workbox.expiration.ExpirationPlugin({
71
+ maxEntries: 30,
72
+ maxAgeSeconds: 365 * 24 * 60 * 60, // 1 an
73
+ }),
74
+ ],
75
+ })
76
+ )
77
+
78
+ workbox.routing.registerRoute(
79
+ ({ request }) => request.destination === 'style' || request.destination === 'script',
80
+ new workbox.strategies.CacheFirst({
81
+ cacheName: 'assets-cache',
82
+ plugins: [
83
+ new workbox.cacheableResponse.CacheableResponsePlugin({
84
+ statuses: [0, 200],
85
+ }),
86
+ new workbox.expiration.ExpirationPlugin({
87
+ maxEntries: 50,
88
+ maxAgeSeconds: 7 * 24 * 60 * 60, // 7 jours
89
+ }),
90
+ ],
91
+ })
92
+ )
93
+
94
+ // NetworkFirst pour les pages HTML
95
+ workbox.routing.registerRoute(
96
+ ({ request }) => request.mode === 'navigate',
97
+ new workbox.strategies.NetworkFirst({
98
+ cacheName: 'pages-cache',
99
+ plugins: [
100
+ new workbox.cacheableResponse.CacheableResponsePlugin({
101
+ statuses: [0, 200],
102
+ }),
103
+ new workbox.expiration.ExpirationPlugin({
104
+ maxEntries: 50,
105
+ maxAgeSeconds: 24 * 60 * 60, // 24 heures
106
+ }),
107
+ ],
108
+ })
109
+ )
110
+ } else {
111
+ console.error('Workbox could not be loaded.')
112
+ }
111
113
  `;
112
114
 
113
115
  // src/service-worker/spa.ts
114
116
  var spaServiceWorkerTemplate = `
115
- import { precacheAndRoute, createHandlerBoundToURL } from 'workbox-precaching'
116
- import { registerRoute, NavigationRoute } from 'workbox-routing'
117
- import { CacheFirst, NetworkFirst, StaleWhileRevalidate } from 'workbox-strategies'
118
- import { CacheableResponsePlugin } from 'workbox-cacheable-response'
119
- import { ExpirationPlugin } from 'workbox-expiration'
120
-
121
- // Precache des assets statiques
122
- precacheAndRoute(self.__WB_MANIFEST)
123
-
124
- // Navigation route pour SPA (toutes les navigations vers index.html)
125
- const navigationHandler = createHandlerBoundToURL('/index.html')
126
- const navigationRoute = new NavigationRoute(navigationHandler, {
127
- allowlist: [/^\\//],
128
- denylist: [/^\\/api/, /^\\/_/],
129
- })
130
- registerRoute(navigationRoute)
131
-
132
- // CacheFirst pour images, fonts
133
- registerRoute(
134
- ({ request }) => request.destination === 'image',
135
- new CacheFirst({
136
- cacheName: 'images-cache',
137
- plugins: [
138
- new CacheableResponsePlugin({
139
- statuses: [0, 200],
140
- }),
141
- new ExpirationPlugin({
142
- maxEntries: 60,
143
- maxAgeSeconds: 30 * 24 * 60 * 60, // 30 jours
144
- }),
145
- ],
146
- })
147
- )
148
-
149
- registerRoute(
150
- ({ request }) => request.destination === 'font',
151
- new CacheFirst({
152
- cacheName: 'fonts-cache',
153
- plugins: [
154
- new CacheableResponsePlugin({
155
- statuses: [0, 200],
156
- }),
157
- new ExpirationPlugin({
158
- maxEntries: 30,
159
- maxAgeSeconds: 365 * 24 * 60 * 60, // 1 an
160
- }),
161
- ],
162
- })
163
- )
164
-
165
- // StaleWhileRevalidate for CSS/JS (background update)
166
- registerRoute(
167
- ({ request }) => request.destination === 'style' || request.destination === 'script',
168
- new StaleWhileRevalidate({
169
- cacheName: 'assets-cache',
170
- plugins: [
171
- new CacheableResponsePlugin({
172
- statuses: [0, 200],
173
- }),
174
- new ExpirationPlugin({
175
- maxEntries: 50,
176
- maxAgeSeconds: 7 * 24 * 60 * 60, // 7 jours
177
- }),
178
- ],
179
- })
180
- )
181
-
182
- // NetworkFirst pour les appels API
183
- registerRoute(
184
- ({ url }) => url.pathname.startsWith('/api/'),
185
- new NetworkFirst({
186
- cacheName: 'api-cache',
187
- plugins: [
188
- new CacheableResponsePlugin({
189
- statuses: [0, 200],
190
- }),
191
- new ExpirationPlugin({
192
- maxEntries: 50,
193
- maxAgeSeconds: 5 * 60, // 5 minutes
194
- }),
195
- ],
117
+ // Load Workbox from CDN
118
+ importScripts('https://storage.googleapis.com/workbox-cdn/releases/7.4.0/workbox-sw.js')
119
+
120
+ // Ensure Workbox is loaded
121
+ if (typeof workbox !== 'undefined') {
122
+ // Precache des assets statiques
123
+ workbox.precaching.precacheAndRoute(self.__WB_MANIFEST)
124
+
125
+ // Navigation route pour SPA (toutes les navigations vers index.html)
126
+ const navigationHandler = workbox.precaching.createHandlerBoundToURL('/index.html')
127
+ const navigationRoute = new workbox.routing.NavigationRoute(navigationHandler, {
128
+ allowlist: [/^\\//],
129
+ denylist: [/^\\/api/, /^\\/_/],
196
130
  })
197
- )
131
+ workbox.routing.registerRoute(navigationRoute)
132
+
133
+ // CacheFirst pour images, fonts
134
+ workbox.routing.registerRoute(
135
+ ({ request }) => request.destination === 'image',
136
+ new workbox.strategies.CacheFirst({
137
+ cacheName: 'images-cache',
138
+ plugins: [
139
+ new workbox.cacheableResponse.CacheableResponsePlugin({
140
+ statuses: [0, 200],
141
+ }),
142
+ new workbox.expiration.ExpirationPlugin({
143
+ maxEntries: 60,
144
+ maxAgeSeconds: 30 * 24 * 60 * 60, // 30 jours
145
+ }),
146
+ ],
147
+ })
148
+ )
149
+
150
+ workbox.routing.registerRoute(
151
+ ({ request }) => request.destination === 'font',
152
+ new workbox.strategies.CacheFirst({
153
+ cacheName: 'fonts-cache',
154
+ plugins: [
155
+ new workbox.cacheableResponse.CacheableResponsePlugin({
156
+ statuses: [0, 200],
157
+ }),
158
+ new workbox.expiration.ExpirationPlugin({
159
+ maxEntries: 30,
160
+ maxAgeSeconds: 365 * 24 * 60 * 60, // 1 an
161
+ }),
162
+ ],
163
+ })
164
+ )
165
+
166
+ // StaleWhileRevalidate for CSS/JS (background update)
167
+ workbox.routing.registerRoute(
168
+ ({ request }) => request.destination === 'style' || request.destination === 'script',
169
+ new workbox.strategies.StaleWhileRevalidate({
170
+ cacheName: 'assets-cache',
171
+ plugins: [
172
+ new workbox.cacheableResponse.CacheableResponsePlugin({
173
+ statuses: [0, 200],
174
+ }),
175
+ new workbox.expiration.ExpirationPlugin({
176
+ maxEntries: 50,
177
+ maxAgeSeconds: 7 * 24 * 60 * 60, // 7 jours
178
+ }),
179
+ ],
180
+ })
181
+ )
182
+
183
+ // NetworkFirst pour les appels API
184
+ workbox.routing.registerRoute(
185
+ ({ url }) => url.pathname.startsWith('/api/'),
186
+ new workbox.strategies.NetworkFirst({
187
+ cacheName: 'api-cache',
188
+ plugins: [
189
+ new workbox.cacheableResponse.CacheableResponsePlugin({
190
+ statuses: [0, 200],
191
+ }),
192
+ new workbox.expiration.ExpirationPlugin({
193
+ maxEntries: 50,
194
+ maxAgeSeconds: 5 * 60, // 5 minutes
195
+ }),
196
+ ],
197
+ })
198
+ )
199
+ } else {
200
+ console.error('Workbox could not be loaded.')
201
+ }
198
202
  `;
199
203
 
200
204
  // src/service-worker/ssr.ts
201
205
  var ssrServiceWorkerTemplate = `
202
- import { precacheAndRoute } from 'workbox-precaching'
203
- import { registerRoute } from 'workbox-routing'
204
- import { CacheFirst, NetworkFirst, StaleWhileRevalidate } from 'workbox-strategies'
205
- import { CacheableResponsePlugin } from 'workbox-cacheable-response'
206
- import { ExpirationPlugin } from 'workbox-expiration'
207
-
208
- // Precache des assets statiques critiques
209
- precacheAndRoute(self.__WB_MANIFEST)
210
-
211
- // NetworkFirst for HTML pages (server priority for fresh content)
212
- registerRoute(
213
- ({ request }) => request.mode === 'navigate',
214
- new NetworkFirst({
215
- cacheName: 'pages-cache',
216
- plugins: [
217
- new CacheableResponsePlugin({
218
- statuses: [0, 200],
219
- }),
220
- new ExpirationPlugin({
221
- maxEntries: 50,
222
- maxAgeSeconds: 24 * 60 * 60, // 24 heures
223
- }),
224
- ],
225
- networkTimeoutSeconds: 3, // Timeout de 3 secondes
226
- })
227
- )
228
-
229
- // CacheFirst pour images, fonts
230
- registerRoute(
231
- ({ request }) => request.destination === 'image',
232
- new CacheFirst({
233
- cacheName: 'images-cache',
234
- plugins: [
235
- new CacheableResponsePlugin({
236
- statuses: [0, 200],
237
- }),
238
- new ExpirationPlugin({
239
- maxEntries: 60,
240
- maxAgeSeconds: 30 * 24 * 60 * 60, // 30 jours
241
- }),
242
- ],
243
- })
244
- )
245
-
246
- registerRoute(
247
- ({ request }) => request.destination === 'font',
248
- new CacheFirst({
249
- cacheName: 'fonts-cache',
250
- plugins: [
251
- new CacheableResponsePlugin({
252
- statuses: [0, 200],
253
- }),
254
- new ExpirationPlugin({
255
- maxEntries: 30,
256
- maxAgeSeconds: 365 * 24 * 60 * 60, // 1 an
257
- }),
258
- ],
259
- })
260
- )
261
-
262
- // StaleWhileRevalidate pour CSS/JS
263
- registerRoute(
264
- ({ request }) => request.destination === 'style' || request.destination === 'script',
265
- new StaleWhileRevalidate({
266
- cacheName: 'assets-cache',
267
- plugins: [
268
- new CacheableResponsePlugin({
269
- statuses: [0, 200],
270
- }),
271
- new ExpirationPlugin({
272
- maxEntries: 50,
273
- maxAgeSeconds: 7 * 24 * 60 * 60, // 7 jours
274
- }),
275
- ],
276
- })
277
- )
278
-
279
- // NetworkFirst pour les appels API
280
- registerRoute(
281
- ({ url }) => url.pathname.startsWith('/api/') || url.pathname.startsWith('/graphql'),
282
- new NetworkFirst({
283
- cacheName: 'api-cache',
284
- plugins: [
285
- new CacheableResponsePlugin({
286
- statuses: [0, 200],
287
- }),
288
- new ExpirationPlugin({
289
- maxEntries: 50,
290
- maxAgeSeconds: 5 * 60, // 5 minutes
291
- }),
292
- ],
293
- networkTimeoutSeconds: 3,
294
- })
295
- )
206
+ // Load Workbox from CDN
207
+ importScripts('https://storage.googleapis.com/workbox-cdn/releases/7.4.0/workbox-sw.js')
208
+
209
+ // Ensure Workbox is loaded
210
+ if (typeof workbox !== 'undefined') {
211
+ // Precache des assets statiques critiques
212
+ workbox.precaching.precacheAndRoute(self.__WB_MANIFEST)
213
+
214
+ // NetworkFirst for HTML pages (server priority for fresh content)
215
+ workbox.routing.registerRoute(
216
+ ({ request }) => request.mode === 'navigate',
217
+ new workbox.strategies.NetworkFirst({
218
+ cacheName: 'pages-cache',
219
+ plugins: [
220
+ new workbox.cacheableResponse.CacheableResponsePlugin({
221
+ statuses: [0, 200],
222
+ }),
223
+ new workbox.expiration.ExpirationPlugin({
224
+ maxEntries: 50,
225
+ maxAgeSeconds: 24 * 60 * 60, // 24 heures
226
+ }),
227
+ ],
228
+ networkTimeoutSeconds: 3, // Timeout de 3 secondes
229
+ })
230
+ )
231
+
232
+ // CacheFirst pour images, fonts
233
+ workbox.routing.registerRoute(
234
+ ({ request }) => request.destination === 'image',
235
+ new workbox.strategies.CacheFirst({
236
+ cacheName: 'images-cache',
237
+ plugins: [
238
+ new workbox.cacheableResponse.CacheableResponsePlugin({
239
+ statuses: [0, 200],
240
+ }),
241
+ new workbox.expiration.ExpirationPlugin({
242
+ maxEntries: 60,
243
+ maxAgeSeconds: 30 * 24 * 60 * 60, // 30 jours
244
+ }),
245
+ ],
246
+ })
247
+ )
248
+
249
+ workbox.routing.registerRoute(
250
+ ({ request }) => request.destination === 'font',
251
+ new workbox.strategies.CacheFirst({
252
+ cacheName: 'fonts-cache',
253
+ plugins: [
254
+ new workbox.cacheableResponse.CacheableResponsePlugin({
255
+ statuses: [0, 200],
256
+ }),
257
+ new workbox.expiration.ExpirationPlugin({
258
+ maxEntries: 30,
259
+ maxAgeSeconds: 365 * 24 * 60 * 60, // 1 an
260
+ }),
261
+ ],
262
+ })
263
+ )
264
+
265
+ // StaleWhileRevalidate pour CSS/JS
266
+ workbox.routing.registerRoute(
267
+ ({ request }) => request.destination === 'style' || request.destination === 'script',
268
+ new workbox.strategies.StaleWhileRevalidate({
269
+ cacheName: 'assets-cache',
270
+ plugins: [
271
+ new workbox.cacheableResponse.CacheableResponsePlugin({
272
+ statuses: [0, 200],
273
+ }),
274
+ new workbox.expiration.ExpirationPlugin({
275
+ maxEntries: 50,
276
+ maxAgeSeconds: 7 * 24 * 60 * 60, // 7 jours
277
+ }),
278
+ ],
279
+ })
280
+ )
281
+
282
+ // NetworkFirst pour les appels API
283
+ workbox.routing.registerRoute(
284
+ ({ url }) => url.pathname.startsWith('/api/') || url.pathname.startsWith('/graphql'),
285
+ new workbox.strategies.NetworkFirst({
286
+ cacheName: 'api-cache',
287
+ plugins: [
288
+ new workbox.cacheableResponse.CacheableResponsePlugin({
289
+ statuses: [0, 200],
290
+ }),
291
+ new workbox.expiration.ExpirationPlugin({
292
+ maxEntries: 50,
293
+ maxAgeSeconds: 5 * 60, // 5 minutes
294
+ }),
295
+ ],
296
+ networkTimeoutSeconds: 3,
297
+ })
298
+ )
299
+ } else {
300
+ console.error('Workbox could not be loaded.')
301
+ }
296
302
  `;
297
303
 
298
304
  // src/service-worker/wordpress.ts
299
305
  var wordpressServiceWorkerTemplate = `
300
- import { precacheAndRoute } from 'workbox-precaching'
301
- import { registerRoute } from 'workbox-routing'
302
- import { CacheFirst, NetworkFirst, StaleWhileRevalidate } from 'workbox-strategies'
303
- import { CacheableResponsePlugin } from 'workbox-cacheable-response'
304
- import { ExpirationPlugin } from 'workbox-expiration'
305
-
306
- // Precache des assets statiques
307
- precacheAndRoute(self.__WB_MANIFEST)
308
-
309
- // NetworkFirst for WordPress pages (server priority)
310
- registerRoute(
311
- ({ request }) => request.mode === 'navigate',
312
- new NetworkFirst({
313
- cacheName: 'wp-pages-cache',
314
- plugins: [
315
- new CacheableResponsePlugin({
316
- statuses: [0, 200],
317
- }),
318
- new ExpirationPlugin({
319
- maxEntries: 50,
320
- maxAgeSeconds: 24 * 60 * 60, // 24 heures
321
- }),
322
- ],
323
- networkTimeoutSeconds: 3,
324
- })
325
- )
326
-
327
- // CacheFirst pour les assets WordPress (wp-content)
328
- registerRoute(
329
- ({ url }) => url.pathname.includes('/wp-content/'),
330
- new CacheFirst({
331
- cacheName: 'wp-assets-cache',
332
- plugins: [
333
- new CacheableResponsePlugin({
334
- statuses: [0, 200],
335
- }),
336
- new ExpirationPlugin({
337
- maxEntries: 100,
338
- maxAgeSeconds: 30 * 24 * 60 * 60, // 30 jours
339
- }),
340
- ],
341
- })
342
- )
343
-
344
- // CacheFirst pour images
345
- registerRoute(
346
- ({ request }) => request.destination === 'image',
347
- new CacheFirst({
348
- cacheName: 'images-cache',
349
- plugins: [
350
- new CacheableResponsePlugin({
351
- statuses: [0, 200],
352
- }),
353
- new ExpirationPlugin({
354
- maxEntries: 60,
355
- maxAgeSeconds: 30 * 24 * 60 * 60, // 30 jours
356
- }),
357
- ],
358
- })
359
- )
360
-
361
- // CacheFirst pour fonts
362
- registerRoute(
363
- ({ request }) => request.destination === 'font',
364
- new CacheFirst({
365
- cacheName: 'fonts-cache',
366
- plugins: [
367
- new CacheableResponsePlugin({
368
- statuses: [0, 200],
369
- }),
370
- new ExpirationPlugin({
371
- maxEntries: 30,
372
- maxAgeSeconds: 365 * 24 * 60 * 60, // 1 an
373
- }),
374
- ],
375
- })
376
- )
377
-
378
- // StaleWhileRevalidate pour CSS/JS
379
- registerRoute(
380
- ({ request }) => request.destination === 'style' || request.destination === 'script',
381
- new StaleWhileRevalidate({
382
- cacheName: 'assets-cache',
383
- plugins: [
384
- new CacheableResponsePlugin({
385
- statuses: [0, 200],
386
- }),
387
- new ExpirationPlugin({
388
- maxEntries: 50,
389
- maxAgeSeconds: 7 * 24 * 60 * 60, // 7 jours
390
- }),
391
- ],
392
- })
393
- )
394
-
395
- // NetworkFirst pour wp-admin et wp-json (API REST WordPress)
396
- registerRoute(
397
- ({ url }) => url.pathname.startsWith('/wp-admin/') || url.pathname.startsWith('/wp-json/'),
398
- new NetworkFirst({
399
- cacheName: 'wp-api-cache',
400
- plugins: [
401
- new CacheableResponsePlugin({
402
- statuses: [0, 200],
403
- }),
404
- new ExpirationPlugin({
405
- maxEntries: 30,
406
- maxAgeSeconds: 5 * 60, // 5 minutes
407
- }),
408
- ],
409
- networkTimeoutSeconds: 3,
410
- })
411
- )
306
+ // Load Workbox from CDN
307
+ importScripts('https://storage.googleapis.com/workbox-cdn/releases/7.4.0/workbox-sw.js')
308
+
309
+ // Ensure Workbox is loaded
310
+ if (typeof workbox !== 'undefined') {
311
+ // Precache des assets statiques
312
+ workbox.precaching.precacheAndRoute(self.__WB_MANIFEST)
313
+
314
+ // NetworkFirst for WordPress pages (server priority)
315
+ workbox.routing.registerRoute(
316
+ ({ request }) => request.mode === 'navigate',
317
+ new workbox.strategies.NetworkFirst({
318
+ cacheName: 'wp-pages-cache',
319
+ plugins: [
320
+ new workbox.cacheableResponse.CacheableResponsePlugin({
321
+ statuses: [0, 200],
322
+ }),
323
+ new workbox.expiration.ExpirationPlugin({
324
+ maxEntries: 50,
325
+ maxAgeSeconds: 24 * 60 * 60, // 24 heures
326
+ }),
327
+ ],
328
+ networkTimeoutSeconds: 3,
329
+ })
330
+ )
331
+
332
+ // CacheFirst pour les assets WordPress (wp-content)
333
+ workbox.routing.registerRoute(
334
+ ({ url }) => url.pathname.includes('/wp-content/'),
335
+ new workbox.strategies.CacheFirst({
336
+ cacheName: 'wp-assets-cache',
337
+ plugins: [
338
+ new workbox.cacheableResponse.CacheableResponsePlugin({
339
+ statuses: [0, 200],
340
+ }),
341
+ new workbox.expiration.ExpirationPlugin({
342
+ maxEntries: 100,
343
+ maxAgeSeconds: 30 * 24 * 60 * 60, // 30 jours
344
+ }),
345
+ ],
346
+ })
347
+ )
348
+
349
+ // CacheFirst pour images
350
+ workbox.routing.registerRoute(
351
+ ({ request }) => request.destination === 'image',
352
+ new workbox.strategies.CacheFirst({
353
+ cacheName: 'images-cache',
354
+ plugins: [
355
+ new workbox.cacheableResponse.CacheableResponsePlugin({
356
+ statuses: [0, 200],
357
+ }),
358
+ new workbox.expiration.ExpirationPlugin({
359
+ maxEntries: 60,
360
+ maxAgeSeconds: 30 * 24 * 60 * 60, // 30 jours
361
+ }),
362
+ ],
363
+ })
364
+ )
365
+
366
+ // CacheFirst pour fonts
367
+ workbox.routing.registerRoute(
368
+ ({ request }) => request.destination === 'font',
369
+ new workbox.strategies.CacheFirst({
370
+ cacheName: 'fonts-cache',
371
+ plugins: [
372
+ new workbox.cacheableResponse.CacheableResponsePlugin({
373
+ statuses: [0, 200],
374
+ }),
375
+ new workbox.expiration.ExpirationPlugin({
376
+ maxEntries: 30,
377
+ maxAgeSeconds: 365 * 24 * 60 * 60, // 1 an
378
+ }),
379
+ ],
380
+ })
381
+ )
382
+
383
+ // StaleWhileRevalidate pour CSS/JS
384
+ workbox.routing.registerRoute(
385
+ ({ request }) => request.destination === 'style' || request.destination === 'script',
386
+ new workbox.strategies.StaleWhileRevalidate({
387
+ cacheName: 'assets-cache',
388
+ plugins: [
389
+ new workbox.cacheableResponse.CacheableResponsePlugin({
390
+ statuses: [0, 200],
391
+ }),
392
+ new workbox.expiration.ExpirationPlugin({
393
+ maxEntries: 50,
394
+ maxAgeSeconds: 7 * 24 * 60 * 60, // 7 jours
395
+ }),
396
+ ],
397
+ })
398
+ )
399
+
400
+ // NetworkFirst pour wp-admin et wp-json (API REST WordPress)
401
+ workbox.routing.registerRoute(
402
+ ({ url }) => url.pathname.startsWith('/wp-admin/') || url.pathname.startsWith('/wp-json/'),
403
+ new workbox.strategies.NetworkFirst({
404
+ cacheName: 'wp-api-cache',
405
+ plugins: [
406
+ new workbox.cacheableResponse.CacheableResponsePlugin({
407
+ statuses: [0, 200],
408
+ }),
409
+ new workbox.expiration.ExpirationPlugin({
410
+ maxEntries: 30,
411
+ maxAgeSeconds: 5 * 60, // 5 minutes
412
+ }),
413
+ ],
414
+ networkTimeoutSeconds: 3,
415
+ })
416
+ )
417
+ } else {
418
+ console.error('Workbox could not be loaded.')
419
+ }
412
420
  `;
413
421
 
414
422
  // src/service-worker/php.ts
415
423
  var phpServiceWorkerTemplate = `
416
- import { precacheAndRoute } from 'workbox-precaching'
417
- import { registerRoute } from 'workbox-routing'
418
- import { CacheFirst, NetworkFirst, StaleWhileRevalidate } from 'workbox-strategies'
419
- import { CacheableResponsePlugin } from 'workbox-cacheable-response'
420
- import { ExpirationPlugin } from 'workbox-expiration'
421
-
422
- // Precache des assets statiques
423
- precacheAndRoute(self.__WB_MANIFEST)
424
-
425
- // NetworkFirst for PHP pages (server priority)
426
- registerRoute(
427
- ({ request }) => request.mode === 'navigate',
428
- new NetworkFirst({
429
- cacheName: 'php-pages-cache',
430
- plugins: [
431
- new CacheableResponsePlugin({
432
- statuses: [0, 200],
433
- }),
434
- new ExpirationPlugin({
435
- maxEntries: 50,
436
- maxAgeSeconds: 24 * 60 * 60, // 24 heures
437
- }),
438
- ],
439
- networkTimeoutSeconds: 3,
440
- })
441
- )
442
-
443
- // CacheFirst pour les assets publics (build/, public/)
444
- registerRoute(
445
- ({ url }) => url.pathname.startsWith('/build/') || url.pathname.startsWith('/public/'),
446
- new CacheFirst({
447
- cacheName: 'public-assets-cache',
448
- plugins: [
449
- new CacheableResponsePlugin({
450
- statuses: [0, 200],
451
- }),
452
- new ExpirationPlugin({
453
- maxEntries: 100,
454
- maxAgeSeconds: 30 * 24 * 60 * 60, // 30 jours
455
- }),
456
- ],
457
- })
458
- )
459
-
460
- // CacheFirst pour images
461
- registerRoute(
462
- ({ request }) => request.destination === 'image',
463
- new CacheFirst({
464
- cacheName: 'images-cache',
465
- plugins: [
466
- new CacheableResponsePlugin({
467
- statuses: [0, 200],
468
- }),
469
- new ExpirationPlugin({
470
- maxEntries: 60,
471
- maxAgeSeconds: 30 * 24 * 60 * 60, // 30 jours
472
- }),
473
- ],
474
- })
475
- )
476
-
477
- // CacheFirst pour fonts
478
- registerRoute(
479
- ({ request }) => request.destination === 'font',
480
- new CacheFirst({
481
- cacheName: 'fonts-cache',
482
- plugins: [
483
- new CacheableResponsePlugin({
484
- statuses: [0, 200],
485
- }),
486
- new ExpirationPlugin({
487
- maxEntries: 30,
488
- maxAgeSeconds: 365 * 24 * 60 * 60, // 1 an
489
- }),
490
- ],
491
- })
492
- )
493
-
494
- // StaleWhileRevalidate pour CSS/JS
495
- registerRoute(
496
- ({ request }) => request.destination === 'style' || request.destination === 'script',
497
- new StaleWhileRevalidate({
498
- cacheName: 'assets-cache',
499
- plugins: [
500
- new CacheableResponsePlugin({
501
- statuses: [0, 200],
502
- }),
503
- new ExpirationPlugin({
504
- maxEntries: 50,
505
- maxAgeSeconds: 7 * 24 * 60 * 60, // 7 jours
506
- }),
507
- ],
508
- })
509
- )
510
-
511
- // NetworkFirst pour les routes API
512
- registerRoute(
513
- ({ url }) => url.pathname.startsWith('/api/') || url.pathname.startsWith('/graphql'),
514
- new NetworkFirst({
515
- cacheName: 'api-cache',
516
- plugins: [
517
- new CacheableResponsePlugin({
518
- statuses: [0, 200],
519
- }),
520
- new ExpirationPlugin({
521
- maxEntries: 50,
522
- maxAgeSeconds: 5 * 60, // 5 minutes
523
- }),
524
- ],
525
- networkTimeoutSeconds: 3,
526
- })
527
- )
424
+ // Load Workbox from CDN
425
+ importScripts('https://storage.googleapis.com/workbox-cdn/releases/7.4.0/workbox-sw.js')
426
+
427
+ // Ensure Workbox is loaded
428
+ if (typeof workbox !== 'undefined') {
429
+ // Precache des assets statiques
430
+ workbox.precaching.precacheAndRoute(self.__WB_MANIFEST)
431
+
432
+ // NetworkFirst for PHP pages (server priority)
433
+ workbox.routing.registerRoute(
434
+ ({ request }) => request.mode === 'navigate',
435
+ new workbox.strategies.NetworkFirst({
436
+ cacheName: 'php-pages-cache',
437
+ plugins: [
438
+ new workbox.cacheableResponse.CacheableResponsePlugin({
439
+ statuses: [0, 200],
440
+ }),
441
+ new workbox.expiration.ExpirationPlugin({
442
+ maxEntries: 50,
443
+ maxAgeSeconds: 24 * 60 * 60, // 24 heures
444
+ }),
445
+ ],
446
+ networkTimeoutSeconds: 3,
447
+ })
448
+ )
449
+
450
+ // CacheFirst pour les assets publics (build/, public/)
451
+ workbox.routing.registerRoute(
452
+ ({ url }) => url.pathname.startsWith('/build/') || url.pathname.startsWith('/public/'),
453
+ new workbox.strategies.CacheFirst({
454
+ cacheName: 'public-assets-cache',
455
+ plugins: [
456
+ new workbox.cacheableResponse.CacheableResponsePlugin({
457
+ statuses: [0, 200],
458
+ }),
459
+ new workbox.expiration.ExpirationPlugin({
460
+ maxEntries: 100,
461
+ maxAgeSeconds: 30 * 24 * 60 * 60, // 30 jours
462
+ }),
463
+ ],
464
+ })
465
+ )
466
+
467
+ // CacheFirst pour images
468
+ workbox.routing.registerRoute(
469
+ ({ request }) => request.destination === 'image',
470
+ new workbox.strategies.CacheFirst({
471
+ cacheName: 'images-cache',
472
+ plugins: [
473
+ new workbox.cacheableResponse.CacheableResponsePlugin({
474
+ statuses: [0, 200],
475
+ }),
476
+ new workbox.expiration.ExpirationPlugin({
477
+ maxEntries: 60,
478
+ maxAgeSeconds: 30 * 24 * 60 * 60, // 30 jours
479
+ }),
480
+ ],
481
+ })
482
+ )
483
+
484
+ // CacheFirst pour fonts
485
+ workbox.routing.registerRoute(
486
+ ({ request }) => request.destination === 'font',
487
+ new workbox.strategies.CacheFirst({
488
+ cacheName: 'fonts-cache',
489
+ plugins: [
490
+ new workbox.cacheableResponse.CacheableResponsePlugin({
491
+ statuses: [0, 200],
492
+ }),
493
+ new workbox.expiration.ExpirationPlugin({
494
+ maxEntries: 30,
495
+ maxAgeSeconds: 365 * 24 * 60 * 60, // 1 an
496
+ }),
497
+ ],
498
+ })
499
+ )
500
+
501
+ // StaleWhileRevalidate pour CSS/JS
502
+ workbox.routing.registerRoute(
503
+ ({ request }) => request.destination === 'style' || request.destination === 'script',
504
+ new workbox.strategies.StaleWhileRevalidate({
505
+ cacheName: 'assets-cache',
506
+ plugins: [
507
+ new workbox.cacheableResponse.CacheableResponsePlugin({
508
+ statuses: [0, 200],
509
+ }),
510
+ new workbox.expiration.ExpirationPlugin({
511
+ maxEntries: 50,
512
+ maxAgeSeconds: 7 * 24 * 60 * 60, // 7 jours
513
+ }),
514
+ ],
515
+ })
516
+ )
517
+
518
+ // NetworkFirst pour les routes API
519
+ workbox.routing.registerRoute(
520
+ ({ url }) => url.pathname.startsWith('/api/') || url.pathname.startsWith('/graphql'),
521
+ new workbox.strategies.NetworkFirst({
522
+ cacheName: 'api-cache',
523
+ plugins: [
524
+ new workbox.cacheableResponse.CacheableResponsePlugin({
525
+ statuses: [0, 200],
526
+ }),
527
+ new workbox.expiration.ExpirationPlugin({
528
+ maxEntries: 50,
529
+ maxAgeSeconds: 5 * 60, // 5 minutes
530
+ }),
531
+ ],
532
+ networkTimeoutSeconds: 3,
533
+ })
534
+ )
535
+ } else {
536
+ console.error('Workbox could not be loaded.')
537
+ }
528
538
  `;
529
539
 
530
540
  // src/service-worker/index.ts