From 6b55313560dd7819bc9847823c4e1ce7c1d43ccc Mon Sep 17 00:00:00 2001 From: Jack Westbrook Date: Thu, 7 Mar 2024 14:35:17 +0100 Subject: [PATCH] feat(frontend): provide a corsworker for vite --- public/app/core/utils/CorsWorker.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/public/app/core/utils/CorsWorker.ts b/public/app/core/utils/CorsWorker.ts index d8bf41d2aff..6c01343af2c 100644 --- a/public/app/core/utils/CorsWorker.ts +++ b/public/app/core/utils/CorsWorker.ts @@ -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; +}