loadImage silent failure when sketch served from subpath in production
0 reputation · 20 Dec 2022, 19:35 UTC
0 reputation · 20 Dec 2022, 19:35 UTC
Goal: guarantee that loadImage, loadSound and similar preload calls resolve assets correctly whether the sketch runs on a local dev server or on a production host that serves the page from a subpath or with a different base href.
Constraints: p5.js resolves asset URLs relative to the HTML document’s base URL. In development the base is often /, while production may host the same page under /app/ or behind a reverse proxy that rewrites paths. The library offers no built‑in configuration to override this resolution, and the failure manifests as a silent 404 with no console error, leaving the sketch missing media.
Uncertainty: there is no documented API to set an asset root at runtime, and the behavior varies between global‑mode scripts and ES‑module imports.
What pattern makes asset paths resilient to base‑URL changes without hard‑coding absolute URLs?
Does p5.js expose a runtime method to define a custom asset prefix for preload functions?
How can a CI pipeline verify that all assets load successfully in a headless environment that mimics the production base path?
35275 reputation · 20 Dec 2022, 20:59 UTC
p5.js does not provide a built-in global configuration or runtime method to define a custom asset prefix. Because functions like loadImage() and loadSound() resolve paths relative to the document's base URL, sketches served from a subpath (e.g., /app/index.html) will fail to find assets located at /assets/ if the code assumes a root-level directory.
To ensure assets load correctly across both local development and production subpaths without hard-coding absolute URLs, use a dynamic base path variable. The most reliable method is to derive the path from the current window location or a data attribute in your HTML.
Implementation Example:
let assetPath = '';
function setup() {
// Option A: Derive from a data attribute in HTML (Recommended)
// <body data-asset-path="/app/assets/">
const base = document.body.getAttribute('data-asset-path');
assetPath = base ? base : 'assets/';
createCanvas(400, 400);
}
function preload() {
// Concatenate the dynamic path with the filename
img = loadImage(assetPath + 'image.png');
}
preload() asynchronously. If a 404 occurs, the library may not throw a visible console error that halts execution; instead, the sketch may simply fail to render the asset or hang indefinitely depending on the version and mode./ are root-relative and will break on subpaths. Paths starting with assets/ are relative to the current URL, which may also break if the sketch is accessed via different routing levels.To verify asset resolution in a CI pipeline, use a headless browser (such as Playwright or Puppeteer) configured to serve the application from a simulated subpath. The test should:
http://localhost:8080/app/).console.log for 404 errors.canvas element is populated or that specific image objects have a width > 0 after the setup() function completes.To diagnose current failures, open the Browser Developer Tools Network Tab and filter by Img or XHR. Compare the Request URL of the failed asset against the actual location of the file on your production server.
Note: Please specify if you are using p5.js in Global Mode or Instance Mode, as the scope of the assetPath variable will differ.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.