Skip to content

Comments

Make workerUrl fetch bundler-agnostic#12

Open
stylessh wants to merge 3 commits intomacaly:mainfrom
stylessh:fix/turbopack-worker-url
Open

Make workerUrl fetch bundler-agnostic#12
stylessh wants to merge 3 commits intomacaly:mainfrom
stylessh:fix/turbopack-worker-url

Conversation

@stylessh
Copy link

When almostnode's library is built by Vite, new URL('./worker/runtime-worker.ts', import.meta.url) in WorkerRuntime is transformed into new Worker(new URL("/assets/runtime-worker-<hash>.js", import.meta.url), ...) in dist/index.mjs.

Turbopack (and Webpack) statically analyze new URL(..., import.meta.url) at build time and fail when the path is a server-relative /assets/... URL — they can't find the file in the consuming project's asset pipeline.

The fix in practice

Before (breaks Turbopack/Webpack — they try to resolve the asset at build time):

// consumer code
const runtime = await createRuntime(vfs, { useWorker: true, dangerouslyAllowSameOrigin: true });
//                                         ^ internally uses new URL("/assets/runtime-worker-HASH.js", import.meta.url)
//                                           Turbopack: __turbopack_context__.x is not a function

After (works with any bundler):

// app/api/almostnode-worker/route.ts
import { getWorkerContent } from 'almostnode/next';
export async function GET() {
  return new Response(getWorkerContent(), { headers: { 'Content-Type': 'application/javascript' } });
}

// In your component
const runtime = await createRuntime(vfs, {
  useWorker: true,
  dangerouslyAllowSameOrigin: true,
  workerUrl: '/api/almostnode-worker',  // ← bypasses the static analysis entirely
});

The default behavior (no workerUrl) is unchanged — Vite users are unaffected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant