Pdf Processor
Error Fix: ReferenceError from a Closure That Was Never a Closure
TLDR
Executing_detectAutoZones inside pageAssembler.js threw ReferenceError: pageWidth is not defined on every document load because _detectAutoZones was declared at module scope and did not close over local variables inside assemblePage. Because Web Worker thread errors serialize only err.message across postMessage bounds, DevTools stacktraces obfuscate the throw location, pointing to main-thread message handlers instead of the worker source line.
| Execution Context | Variable Scope Type | Throw Location | Stacktrace Output |
|---|---|---|---|
| Module-Level Helper | Unbound Free Variable (pageWidth) | _detectAutoZones line 34 | Obfuscated (fileUpload.js:83) |
| Explicit Signature Pass | Parameter (pageWidth) | None ($100\%$ Execution success) | Clean Document Render |
Technical defect diagnostics & remediation
// DEFECTIVE IMPLEMENTATION: Free variable reference in module-level function
function _detectAutoZones(regions, numCols) {
// ReferenceError: pageWidth is NOT in scope for module-level functions!
const col0 = g.list.filter(r => r.bbox?.x < pageWidth / 2);
}
Because _detectAutoZones was declared outside assemblePage(), pageWidth was an unbound free variable.
Remediation code pattern
Explicitly passpageWidth as a parameter in the function signature:
// REFACTORED IMPLEMENTATION: Explicit parameter signature
export function _detectAutoZones(regions, numCols, pageWidth) {
const col0 = g.list.filter(r => r.bbox?.x < pageWidth / 2);
// ...
}
// Call site inside assemblePage() const autoZones = _detectAutoZones(regions, numCols, pageWidth);
Rule of thumb: Pass page-level context variables explicitly through function signatures, and grep codebases for identifier strings when Web Worker stacktraces are obfuscated.
Read this post in the full Engineering Journal →