diff --git a/src/apply.js b/src/apply.js index c90b7888..aeea4a62 100644 --- a/src/apply.js +++ b/src/apply.js @@ -751,7 +751,7 @@ function setupGeoJSONSource(glSource, styleUrl, options) { const data = glSource.data; const sourceOptions = {}; if (typeof data == 'string') { - const geoJsonUrl = normalizeSourceUrl( + const [geoJsonUrl] = normalizeSourceUrl( data, options.accessToken, options.accessTokenParam || 'access_token', diff --git a/src/mapbox.js b/src/mapbox.js index f69929eb..6f8ecccd 100644 --- a/src/mapbox.js +++ b/src/mapbox.js @@ -57,13 +57,15 @@ export function normalizeStyleUrl(url, token) { return `${mapboxBaseUrl}/styles/v1/${style}?&access_token=${token}`; } +const mapboxSubdomains = ['a', 'b', 'c', 'd']; + /** * Turns mapbox:// source URLs into vector tile URL templates. * @param {string} url The source URL. * @param {string} token The access token. * @param {string} tokenParam The access token key. * @param {string} styleUrl The style URL. - * @return {string} A vector tile template. + * @return {Array} A vector tile template. * @private */ export function normalizeSourceUrl(url, token, tokenParam, styleUrl) { @@ -71,17 +73,22 @@ export function normalizeSourceUrl(url, token, tokenParam, styleUrl) { const mapboxPath = getMapboxPath(url); if (!mapboxPath) { if (!token) { - return decodeURI(urlObject.href); + return [decodeURI(urlObject.href)]; } if (!urlObject.searchParams.has(tokenParam)) { urlObject.searchParams.set(tokenParam, token); } - return decodeURI(urlObject.href); + return [decodeURI(urlObject.href)]; } if (mapboxPath === 'mapbox.satellite') { const sizeFactor = window.devicePixelRatio >= 1.5 ? '@2x' : ''; - return `https://api.mapbox.com/v4/${mapboxPath}/{z}/{x}/{y}${sizeFactor}.webp?access_token=${token}`; + return [ + `https://api.mapbox.com/v4/${mapboxPath}/{z}/{x}/{y}${sizeFactor}.webp?access_token=${token}`, + ]; } - return `https://{a-d}.tiles.mapbox.com/v4/${mapboxPath}/{z}/{x}/{y}.vector.pbf?access_token=${token}`; + return mapboxSubdomains.map( + (sub) => + `https://${sub}.tiles.mapbox.com/v4/${mapboxPath}/{z}/{x}/{y}.vector.pbf?access_token=${token}`, + ); } diff --git a/src/util.js b/src/util.js index d3cc210b..fbb2cf52 100644 --- a/src/util.js +++ b/src/util.js @@ -1,6 +1,5 @@ import TileState from 'ol/TileState.js'; import {VectorTile} from 'ol'; -import {expandUrl} from 'ol/tileurlfunction.js'; import {getUid} from 'ol/util.js'; import {normalizeSourceUrl, normalizeStyleUrl} from './mapbox.js'; import {toPromise} from 'ol/functions.js'; @@ -239,7 +238,7 @@ export function getTileJson(glSource, styleUrl, options = {}) { promise = Promise.resolve({ tileJson: Object.assign({}, glSource, { url: undefined, - tiles: expandUrl(normalizedSourceUrl), + tiles: normalizedSourceUrl, }), tileLoadFunction, }); @@ -247,7 +246,7 @@ export function getTileJson(glSource, styleUrl, options = {}) { const metadata = {}; promise = fetchResource( 'Source', - normalizedSourceUrl, + normalizedSourceUrl[0], options, metadata, ).then(function (tileJson) { @@ -260,7 +259,7 @@ export function getTileJson(glSource, styleUrl, options = {}) { options.accessToken, options.accessTokenParam || 'access_token', metadata.request.url, - ); + )[0]; }); return Promise.resolve({tileJson, tileLoadFunction}); }); @@ -276,7 +275,7 @@ export function getTileJson(glSource, styleUrl, options = {}) { options.accessToken, options.accessTokenParam || 'access_token', styleUrl || location.href, - ); + )[0]; }), }); promise = Promise.resolve({ diff --git a/test/mapbox.test.js b/test/mapbox.test.js index 54e35d50..bca11062 100644 --- a/test/mapbox.test.js +++ b/test/mapbox.test.js @@ -94,22 +94,32 @@ describe('Mapbox utilities', function () { const cases = [ { url: 'mapbox://mapbox.mapbox-streets-v7', - expected: - 'https://{a-d}.tiles.mapbox.com/v4/mapbox.mapbox-streets-v7/{z}/{x}/{y}.vector.pbf?access_token=test-token', + expected: [ + 'https://a.tiles.mapbox.com/v4/mapbox.mapbox-streets-v7/{z}/{x}/{y}.vector.pbf?access_token=test-token', + 'https://b.tiles.mapbox.com/v4/mapbox.mapbox-streets-v7/{z}/{x}/{y}.vector.pbf?access_token=test-token', + 'https://c.tiles.mapbox.com/v4/mapbox.mapbox-streets-v7/{z}/{x}/{y}.vector.pbf?access_token=test-token', + 'https://d.tiles.mapbox.com/v4/mapbox.mapbox-streets-v7/{z}/{x}/{y}.vector.pbf?access_token=test-token', + ], }, { url: 'https://example.com/source/{z}/{x}/{y}.pbf', - expected: 'https://example.com/source/{z}/{x}/{y}.pbf?token=test-token', + expected: [ + 'https://example.com/source/{z}/{x}/{y}.pbf?token=test-token', + ], }, { url: 'https://example.com/source/{z}/{x}/{y}.pbf?foo=bar', - expected: + expected: [ 'https://example.com/source/{z}/{x}/{y}.pbf?foo=bar&token=test-token', + ], }, { - url: 'https://example.com/source/{z}/{x}/{y}.pbf?token=override-token', - expected: + url: [ 'https://example.com/source/{z}/{x}/{y}.pbf?token=override-token', + ], + expected: [ + 'https://example.com/source/{z}/{x}/{y}.pbf?token=override-token', + ], }, ]; @@ -119,7 +129,7 @@ describe('Mapbox utilities', function () { it(`works for ${c.url}`, () => { should( normalizeSourceUrl(c.url, token, tokenParam, location.href), - ).equal(c.expected); + ).deepEqual(c.expected); }); } });