PageSourceSearch

https://rustrepo.com/sw.js

js rustrepo.com collected 2026-10-04 02:08:39 UTC 3,178 bytes, 109 lines download raw bytes

1/**
2 * RustRepo Service Worker
3 * Provides offline support and caching for PWA functionality
4 */
5
6const CACHE_NAME = 'rustrepo-v1';
7const OFFLINE_URL = '/';
8
9// Assets to cache on install
10const STATIC_ASSETS = [
11    '/',
12    '/icon.svg',
13    '/icon-192.png',
14    '/icon-512.png',
15    '/manifest.json',
16];
17
18// Install event - cache static assets
19self.addEventListener('install', (event) => {
20    event.waitUntil(
21        caches.open(CACHE_NAME).then((cache) => {
22            console.log('[SW] Caching static assets');
23            return cache.addAll(STATIC_ASSETS);
24        })
25    );
26    // Activate immediately
27    self.skipWaiting();
28});
29
30// Activate event - clean up old caches
31self.addEventListener('activate', (event) => {
32    event.waitUntil(
33        caches.keys().then((cacheNames) => {
34            return Promise.all(
35                cacheNames
36                    .filter((name) => name !== CACHE_NAME)
37                    .map((name) => {
38                        console.log('[SW] Deleting old cache:', name);
39                        return caches.delete(name);
40                    })
41            );
42        })
43    );
44    // Take control of all pages immediately
45    self.clients.claim();
46});
47
48// Fetch event - network first, fallback to cache
49self.addEventListener('fetch', (event) => {
50    // Skip non-GET requests
51    if (event.request.method !== 'GET') {
52        return;
53    }
54
55    // Skip cross-origin requests
56    if (!event.request.url.startsWith(self.location.origin)) {
57        return;
58    }
59
60    // Skip API requests and other dynamic content
61    const url = new URL(event.request.url);
62    if (url.pathname.startsWith('/api/') || url.pathname.startsWith('/_next/')) {
63        return;
64    }
65
66    event.respondWith(
67        // Try network first
68        fetch(event.request)
69            .then((response) => {
70                // Clone the response before caching
71                const responseClone = response.clone();
72
73                // Cache successful responses
74                if (response.status === 200) {
75                    caches.open(CACHE_NAME).then((cache) => {
76                        cache.put(event.request, responseClone);
77                    });
78                }
79
80                return response;
81            })
82            .catch(() => {
83                // Fallback to cache on network failure
84                return caches.match(event.request).then((cachedResponse) => {
85                    if (cachedResponse) {
86                        return cachedResponse;
87                    }
88
89                    // For navigation requests, return the offline page
90                    if (event.request.mode === 'navigate') {
91                        return caches.match(OFFLINE_URL);
92                    }
93
94                    // Return a fallback for other requests
95                    return new Response('Offline', {
96                        status: 503,
97                        statusText: 'Service Unavailable',
98                    });
99                });
100            })
101    );
102});
103
104// Handle messages from the main thread
105self.addEventListener('message', (event) => {
106    if (event.data === 'skipWaiting') {
107        self.skipWaiting();
108    }
109});

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.