Skip to content

Commit

Permalink
use non-XHR raster tile loads when possible
Browse files Browse the repository at this point in the history
Addresses #6643.
  • Loading branch information
mourner committed Jul 23, 2018
1 parent e9e08e2 commit 35da2d6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/source/raster_tile_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class RasterTileSource extends Evented implements Source {
tiles: Array<string>;

_loaded: boolean;
_avoidXHR: boolean;
_options: RasterSourceSpecification | RasterDEMSourceSpecification;
_tileJSONRequest: ?Cancelable;

Expand All @@ -52,6 +53,7 @@ class RasterTileSource extends Evented implements Source {
this._loaded = false;

this._options = extend({}, options);
this._avoidXHR = false;
extend(this, pick(options, ['url', 'scheme', 'tileSize']));
}

Expand Down Expand Up @@ -110,6 +112,12 @@ class RasterTileSource extends Evented implements Source {
delete (img: any).cacheControl;
delete (img: any).expires;

// if the tiles have aggressive caching (retained for at least 1 hour),
// switch to faster image loading technique for subsequent tile loads
if (tile.getExpiryTimeout() > 1000 * 60 * 60) {
this._avoidXHR = true;
}

const context = this.map.painter.context;
const gl = context.gl;
tile.texture = this.map.painter.getTileTexture(img.width);
Expand All @@ -128,7 +136,7 @@ class RasterTileSource extends Evented implements Source {

callback(null);
}
});
}, this._avoidXHR);
}

abortTile(tile: Tile, callback: Callback<void>) {
Expand Down
17 changes: 15 additions & 2 deletions src/util/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,21 @@ function sameOrigin(url) {

const transparentPngUrl = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVQYV2NgAAIAAAUAAarVyFEAAAAASUVORK5CYII=';

export const getImage = function(requestParameters: RequestParameters, callback: Callback<HTMLImageElement>): Cancelable {
// request the image with XHR to work around caching issues
export const getImage = function(requestParameters: RequestParameters, callback: Callback<HTMLImageElement>, avoidXHR?: boolean): Cancelable {
// if we know for sure that the tile is cached for a long time, avoid XHR for better performance
// https://github.com/mapbox/mapbox-gl-js/issues/6643
if (avoidXHR && requestParameters.headers === undefined && requestParameters.credentials === undefined) {
const img: HTMLImageElement = new window.Image();
const url = requestParameters.url;
if (!sameOrigin(url)) {
img.crossOrigin = 'Anonymous';
}
img.onload = () => callback(null, img);
img.src = url;
return {cancel: () => { img.src = transparentPngUrl; }};
}

// otherwise request the image with XHR to work around caching issues
// see https://github.com/mapbox/mapbox-gl-js/issues/1470
return getArrayBuffer(requestParameters, (err, imgData) => {
if (err) {
Expand Down

0 comments on commit 35da2d6

Please sign in to comment.