Return null from loadTexture when image fails to load instead of wrongly creating a VideoTexture#5781
Conversation
…ly creating a VideoTexture
src/utils/src-loader.js
Outdated
| checkIsImageFallback(src, onResult); | ||
| // Non-success status (404, etc.) - resource not found. | ||
| onResult(null); |
There was a problem hiding this comment.
At this stage it's too early to conclude the resource would fail loading, as the status code might be in the 3XX range (redirects) or the server might not support HEAD requests (405). In those cases its still possible to load the image through the image tag.
There was a problem hiding this comment.
Good point. I made changes.
There was a problem hiding this comment.
The change was also wrong, assuming an error loading via the image tag meant not found. I reverted all that and instead check for common image extensions before doing a HEAD request. I had a similar patch in an aframe 1.6.0 project to not spam the server with HEAD requests because I used different images urls on a plane at regular interval like a slideshow.
Now the promise rejection happens on the ImageLoader error callback.
Description:
Previously, when an image failed to load (404),
validateSrcinsrc-loader.jswould fall back to treating the URL as a video, resulting inloadTexturereturning aVideoTextureinstead of indicating the error. (You got insteadcomponents:texture:warn './rainbow.jpg' is not a valid video). This made it difficult for callers to detect failed image loads.Example in the community component media-image where I checked
texture.image.tagName === "VIDEO"to know if the image was not found:https://github.com/c-frame/aframe-gltf-model-plus/blob/6269b0499fcf497097d55375c4f10a70ae85d4c1/src/components/media-image.js#L68-L76
Changes proposed:
This change properly handles loading errors by:
checkIsImageto check for common image extensions before doing a HEAD requestloadTextureSourceto reject the promise when resource is not foundloadTextureto callcb(null)when source loading failsloadImageUrlwhen image request fails. Also fixed a typo in the error message format string ($s → %s)..then(cb, onRejected)instead of.then(cb).catch(onRejected)to avoid catching errors thrown by the callback itselfPR done with the help of Claude Code with Opus 4.5