Changes: - Renamed from "Text Corruptor" to "GlitchCraft" - New slogan: "Artisanal text corruption, served fresh!" - Updated server to support both IPv4 and IPv6 (dual-stack) - Server now listens on all interfaces (::) instead of just localhost - Updated all references in code and documentation The new name reflects both the glitch aesthetic and the craftsmanship of creating beautifully corrupted text, with a playful tone. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
90 lines
2.8 KiB
JavaScript
90 lines
2.8 KiB
JavaScript
/**
|
|
* Service Worker for GlitchCraft PWA
|
|
* Enables offline functionality and caching
|
|
*/
|
|
|
|
const CACHE_NAME = 'glitchcraft-v1';
|
|
const urlsToCache = ['/', '/index.html', '/styles.css', '/zalgo.js', '/app.js', '/manifest.json'];
|
|
|
|
// Install event - cache essential files
|
|
self.addEventListener('install', event => {
|
|
event.waitUntil(
|
|
caches
|
|
.open(CACHE_NAME)
|
|
.then(cache => {
|
|
console.log('Opened cache');
|
|
return cache.addAll(urlsToCache);
|
|
})
|
|
.then(() => {
|
|
// Force the service worker to become active immediately
|
|
return self.skipWaiting();
|
|
})
|
|
);
|
|
});
|
|
|
|
// Fetch event - serve from cache when offline
|
|
self.addEventListener('fetch', event => {
|
|
event.respondWith(
|
|
caches.match(event.request).then(response => {
|
|
// Cache hit - return response
|
|
if (response) {
|
|
return response;
|
|
}
|
|
|
|
// Clone the request because it's a stream
|
|
const fetchRequest = event.request.clone();
|
|
|
|
return fetch(fetchRequest)
|
|
.then(response => {
|
|
// Check if we received a valid response
|
|
if (!response || response.status !== 200 || response.type !== 'basic') {
|
|
return response;
|
|
}
|
|
|
|
// Clone the response because it's a stream
|
|
const responseToCache = response.clone();
|
|
|
|
caches.open(CACHE_NAME).then(cache => {
|
|
cache.put(event.request, responseToCache);
|
|
});
|
|
|
|
return response;
|
|
})
|
|
.catch(() => {
|
|
// Network request failed, serve offline fallback if available
|
|
return caches.match('/index.html');
|
|
});
|
|
})
|
|
);
|
|
});
|
|
|
|
// Activate event - clean up old caches
|
|
self.addEventListener('activate', event => {
|
|
const cacheWhitelist = [CACHE_NAME];
|
|
|
|
event.waitUntil(
|
|
caches
|
|
.keys()
|
|
.then(cacheNames => {
|
|
return Promise.all(
|
|
cacheNames.map(cacheName => {
|
|
if (cacheWhitelist.indexOf(cacheName) === -1) {
|
|
console.log('Deleting old cache:', cacheName);
|
|
return caches.delete(cacheName);
|
|
}
|
|
})
|
|
);
|
|
})
|
|
.then(() => {
|
|
// Ensure the service worker takes control immediately
|
|
return self.clients.claim();
|
|
})
|
|
);
|
|
});
|
|
|
|
// Handle messages from the client
|
|
self.addEventListener('message', event => {
|
|
if (event.data && event.data.type === 'SKIP_WAITING') {
|
|
self.skipWaiting();
|
|
}
|
|
});
|