Skip to content

Add fetch() polyfill over XMLHttpRequest for Playground#1707

Open
bkaradzic-microsoft wants to merge 1 commit into
BabylonJS:masterfrom
bkaradzic-microsoft:weekend/tpc-1581-fetch-shim
Open

Add fetch() polyfill over XMLHttpRequest for Playground#1707
bkaradzic-microsoft wants to merge 1 commit into
BabylonJS:masterfrom
bkaradzic-microsoft:weekend/tpc-1581-fetch-shim

Conversation

@bkaradzic-microsoft
Copy link
Copy Markdown
Contributor

@bkaradzic-microsoft bkaradzic-microsoft commented May 18, 2026

Adds a JS polyfill for fetch so playgrounds depending on it stop hitting ReferenceError: 'fetch' is not defined.

Changes:

  • New Apps/Playground/Scripts/fetch_polyfill.js - full fetch(input, init) wrapping XMLHttpRequest with Response-like text()/arrayBuffer()/json()/blob(). Self-detecting (no-op if global fetch already exists). Tailored to BN's XHR which dispatches only via addEventListener and fires loadend (no load). Decodes ArrayBuffer for .text() when responseType is arraybuffer.
  • Apps/Playground/CMakeLists.txt - adds fetch_polyfill.js to SCRIPTS list.
  • Apps/Playground/Shared/AppContext.cpp - loads fetch_polyfill.js before file_polyfill.js.

Note: Polyfill resolves the headline error but re-enables 0 tests. 5 affected playground tests still excluded - they hit secondary blockers (4 MSDF tests need babylonjs-addons, idx 151 needs earcut). Tracked as follow-ups.

Verified: regression sweep clean on idx 0/5/10/47/100/165/200.

Landing context

This PR is one of 7 splits from the proven CI-green combined preview in draft PR #1702 (see #1702 for the full intended end-state and verified CI run 26044922430).

Note: the original split included an 8th PR (#1709, ES2020+ -> ES2019 syntax-repair polyfill for Chakra). It was closed in favour of investigating @babel/standalone properly (#1711).

Recommended landing order

Tier 1 - parallel-reviewable, no source conflicts:

  1. Fix ExternalTexture_OpenGL throw-stubs to avoid MSVC C4702 under /WX #1703 - ExternalTexture C4702 build fix
  2. Document accurate root cause for post-#1695 pixel-diff fallouts #1704 - config.json reason rewrites (5 entries)
  3. Document accurate root cause for 17 subtle pixel-diff tests #1705 - config.json reason rewrites (17 entries)

Tier 2 - sequential, each touches Apps/Playground/CMakeLists.txt SCRIPTS list + Apps/Playground/Shared/AppContext.cpp LoadScript order; rebase the next branch after the previous merges:

  1. Add File/Blob/FileReader polyfill for Playground (re-enables 19 GLTF tests) #1706 - File/Blob/FileReader polyfill (largest test impact: 19 re-enables)
  2. Add fetch() polyfill over XMLHttpRequest for Playground #1707 - fetch polyfill
  3. Add DOM globals polyfill + native AbortController for Playground #1708 - DOM globals + native AbortController + Android CMake link
  4. Add cubemap auto-expand polyfill for Playground (re-enables 7 PBR tests) #1710 - Cubemap auto-expand polyfill (loaded after babylon.max.js)

Reference policy reminder

Reference PNGs across all 7 PRs come from Babylon.js; never re-baked by BN. Combined diff: 0 PNGs.

Implements a minimal `fetch(input, init)` polyfill in Apps/Playground that
wraps XMLHttpRequest. Provides a Response-like result with
`.ok`, `.status`, `.statusText`, `.url`, `.text()`, `.arrayBuffer()`,
`.json()`, `.blob()`, and a `headers` stub. Self-detecting: no-op if a
global `fetch` already exists; bails if XMLHttpRequest is unavailable.

Wired into the Playground SCRIPTS list and loaded by AppContext before
`ammo.js` / `babylon.max.js` so playground snippets that use the modern
`fetch` API can run on Babylon Native's host environments (Chakra and V8),
which do not provide `fetch` by default.

Babylon Native's `XMLHttpRequest` only dispatches events through
`addEventListener` (no `onload`/`onerror` properties) and fires
`loadend` rather than `load`; the polyfill listens on `loadend` and
inspects `status` to decide resolve/reject. When `responseType` is
`arraybuffer`, BN's `XHR.responseText` is empty, so `.text()` decodes
the array buffer directly rather than reading `responseText`.
Copy link
Copy Markdown
Member

@ryantrem ryantrem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar question as the other PR - should we just add a full native fetch polyfill? Maybe we could add it to XMLHttpRequest (@bghgary might say no though 😅), or a new polyfill. It would be good to have this in BN anyway so the BJS codebase can use fetch rather than XMLHttpRequest.

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