feat(frontend): provide a corsworker for vite

This commit is contained in:
Jack Westbrook
2025-03-03 09:43:30 +01:00
parent 06d4038d4f
commit 6b55313560
+12
View File
@@ -19,3 +19,15 @@ export class CorsWorker extends window.Worker {
URL.revokeObjectURL(objectURL);
}
}
// Vite equivalent of the above CorsWorker to allow loading workers from a different origin
export function WorkaroundWorker(workerUrl: string, options: WorkerOptions) {
const js = `import ${JSON.stringify(new URL(workerUrl, import.meta.url))}`;
const blob = new Blob([js], { type: 'application/javascript' });
const objURL = URL.createObjectURL(blob);
const worker = new Worker(objURL, { type: 'module', name: options?.name });
worker.addEventListener('error', (e) => {
URL.revokeObjectURL(objURL);
});
return worker;
}