Allowing tile urls to carry the query params from the original request

This commit is contained in:
Mario Freitas 2020-08-10 15:04:58 +02:00
parent 042b8b986a
commit a120435257

View file

@ -24,9 +24,25 @@ const fixUrl = (req, url, publicUrl, opt_nokey) => {
query = `?${queryParams.join('&')}`;
}
return url.replace(
'local://', utils.getPublicUrl(publicUrl, req)) + query;
'local://', utils.getPublicUrl(publicUrl, req)) + + getQuery(req, opt_nokey);
};
// Return the query suffix from the original request
const getQuery = (req, opt_nokey) => {
const queryParams = [];
if (!opt_nokey && req.query.key) {
queryParams.unshift(`key=${encodeURIComponent(req.query.key)}`);
}
let query = '';
if (queryParams.length) {
query = `?${queryParams.join('&')}`;
}
return query;
}
module.exports = {
init: (options, repo) => {
const app = express().disable('x-powered-by');
@ -40,6 +56,13 @@ module.exports = {
for (const name of Object.keys(styleJSON_.sources)) {
const source = styleJSON_.sources[name];
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
if (styleJSON_.sprite) {