From e9bb16936e9e0108dd7420c9f1735eafeb32a6ae Mon Sep 17 00:00:00 2001 From: acalcutt Date: Tue, 23 Apr 2024 12:58:07 -0400 Subject: [PATCH] fix: don't force multi-sprites in style Signed-off-by: acalcutt --- src/serve_rendered.js | 3 +++ src/serve_style.js | 38 +++++++++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/serve_rendered.js b/src/serve_rendered.js index 6696e14..c6953e7 100644 --- a/src/serve_rendered.js +++ b/src/serve_rendered.js @@ -1046,6 +1046,9 @@ export const serve_rendered = { } if (styleJSON.sprite) { + if (!Array.isArray(styleJSON.sprite)) { + styleJSON.sprite = [{ id: 'default', url: styleJSON.sprite }]; + } styleJSON.sprite.forEach((spriteItem) => { if (!httpTester.test(spriteItem.url)) { spriteItem.url = diff --git a/src/serve_style.js b/src/serve_style.js index 1742e11..7de4f06 100644 --- a/src/serve_style.js +++ b/src/serve_style.js @@ -61,9 +61,13 @@ export const serve_style = { } // mapbox-gl-js viewer cannot handle sprite urls with query if (styleJSON_.sprite) { - styleJSON_.sprite.forEach((spriteItem) => { - spriteItem.url = fixUrl(req, spriteItem.url, item.publicUrl); - }); + if (Array.isArray(styleJSON_.sprite)) { + styleJSON_.sprite.forEach((spriteItem) => { + spriteItem.url = fixUrl(req, spriteItem.url, item.publicUrl); + }); + } else { + styleJSON_.sprite = fixUrl(req, styleJSON_.sprite, item.publicUrl); + } } if (styleJSON_.glyphs) { styleJSON_.glyphs = fixUrl(req, styleJSON_.glyphs, item.publicUrl); @@ -173,22 +177,34 @@ export const serve_style = { let spritePaths = []; if (styleJSON.sprite) { if (!Array.isArray(styleJSON.sprite)) { - styleJSON.sprite = [{ id: 'default', url: styleJSON.sprite }]; - } - - for (let spriteItem of styleJSON.sprite) { - if (!httpTester.test(spriteItem.url)) { + if (!httpTester.test(styleJSON.sprite)) { let spritePath = path.join( options.paths.sprites, - spriteItem.url + styleJSON.sprite .replace('{style}', path.basename(styleFile, '.json')) .replace( '{styleJsonFolder}', path.relative(options.paths.sprites, path.dirname(styleFile)), ), ); - spriteItem.url = `local://styles/${id}/sprite/` + spriteItem.id; - spritePaths.push({ id: spriteItem.id, path: spritePath }); + styleJSON.sprite = `local://styles/${id}/sprite`; + spritePaths.push({ id: 'default', path: spritePath }); + } + } else { + for (let spriteItem of styleJSON.sprite) { + if (!httpTester.test(spriteItem.url)) { + let spritePath = path.join( + options.paths.sprites, + spriteItem.url + .replace('{style}', path.basename(styleFile, '.json')) + .replace( + '{styleJsonFolder}', + path.relative(options.paths.sprites, path.dirname(styleFile)), + ), + ); + spriteItem.url = `local://styles/${id}/sprite/` + spriteItem.id; + spritePaths.push({ id: spriteItem.id, path: spritePath }); + } } } }