skip to content

A tax-filing web app loads its bundles from a separate static domain, and every error report reads only 'Script error.'; why, and what must change?

level: middleimportance: should knowfreq 42%

answer

  1. a privacy rule, not a bug
  2. muted errors for cross-origin scripts
  3. two halves: page and server
  4. header first, attribute second
  5. lazy chunks need it too

basics

~20 s

The bundles are cross-origin scripts fetched without CORS, so the browser mutes their errors to 'Script error.' with no file, line or error object. Add crossorigin="anonymous" to every script tag and have the static domain send Access-Control-Allow-Origin.

solid answer

~50 s

A classic script whose response is cross-origin and not CORS-approved has **muted errors**: the error event carries the message `Script error.`, an empty file name, line and column 0 and a null `error`, so there is no stack to map or group. It is a privacy rule; otherwise any page could learn details of scripts it embeds from other origins. The fix has two halves that only work together. The page requests every bundle, including lazily injected chunks, with `crossorigin="anonymous"`, which makes the fetch a CORS request without credentials. The static domain answers with `Access-Control-Allow-Origin` naming the app's origin, or `*` for public, credential-free bundles, plus `Vary: Origin` if it echoes the origin. The header alone changes nothing, because the request was never a CORS request; the attribute alone makes the request fail its CORS check, which blocks the script. So ship the header first, then the attribute.

code

http · 9 lines
http
GET /assets/filing-step.4f9c2a.js HTTP/1.1
Host: static.filing.example
Origin: https://app.filing.example

HTTP/1.1 200 OK
Content-Type: text/javascript
Access-Control-Allow-Origin: https://app.filing.example
Vary: Origin
Cache-Control: public, max-age=31536000, immutable

go deeper

for a junior

Recall that errors from scripts on another origin arrive as 'Script error.' unless the script was loaded with CORS, and that fixing it needs a page change and a server change.

for a middle

Explain muted errors, the crossorigin attribute's anonymous mode, the Access-Control-Allow-Origin header, and why either half alone fails, one of them by blocking the script.

for a senior

Plan the rollout: header first and verified on every path and cached copy, then the attribute including runtime-injected chunks, then watch the share of muted reports and chunk-load failures.

for a principal

Weigh moving bundles to the app's own origin against keeping a separate asset domain, and set the policy for third-party scripts whose errors the team will never see in full.

## What the browser hides, and why A **cross-origin script** is one loaded from a different origin (scheme, host and port) than the page. The tax-filing app serves its HTML from the app's own domain and its JavaScript bundles from a **separate static domain** in front of a content delivery network, which is common for caching and for keeping cookies off asset requests. By default a `<script>` tag fetches its file without CORS (Cross-Origin Resource Sharing, the protocol by which a server opts in to sharing a response with another origin). The HTML standard records whether the response was CORS-approved; when it was not, the script is created with **muted errors**. Any error it throws is reported to the window `error` event with: - the message replaced by `Script error.`; - an empty file name, and line and column set to 0; - the `error` object set to `null`, so there is no stack. The script still runs normally: the HTML standard notes that whether the response is CORS-approved "only affects how error reporting happens". The reason is privacy. Without muting, a page could embed a script from another site and read details out of its error messages, which may reveal data that site meant to keep private. For the tax app's own team the effect is that the error pipeline receives thousands of identical, stackless reports that cannot be symbolicated, grouped or traced to a release. ## The two halves of the fix | Page sends `crossorigin` | Static domain sends CORS header | Result | |---|---|---| | no | no | runs; errors muted to `Script error.` | | no | yes | runs; errors still muted, because the request was not a CORS request | | yes | no | the CORS check fails and the script is not run: worse than muted | | yes | yes | runs; full message, file, line, column and stack | - **Page side.** Each script tag carries `crossorigin="anonymous"` (an empty `crossorigin` means the same). `anonymous` sends no cookies or credentials to another origin, which suits public bundles. That includes chunks the app injects at runtime: the bundler's runtime loader must be configured to set the attribute on every script element it creates, or lazily loaded code stays muted. - **Server side.** The static domain answers with `Access-Control-Allow-Origin`. For credential-free public assets, `*` is acceptable; if the CDN echoes the requesting origin instead, it must also send `Vary: Origin` so a cached response for one origin is not served to another. ## Rolling it out without breaking the filing flow Order matters because the attribute without the header blocks the app: 1. **Add the header on the static domain first.** It is harmless to the current non-CORS requests. Purge or re-version cached objects so every cached copy carries it. 2. **Verify from the app's origin** that every bundle and chunk path returns the header, including error responses and old releases still referenced by open tabs. 3. **Add the attribute** to the HTML's script tags and to the runtime chunk loader's configuration, in a normal weekly release. 4. **Watch the pipeline**: the share of `Script error.` reports should fall to the residue from scripts the team does not control, and chunk-load failures should not rise. A jump in load failures means some path is missing the header. ## Alternatives and what stays muted - **Serve bundles from the app's own origin**, for example by routing a path such as `/assets/` through the same CDN. Same-origin scripts are never muted, and the problem disappears; the cost is giving up a separate cookie-free asset domain and reworking the CDN set-up. - **Third-party scripts** (a payment widget, a support chat) live on origins the team does not control and stay muted unless their owners send CORS headers. Group them into one "third-party, muted" issue that never pages anyone, so they do not drown the app's own errors. - **Inline scripts** in the HTML are same-origin and never muted. ## The interview point Candidates who answer "add `crossorigin`" have half the fix. The answer that lands names both halves, says which order to ship them in, remembers the runtime-injected chunks, and explains that muting exists to protect users, not to annoy developers.

  • Why ship the CORS header before the crossorigin attribute, rather than both in one release?
    The attribute turns each script request into a CORS request. If any bundle path, cached copy or old release still lacks the header, that request fails its check and the script does not run, so the app breaks for those users. The header alone changes nothing visible. Shipping it first, verifying every path, and only then adding the attribute keeps the filing flow working at every step.
  • The app also embeds a third-party payment widget whose errors read 'Script error.'; what can the team do?
    Very little on its own: the widget's origin decides whether to send CORS headers. The team can ask the widget's owner to send them and load the script with the attribute, but until then it should group the widget's muted errors into one issue that never pages anyone, and watch the payment step's own failures, which the app's code does see.

It is like asking another office for a full incident report: they only send it if you ask in writing and they have signed off on sharing it with you. Ask without their sign-off and nothing arrives; get sign-off without asking and you still receive only the one-line summary.

  • Adding crossorigin to the script tags alone fixes 'Script error.'.
  • The CORS header on the static domain is enough; the tags need no change.
  • 'Script error.' means the bundle is broken and should be rebuilt.
  • Muting is a bug in the browser rather than a deliberate privacy rule.
  • Only the entry script needs the attribute; lazily loaded chunks inherit it.