Skip to content

Commit 8de1f3c

Browse files
committed
Avoid putting bytes which aren't a valid resource into the cache
Remove assumption that a HTTP 304 Not Modified response can only occur if `forceFromServer` is `false`
1 parent 2d7f644 commit 8de1f3c

File tree

1 file changed

+7
-5
lines changed
  • lib/src/layer/modern_tile_layer/tile_loader/bytes_fetchers/network/fetcher

1 file changed

+7
-5
lines changed

lib/src/layer/modern_tile_layer/tile_loader/bytes_fetchers/network/fetcher/network.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,8 @@ class NetworkBytesFetcher implements SourceBytesFetcher<Iterable<String>> {
304304
);
305305

306306
// Server says nothing's changed - but might return new useful headers
307-
if (!forceFromServer &&
308-
cachedTile != null &&
309-
response.statusCode == HttpStatus.notModified) {
307+
// This should usually only happen when `!forceFromServer`
308+
if (cachedTile != null && response.statusCode == HttpStatus.notModified) {
310309
late final R transformedCacheBytes;
311310
try {
312311
transformedCacheBytes = await transformer(cachedTile.bytes);
@@ -322,10 +321,13 @@ class NetworkBytesFetcher implements SourceBytesFetcher<Iterable<String>> {
322321
}
323322
}
324323

325-
// Server says the image has changed - store it new
324+
// Server says the image has changed
326325
if (response.statusCode == HttpStatus.ok) {
326+
final resource = await transformer(bytes);
327+
// If the transformer fails, the error will be caught by an outer
328+
// try/catch block, and the bytes won't be put to the cache
327329
cachePut(bytes: bytes, headers: response.headers);
328-
return await transformer(bytes);
330+
return resource;
329331
}
330332

331333
// It's likely an error at this point

0 commit comments

Comments
 (0)