PageSourceSearch

https://gocdkeys.es/sw-push.js

js gocdkeys.es collected 2026-10-08 04:28:36 UTC 3,784 bytes, 93 lines download raw bytes

1/**
2 * Service worker de notificaciones push.  [2026-08-02 — Fase 3 auditoría SEO]
3 *
4 * Repone la captación, muerta desde 2024: el código de suscripción vivía solo en el
5 * legacy (new.gocdkeys.com) y al migrar al frontend Laravel se quedó atrás.
6 *
7 * ⚠️ COMPATIBILIDAD DE PAYLOAD: el SW del legacy leía el destino de `data.link`.
8 * El emisor (backend `webpush:send`) manda `url` Y `link` con el mismo valor, para
9 * que sirvan a la vez los suscriptores viejos (SW legacy) y los nuevos (este). Aquí
10 * se acepta cualquiera de las dos.
11 *
12 * Este worker NO cachea nada ni intercepta fetch: solo notificaciones. Meterle una
13 * capa de caché offline afectaría a todas las páginas y no es lo que se ha pedido.
14 */
15
16self.addEventListener('push', function (event) {
17    var data = {};
18    try {
19        if (event.data) {
20            data = event.data.json();
21        }
22    } catch (e) {
23        data = {};
24    }
25
26    var title = data.title || 'Gocdkeys';
27    var dest = data.url || data.link || '/';
28
29    event.waitUntil(
30        self.registration.showNotification(title, {
31            body: data.body || '',
32            icon: data.icon || '/images/favicons/110110.png',
33            badge: data.badge || '/images/favicons/110110.png',
34            image: data.image || undefined,
35            lang: data.lang || 'en',
36            // `tag` agrupa: una notificación nueva del mismo tema reemplaza a la
37            // anterior en vez de apilarse. Evita la bandeja llena de avisos viejos.
38            tag: data.tag || 'gocdkeys',
39            renotify: false,
40            data: { url: dest }
41        })
42    );
43});
44
45self.addEventListener('notificationclick', function (event) {
46    event.notification.close();
47
48    var dest = (event.notification.data && event.notification.data.url) || '/';
49
50    event.waitUntil(
51        clients.matchAll({ type: 'window', includeUncontrolled: true }).then(function (list) {
52            // Si ya hay una pestaña del sitio abierta, se reutiliza y se navega en
53            // ella. Abrir una pestaña nueva por cada clic es lo que hacía el worker
54            // viejo y llena el navegador.
55            for (var i = 0; i < list.length; i++) {
56                var c = list[i];
57                if (c.url.indexOf(self.location.origin) === 0 && 'focus' in c) {
58                    if ('navigate' in c) {
59                        return c.navigate(dest).then(function (nc) {
60                            return nc ? nc.focus() : c.focus();
61                        }).catch(function () {
62                            return c.focus();
63                        });
64                    }
65                    return c.focus();
66                }
67            }
68            if (clients.openWindow) {
69                return clients.openWindow(dest);
70            }
71        })
72    );
73});
74
75// Si el navegador caduca la suscripción, se renueva sola con la MISMA clave pública
76// y se reenvía al servidor. Sin esto, las suscripciones se van perdiendo en silencio
77// (parte de por qué la lista actual está tan envejecida).
78self.addEventListener('pushsubscriptionchange', function (event) {
79    event.waitUntil(
80        self.registration.pushManager.subscribe({
81            userVisibleOnly: true,
82            applicationServerKey: (event.oldSubscription && event.oldSubscription.options)
83                ? event.oldSubscription.options.applicationServerKey
84                : undefined
85        }).then(function (sub) {
86            return fetch('/webpush/subscribe', {
87                method: 'POST',
88                headers: { 'Content-Type': 'application/json' },
89                body: JSON.stringify({ subscription: sub.toJSON(), renewed: true })
90            });
91        }).catch(function () { /* silencio: se reintentará en la próxima visita */ })
92    );
93});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.