Skip to content

Serve ES modules with a wrapper the browser accepts - #892

Merged
Elimpizza merged 2 commits into
mainfrom
fix/esm-script-rewriting
Nov 16, 2026
Merged

Elimpizza merged 2 commits into
mainfrom
fix/esm-script-rewriting

Conversation

@karenrasmussen

Copy link
Copy Markdown
Contributor

Context

Kanopy rebuilt its frontend and now ships it as an ES module. The page loads through Via but never boots: the browser discards the bundle before running a line of it, leaving Kanopy's loading screen up forever. pywb wraps every script it serves in a block which redeclares window, location and the rest so wombat can proxy them.
A module rejects that wrapper twice over, it declares let arguments, which strict mode forbids, and it encloses the module's export in a block.
pywb has no notion of modules at all: every <script src> maps to the classic JS modifier whatever its type. This is a known pywb bug, open since 2022 (webrecorder/pywb#771 for the strict mode error, #964 for the enclosed export, #764 tracking the fix). 2.10.0 doesn't address it, so upgrading isn't a way out.

Changes

A modifier of our own, esm_, applied to <script type="module"> and <link rel="modulepreload">, and a rewriter which serves what carries it: pywb's own wrapper minus let arguments and minus the block. The block can go because a module's outermost scope is already its own, a classic script's is the global scope, which is why pywb needs one. Dropping it also leaves the export at the top level, where it must be.

@Elimpizza
Elimpizza force-pushed the fix/esm-script-rewriting branch from 7a39db3 to e4ec71d Compare November 12, 2026 15:12
@Elimpizza
Elimpizza merged commit 410f729 into main Nov 16, 2026
5 checks passed
@Elimpizza
Elimpizza deleted the fix/esm-script-rewriting branch November 16, 2026 14:53
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.

2 participants