fixing data export in chrome and firefox
This commit is contained in:
80
public/sw.js
80
public/sw.js
@@ -1,4 +1,4 @@
|
||||
const CACHE_NAME = 'w0bm-pwa-v8';
|
||||
const CACHE_NAME = 'w0bm-pwa-v9';
|
||||
const ASSETS_TO_CACHE = [
|
||||
'/',
|
||||
'/s/css/f0ckm.css',
|
||||
@@ -6,6 +6,10 @@ const ASSETS_TO_CACHE = [
|
||||
'/s/img/favicon.png'
|
||||
];
|
||||
|
||||
// Stream bridge for memory-efficient exports
|
||||
const streamControllers = new Map();
|
||||
const streamQueues = new Map();
|
||||
|
||||
self.addEventListener('install', (event) => {
|
||||
console.log('[SW] Installing v8...');
|
||||
event.waitUntil(
|
||||
@@ -38,6 +42,47 @@ self.addEventListener('fetch', (event) => {
|
||||
const url = new URL(event.request.url);
|
||||
if (url.origin !== self.location.origin) return;
|
||||
|
||||
// Handle streaming export downloads
|
||||
if (url.pathname === '/api/v2/export/stream') {
|
||||
const streamId = url.searchParams.get('id');
|
||||
const fileName = url.searchParams.get('filename') || 'export.zip';
|
||||
|
||||
const stream = new ReadableStream({
|
||||
start(controller) {
|
||||
streamControllers.set(streamId, controller);
|
||||
// Flush any chunks that arrived before the fetch was set up
|
||||
const queue = streamQueues.get(streamId) || [];
|
||||
streamQueues.delete(streamId);
|
||||
for (const item of queue) {
|
||||
try {
|
||||
if (item.chunk) controller.enqueue(item.chunk);
|
||||
if (item.done) { controller.close(); streamControllers.delete(streamId); }
|
||||
if (item.port) item.port.postMessage({ type: 'ACK' });
|
||||
} catch (e) {
|
||||
if (item.port) item.port.postMessage({ type: 'ERROR', error: e.message });
|
||||
}
|
||||
}
|
||||
},
|
||||
cancel() {
|
||||
streamControllers.delete(streamId);
|
||||
streamQueues.delete(streamId);
|
||||
}
|
||||
});
|
||||
|
||||
event.respondWith(new Response(stream, {
|
||||
headers: {
|
||||
'Content-Type': 'application/zip',
|
||||
'Content-Disposition': `attachment; filename="${fileName}"`,
|
||||
'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0',
|
||||
'Pragma': 'no-cache',
|
||||
'Expires': '0',
|
||||
'Accept-Ranges': 'none',
|
||||
'X-Content-Type-Options': 'nosniff'
|
||||
}
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
if (url.pathname === '/') {
|
||||
event.respondWith(
|
||||
fetch(event.request)
|
||||
@@ -61,3 +106,36 @@ self.addEventListener('fetch', (event) => {
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
self.addEventListener('message', (event) => {
|
||||
if (event.data && event.data.type === 'CLAIM') {
|
||||
self.clients.claim();
|
||||
return;
|
||||
}
|
||||
if (event.data && event.data.type === 'EXPORT_CHUNK') {
|
||||
const { id, chunk, done } = event.data;
|
||||
const controller = streamControllers.get(id);
|
||||
|
||||
if (controller) {
|
||||
try {
|
||||
if (chunk) controller.enqueue(chunk);
|
||||
if (done) {
|
||||
controller.close();
|
||||
streamControllers.delete(id);
|
||||
streamQueues.delete(id);
|
||||
}
|
||||
if (event.ports && event.ports[0]) event.ports[0].postMessage({ type: 'ACK' });
|
||||
} catch (e) {
|
||||
console.error('[SW] Stream error:', e);
|
||||
streamControllers.delete(id);
|
||||
if (event.ports && event.ports[0]) event.ports[0].postMessage({ type: 'ERROR', error: e.message });
|
||||
}
|
||||
} else {
|
||||
// Controller not ready yet — queue the chunk for when the fetch arrives
|
||||
if (!streamQueues.has(id)) streamQueues.set(id, []);
|
||||
// Transfer port out of event so we can reply after controller is set
|
||||
const port = (event.ports && event.ports[0]) || null;
|
||||
streamQueues.get(id).push({ chunk, done, port });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user