1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | const VERSION = "1.00" |
19 | |
20 | |
21 | |
22 | |
23 | const CACHE = "sincro" |
24 | |
25 | |
26 | |
27 | |
28 | const ARCHIVOS = [ |
29 | "agrega.html", |
30 | "index.html", |
31 | "modifica.html", |
32 | "error/datosnojson.html", |
33 | "error/eliminadoincorrecto.html", |
34 | "error/errorinterno.html", |
35 | "error/faltaid.html", |
36 | "error/faltanombre.html", |
37 | "error/idincorrecto.html", |
38 | "error/modificacionincorrecta.html", |
39 | "error/nombreenblanco.html", |
40 | "error/nombreincorrecto.html", |
41 | "error/resultadonojson.html", |
42 | "js/esperaUnPocoYSincroniza.js", |
43 | "js/registraServiceWorker.js", |
44 | "js/renderiza.js", |
45 | "js/sincroniza.js", |
46 | "js/bd/Bd.js", |
47 | "js/bd/pasatiempoAgrega.js", |
48 | "js/bd/pasatiempoBusca.js", |
49 | "js/bd/pasatiempoConsultaNoEliminados.js", |
50 | "js/bd/pasatiempoConsultaTodos.js", |
51 | "js/bd/pasatiempoElimina.js", |
52 | "js/bd/pasatiempoModifica.js", |
53 | "js/bd/pasatiemposReemplaza.js", |
54 | "js/modelo/PASATIEMPO.js", |
55 | "js/modelo/validaId.js", |
56 | "js/modelo/validaNombre.js", |
57 | "js/modelo/validaPasatiempo.js", |
58 | "js/modelo/validaPasatiempos.js", |
59 | "lib/js/bdConsulta.js", |
60 | "lib/js/bdEjecuta.js", |
61 | "lib/js/consumeJson.js", |
62 | "lib/js/creaIdCliente.js", |
63 | "lib/js/enviaJson.js", |
64 | "lib/js/exportaAHtml.js", |
65 | "lib/js/htmlentities.js", |
66 | "lib/js/muestraError.js", |
67 | "lib/js/muestraObjeto.js", |
68 | "lib/js/ProblemDetails.js", |
69 | "/" |
70 | ] |
71 | |
72 | |
73 | if (self instanceof ServiceWorkerGlobalScope) { |
74 | |
75 | self.addEventListener("install", |
76 | ( evt) => { |
77 | console.log("El service worker se está instalando.") |
78 | evt.waitUntil(llenaElCache()) |
79 | }) |
80 | |
81 | |
82 | self.addEventListener("fetch", ( evt) => { |
83 | if (evt.request.method === "GET") { |
84 | evt.respondWith(buscaLaRespuestaEnElCache(evt)) |
85 | } |
86 | }) |
87 | |
88 | |
89 | self.addEventListener("activate", |
90 | () => console.log("El service worker está activo.")) |
91 | } |
92 | |
93 | async function llenaElCache() { |
94 | console.log("Intentando cargar caché:", CACHE) |
95 | |
96 | const keys = await caches.keys() |
97 | for (const key of keys) { |
98 | await caches.delete(key) |
99 | } |
100 | |
101 | const cache = await caches.open(CACHE) |
102 | |
103 | await cache.addAll(ARCHIVOS) |
104 | console.log("Cache cargado:", CACHE) |
105 | console.log("Versión:", VERSION) |
106 | } |
107 | |
108 | |
109 | async function buscaLaRespuestaEnElCache(evt) { |
110 | |
111 | const cache = await caches.open(CACHE) |
112 | const request = evt.request |
113 | |
114 | |
115 | const response = await cache.match(request, { ignoreSearch: true }) |
116 | if (response === undefined) { |
117 | |
118 | |
119 | return fetch(request) |
120 | } else { |
121 | |
122 | return response |
123 | } |
124 | } |