From 2cd3ea3e8783f1fd249f886048778b50e1f3c504 Mon Sep 17 00:00:00 2001 From: Myles Scolnick Date: Fri, 15 Mar 2024 16:17:29 -0400 Subject: [PATCH] fix: try to load worker from file, and fallback to inline (#959) --- frontend/src/core/pyodide/bridge.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/frontend/src/core/pyodide/bridge.ts b/frontend/src/core/pyodide/bridge.ts index 3ae40e64b63..f067450e56c 100644 --- a/frontend/src/core/pyodide/bridge.ts +++ b/frontend/src/core/pyodide/bridge.ts @@ -55,10 +55,24 @@ export class PyodideBridge implements RunRequests, EditRequests { constructor() { if (isPyodide()) { - // Create a worker - const worker = new InlineWorker({ - name: getMarimoVersion(), - }); + // Create a worker, try by URL then inline + let worker: Worker; + const version = getMarimoVersion(); + if (window.Worker) { + try { + worker = new Worker(new URL("worker/worker", import.meta.url), { + type: "module", + /* @vite-ignore */ + name: version, + }); + } catch { + Logger.debug("Failed to create worker by URL, trying inline."); + worker = new InlineWorker({ name: version }); + } + } else { + Logger.debug("Worker not available, using inline worker."); + worker = new InlineWorker({ name: version }); + } // Create the RPC this.rpc = getWorkerRPC(worker);