Engineering Journal
Pdf Processor
Pdf Processor

Error Fix: ReferenceError from a Closure That Was Never a Closure

2026-06-04

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 ContextVariable Scope TypeThrow LocationStacktrace Output
Module-Level HelperUnbound Free Variable (pageWidth)_detectAutoZones line 34Obfuscated (fileUpload.js:83)
Explicit Signature PassParameter (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 pass pageWidth 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 →