Skip to content

fix: surface actionable WorkerScopeError for transpiled reference errors#199

Draft
naorpeled wants to merge 1 commit into
masterfrom
fix/worker-scope-error-133
Draft

fix: surface actionable WorkerScopeError for transpiled reference errors#199
naorpeled wants to merge 1 commit into
masterfrom
fix/worker-scope-error-133

Conversation

@naorpeled

Copy link
Copy Markdown
Collaborator

Problem

Closes #133.

Users hit Uncaught ReferenceError: f is not defined (and similar) in production builds (e.g. Create React App) while the same code works under npm start.

Root cause

createWorkerBlobUrl serializes the passed fn with Function.prototype.toString and runs it inside a worker blob. In production, transpilers/minifiers (Babel, Terser, …) hoist helper functions into module scope and rename them to single letters like f. toString() only captures the function body, so those external helpers are not copied into the worker — the serialized function references identifiers that don't exist there, throwing a cryptic ReferenceError. Dev builds don't minify the same way, so it only appears in production.

This is an inherent limitation of the serialization approach (already noted in the README), but the cryptic error gave users no path forward.

Change

  • src/lib/workerError.ts (new) — pure helpers isTranspileScopeError, buildTranspileScopeErrorMessage, enhanceWorkerError, and a WorkerScopeError class that preserves the original event on .originalEvent.
  • src/useWorker.tsonerror detects this ReferenceError shape, logs an actionable message, and rejects with a WorkerScopeError. Non-matching errors keep the existing behavior (reject with the raw ErrorEvent) — backward compatible.
  • src/index.ts — exports the new helpers/class.
  • README.md — "Known issues" rewritten with the concrete cause, WorkerScopeError handling, remoteDependencies, and the new Function workaround.
  • __tests__/workerError.test.js (new) — 4 node:test cases run against the built dist.
  • Patch changeset added.

Verification

  • pnpm build — clean.
  • node --test __tests__/workerError.test.js — 4/4 pass.
  • pnpm check (biome) — exits 0.

Note

This makes the failure self-explanatory rather than auto-fixing arbitrary user code — external helpers genuinely can't be captured via toString(), so the actual fix remains "keep the function self-contained," which the error now states directly.

🤖 Generated with Claude Code

Workers serialize the passed function via Function.prototype.toString. In
production builds (e.g. CRA) transpilers/minifiers hoist helper functions out
of the function, so the serialized code references identifiers that don't exist
inside the worker, throwing a cryptic "ReferenceError: <x> is not defined".

Detect this case in the worker onerror handler and reject with a WorkerScopeError
that explains the cause and fix, while preserving the original ErrorEvent on
`originalEvent`. Non-matching errors keep the existing behavior.

Closes #133

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@pkg-pr-new

pkg-pr-new Bot commented Jun 6, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@koale/useworker@199

commit: eec193b

@naorpeled naorpeled marked this pull request as draft June 6, 2026 17:34
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.

Uncaught ReferenceError: f is not defined

1 participant