Fix reloading tiles in case of an error with tile.load()

This commit is contained in:
Andreas Hocevar
2022-07-21 23:18:31 +02:00
parent 1c68d5093d
commit a1d68b4f66
7 changed files with 97 additions and 10 deletions

View File

@@ -112,7 +112,40 @@ class ImageTile extends Tile {
}
/**
* Load not yet loaded URI.
* Load the image or retry if loading previously failed.
* Loading is taken care of by the tile queue, and calling this method is
* only needed for preloading or for reloading in case of an error.
*
* To retry loading tiles on failed requests, use a custom `tileLoadFunction`
* that checks for error status codes and reloads only when the status code is
* 408, 429, 500, 502, 503 and 504, and only when not too many retries have been
* made already:
*
* ```js
* const retryCodes = [408, 429, 500, 502, 503, 504];
* const retries = {};
* source.setTileLoadFunction((tile, src) => {
* const image = tile.getImage();
* fetch(src)
* .then((response) => {
* if (retryCodes.includes(response.status)) {
* retries[src] = (retries[src] || 0) + 1;
* if (retries[src] <= 3) {
* setTimeout(() => tile.load(), retries[src] * 1000);
* }
* return Promise.reject();
* }
* return response.blob();
* })
* .then((blob) => {
* const imageUrl = URL.createObjectURL(blob);
* image.src = imageUrl;
* setTimeout(() => URL.revokeObjectURL(imageUrl), 5000);
* })
* .catch(() => tile.setState(3)); // error
* });
* ```
*
* @api
*/
load() {