Small fixes to remove repeated code
This commit is contained in:
parent
042b8b986a
commit
893528a652
1 changed files with 19 additions and 3 deletions
|
|
@ -15,17 +15,26 @@ const fixUrl = (req, url, publicUrl, opt_nokey) => {
|
||||||
if (!url || (typeof url !== 'string') || url.indexOf('local://') !== 0) {
|
if (!url || (typeof url !== 'string') || url.indexOf('local://') !== 0) {
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return url.replace(
|
||||||
|
'local://', utils.getPublicUrl(publicUrl, req)) + getQuery(req, opt_nokey);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// Return the query suffix from the original request
|
||||||
|
const getQuery = (req, opt_nokey) => {
|
||||||
const queryParams = [];
|
const queryParams = [];
|
||||||
if (!opt_nokey && req.query.key) {
|
if (!opt_nokey && req.query.key) {
|
||||||
queryParams.unshift(`key=${encodeURIComponent(req.query.key)}`);
|
queryParams.unshift(`key=${encodeURIComponent(req.query.key)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
let query = '';
|
let query = '';
|
||||||
if (queryParams.length) {
|
if (queryParams.length) {
|
||||||
query = `?${queryParams.join('&')}`;
|
query = `?${queryParams.join('&')}`;
|
||||||
}
|
}
|
||||||
return url.replace(
|
|
||||||
'local://', utils.getPublicUrl(publicUrl, req)) + query;
|
return query;
|
||||||
};
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
init: (options, repo) => {
|
init: (options, repo) => {
|
||||||
|
|
@ -40,6 +49,13 @@ module.exports = {
|
||||||
for (const name of Object.keys(styleJSON_.sources)) {
|
for (const name of Object.keys(styleJSON_.sources)) {
|
||||||
const source = styleJSON_.sources[name];
|
const source = styleJSON_.sources[name];
|
||||||
source.url = fixUrl(req, source.url, item.publicUrl);
|
source.url = fixUrl(req, source.url, item.publicUrl);
|
||||||
|
|
||||||
|
// Apply the query parameters to the tile urls
|
||||||
|
if (source.tiles) {
|
||||||
|
for (let i = 0; i < source.tiles.length; i++) {
|
||||||
|
source.tiles[i] = source.tiles[i] + getQuery(req);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// mapbox-gl-js viewer cannot handle sprite urls with query
|
// mapbox-gl-js viewer cannot handle sprite urls with query
|
||||||
if (styleJSON_.sprite) {
|
if (styleJSON_.sprite) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue