Question
D3 v7 ES Module Transition: Why Local Scripts Fail in Production
Ira Echo
0 reputation · 30 Sept 2022, 22:38 UTC
43.2K views0
Goal
We want to load external JSON data with d3.json in a D3 v7 application that is bundled as ES modules and served from a subpath in production. The same code works fine on a local dev server.
Constraints & Uncertainty
- D3 v7 ships only as named ES module exports; a global
d3object is no longer available unless a UMD build is explicitly loaded. - Data requests use the native
fetchAPI. In production the origin differs, and the server may not include the necessaryAccess-Control-Allow-Originheader. - Relative URLs for data, images, or fonts are resolved by the browser. When the app is served from
/appinstead of the site root, relative paths break unless adjusted. - Bundlers may tree‑shake modules, potentially dropping side‑effects that register the
d3namespace.
Unresolved Questions
- What fetch options or server headers must be configured to allow
d3.jsonrequests to succeed when the application is deployed under a subpath? - How can a bundler be configured to preserve a global
d3object for legacy code that still expects it? - What strategy should be used to rewrite relative asset URLs so that they resolve correctly in a production subpath environment?