Skip to content

fix(loader): auto.js runs as a classic script, so Wix can load it - #409

Merged
Ardnived merged 2 commits into
mainfrom
fix/auto-js-classic-bridge
Nov 15, 2026
Merged

Ardnived merged 2 commits into
mainfrom
fix/auto-js-classic-bridge

Conversation

@Ardnived

Copy link
Copy Markdown
Contributor

Why

A Wix client (sahajayoga.ca, testing at /sahajatlastesting) followed the Install tab and got a blank page with Uncaught SyntaxError: Cannot use import statement outside a module.

Wix's Custom Element writes the Server URL as <script src="https://sahajatlas.com/auto.js?key=…"> in <head>, with no type. Observed in headless Chromium on their live page. auto.js was the ES-module loader, starting with a static import … from "./assets/preload-helper-….js", so as a classic script it never parsed. This is the open question in #152 ("does Wix load it as a module or a classic script?"). That issue was closed without the test, and #349 then documented the Custom Element path as working.

Their setup is otherwise correct: <sahaj-atlas> is on the page at 1366×845, and their Velo router already reaches /api/atlas/wix/route.

What changes

  • auto.js is now a classic-script bridge (src/loader/bridge.ts, 300 B gz). It inserts <script type="module" src="loader.js?<same query>">, copying the host tag's nonce. It works whether the host loads it as a module or a classic script, so no snippet, Install tab, WordPress plugin or WeMeditateWeb change is needed.
  • The loader is emitted as loader.js (unhashed, revalidated like auto.js and embed.js). It finds the host's auto.js tag for its settings and position, which it already did when it ran as a module.
  • New build gate, assert:classic (part of pnpm build, so a bad bridge can't deploy). It runs the built auto.js in a vm context with a stub page:
    • it must compile as a classic script;
    • it must start loader.js with the host's query and nonce, both as a classic script and as a module;
    • it must declare nothing global. The Sentry debug-ID globals are allowlisted, since vite.config.ts already accepts them.
  • tests/smoke/embed.smoke.test.ts runs the same audit on the deployed auto.js, and checks loader.js as the loader. Production smoke covers the credentialed build.
  • docs/embedding.md:
    • type="module" goes from "required" to "keep it", because a classic tag in the body blocks the host page's parsing.
    • The file table gains loader.js, and the caching notes cover all three root files.
    • The troubleshooting rows now cover the two ways this can still fail.

Performance

Measured with an A/B on WeMeditate's /map, a full-page embed visible on load (the worst case for an extra hop). Each figure is the median of 5 runs, in ms:

network / visit extra request widget ready (before → after) host LCP
desktop, first 89 715 → 778 unchanged
desktop, repeat 33 601 → 511 (noise) unchanged
slow 4G + 4× CPU, first 299 4869 → 4889 unchanged
slow 4G + 4× CPU, repeat 252 4641 → 4676 unchanged

The extra request overlaps downloads the page already makes, so time to a working widget barely moves. Embeds below the fold pay nothing visible. I kept loader.js unhashed rather than hashing it to save the repeat-visit revalidation. The A/B above revalidated it on every visit and showed no effect on ready time, and hashing would need a post-hash rewrite of auto.js that interacts with flattenEntryImports and the source maps.

Budgets

  • Loader graph: budget raised from 3.2 to 3.6 KiB. It measures 2.78 KiB uncredentialed (what CI reads) and 3.26 KiB in the offline credentialed build, because the bridge is a chunk of its own with its own debug-ID snippet. scripts/AGENTS.md records the new +0.5 KiB gap.
  • Per-edit selection ceiling: ratcheted from 21 to 22 for assert-classic-bridge.test.ts, which reads the bridge source through repo-source. The hook took 3.3 s for the widest edit and 4.4 s for i18n.ts, median of 3 runs on 11 cores. Both are recorded in docs/testing.md.

Security notes (loader and headers paths touched)

  • The bridge builds the loader URL only from its own tag: new URL('./loader.js', script.src) plus that tag's own query string. It reads no other host input.
  • In module mode it finds that tag with script[src*="auto.js"], the same lookup the loader has used since feat(widget): split the embed into a smart loader and an element bundle #149. A page whose first match is some other vendor's auto.js would already lose its configuration today.
  • It copies the nonce so a nonce-only CSP still admits the inserted module. Everything it can throw is caught and logged with the [sahaj-atlas] prefix.
  • No new origins, so no CSP change. _headers gains one Cache-Control rule for /loader.js, and deploy-headers.test.ts still holds the no-overlap rule.

Test plan

  • .claude/skills/pr-prep/check.sh (lint, typecheck, 2080 unit tests)
  • pnpm build (uncredentialed and offline-credentialed), pnpm size
  • The gate fails the build when the bridge imports ./literals, which creates a shared chunk. It also rejects today's production auto.js ("Cannot use import statement outside a module").
  • The source spec fails when the nonce copy is removed or a binding moves out of the IIFE.
  • embed.smoke.test.ts passes against vite preview of this build and fails against today's sahajatlas.com.
  • This build served in place of sahajatlas.com on the client's live Wix page: Wix's classic tag inserted loader.js as a module, the readiness marker was set, and the map rendered. The embed report and Sentry were blocked, so nothing was recorded against the client.
  • Same on WeMeditate's /map (a type="module" host): boots, with readiness marker.
  • CI smoke against this PR's preview

🤖 Generated with Claude Code

Ardnived and others added 2 commits November 15, 2026 09:34
Wix's Custom Element writes `<script src="…/auto.js?key=…">` into `<head>`
with no `type`, observed on a live client site (sahajayoga.ca). `auto.js`
was the ES-module loader, with a static import of rolldown's preload
helper, so the browser threw "Cannot use import statement outside a
module" and nothing of the widget ran. #152 asked exactly this question
and was closed unanswered; #349 then documented the Custom Element path.

`auto.js` is now a classic-script bridge (`src/loader/bridge.ts`, ~300 B
gz) that inserts the loader as `<script type="module" src="loader.js?…">`.
It works whichever way a host loads it, so the Wix Install tab and every
existing snippet stay as they are. The loader keeps finding the host's
`auto.js` tag for its settings and position, as it did when it ran as a
module.

- `pnpm build` now runs `assert:classic`: the built `auto.js` must compile
  as a classic script, start `loader.js` with the host's query and nonce
  both as a script and as a module, and leave nothing on the global object.
  The embed smoke spec runs the same audit on the deployed bytes.
- `loader.js` is unhashed and revalidated like the other two root files.
- The loader budget moves 3.2 → 3.6 KiB: 2.78 uncredentialed, 3.26 in an
  offline credentialed build, since the bridge is a chunk of its own.
- The per-edit selection ceiling ratchets 21 → 22 for the new spec.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sydevs-bot sydevs-bot added the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Nov 15, 2026
@sydevs-bot

sydevs-bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

🤖 no session running

recent dispatches
  • 16:38Z review-pr · attempt 1 · session · done
  • 16:38Z review-pr · attempt 1 · session

Written by the sydevs dispatcher. Sessions never edit this comment.

@sydevs-bot sydevs-bot moved this from Awaiting to Working in SYDevelopers Technical Roadmap Nov 15, 2026
@Ardnived
Ardnived merged commit 4c8de9a into main Nov 15, 2026
9 of 10 checks passed
@Ardnived
Ardnived deleted the fix/auto-js-classic-bridge branch November 15, 2026 16:40
@sydevs-bot sydevs-bot moved this from Working to Done in SYDevelopers Technical Roadmap Nov 15, 2026
@sydevs-bot sydevs-bot removed the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

2 participants