Browse Source

FileLoader: Fix error response bodies being cached as results. Fixes #17635.

Until now, the response body was saved to the cache, no matter what the HTTP
status code was.

So a request that returned e.g. HTTP 404 with response body
"No such file" and `onError` being called would, if run a second time,
be found in the cache with "No such file" as the result, and
`onLoad("No such file")` would be called, which is nonsensical.

One common failure was that texture loaders would thus interpret
plain error strings as textures to decode, and failed.
Niklas Hambüchen 5 years ago
parent
commit
6718fd7427
1 changed files with 4 additions and 2 deletions
  1. 4 2
      src/loaders/FileLoader.js

+ 4 - 2
src/loaders/FileLoader.js

@@ -172,8 +172,6 @@ FileLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
 
 				var response = this.response;
 
-				Cache.add( url, response );
-
 				var callbacks = loading[ url ];
 
 				delete loading[ url ];
@@ -185,6 +183,10 @@ FileLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
 
 					if ( this.status === 0 ) console.warn( 'THREE.FileLoader: HTTP Status 0 received.' );
 
+					// Add to cache only on HTTP success, so that we do not cache
+					// error response bodies as proper responses to requests.
+					Cache.add( url, response );
+
 					for ( var i = 0, il = callbacks.length; i < il; i ++ ) {
 
 						var callback = callbacks[ i ];