fix: surface actionable WorkerScopeError for transpiled reference errors#199
Draft
naorpeled wants to merge 1 commit into
Draft
fix: surface actionable WorkerScopeError for transpiled reference errors#199naorpeled wants to merge 1 commit into
naorpeled wants to merge 1 commit into
Conversation
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>
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 undernpm start.Root cause
createWorkerBlobUrlserializes the passedfnwithFunction.prototype.toStringand runs it inside a worker blob. In production, transpilers/minifiers (Babel, Terser, …) hoist helper functions into module scope and rename them to single letters likef.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 crypticReferenceError. 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 helpersisTranspileScopeError,buildTranspileScopeErrorMessage,enhanceWorkerError, and aWorkerScopeErrorclass that preserves the original event on.originalEvent.src/useWorker.ts—onerrordetects thisReferenceErrorshape, logs an actionable message, and rejects with aWorkerScopeError. Non-matching errors keep the existing behavior (reject with the rawErrorEvent) — backward compatible.src/index.ts— exports the new helpers/class.README.md— "Known issues" rewritten with the concrete cause,WorkerScopeErrorhandling,remoteDependencies, and thenew Functionworkaround.__tests__/workerError.test.js(new) — 4node:testcases run against the built dist.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