Choosing between bundler inlining and CORS headers for p5.js asset loading in production
0 reputation · 19 Feb 2020, 12:13 UTC
0 reputation · 19 Feb 2020, 12:13 UTC
When a p5.js sketch loads images or sound files with loadImage() or loadSound() it works without extra configuration while the page is opened via a file:// URL.
In a production deployment served over HTTP/HTTPS the same relative paths trigger CORS errors unless the server sends the appropriate Access‑Control‑Allow‑Origin header or the assets are bundled into the JavaScript payload.
Both approaches satisfy the same constraint — the browser must be allowed to read the binary data — but they differ in cache behavior, build complexity, and runtime bundle size. The decision also depends on whether the project already uses a build step and on the expected frequency of asset updates.
Which approach minimizes ongoing maintenance for a static‑site deployment? How does each method affect cache invalidation when assets change? Are there scenarios where a hybrid strategy (CORS for large media, inlining for small icons) is preferable?
35275 reputation · 19 Feb 2020, 19:46 UTC
For a static-site deployment with same-origin assets, neither CORS nor inlining is required for loadImage() / loadSound(). If assets are served from a different origin than the page, CORS headers minimize ongoing maintenance versus bundler inlining. Inlining is lower maintenance only on restrictive hosts that cannot set headers.
Cache invalidation: CORS-based loading allows independent asset versioning. Inlining forces a JS bundle rebuild and redeploy for any asset change.
A hybrid strategy is preferable when you have many small UI assets and a few large media files: inline small icons under ~10KB as data URLs, serve large textures and audio via CORS-enabled same-origin or CDN URLs.
Confirmed: p5.js loadImage and loadSound use XHR/fetch under the hood. Browsers block reading binary data from a different origin unless the response includes Access-Control-Allow-Origin. Data URLs imported by a bundler are same-origin by definition and require no runtime network request.
Likely: The CORS errors described for relative paths in production usually indicate assets are being requested from a different origin than the page, e.g., a CDN, S3 bucket, or a sub-domain, or the page is being served with a different scheme/host than the asset base. With true same-origin relative paths, CORS is not triggered.
netlify.toml, vercel.json, or S3 CORS rules.| Method | Invalidation unit | Effect |
|---|---|---|
| CORS headers | Individual asset file | Change filename or hash, update reference in sketch. Browser caches assets separately from code. |
| Inlining | JavaScript bundle | Any asset change changes the bundle hash. All users re-download the entire script even if only one icon changed. |
Access-Control-Allow-Origin.One missing diagnostic detail that changes the recommendation: Are the assets served from the same origin as the HTML page in production, or from a different CDN/domain? If same origin, use relative paths with no CORS or inlining. If different origin, CORS headers are the lower-maintenance default.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.