From 622fd56619c9026799fbae84443dfcb05642c87b Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Fri, 13 Dec 2024 15:28:36 +0100 Subject: [PATCH] fixup: cache wikipedia calls and be a bit more defensive --- .../umap/js/modules/rendering/template.js | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/umap/static/umap/js/modules/rendering/template.js b/umap/static/umap/js/modules/rendering/template.js index 232080dc1..80f0be0be 100644 --- a/umap/static/umap/js/modules/rendering/template.js +++ b/umap/static/umap/js/modules/rendering/template.js @@ -243,11 +243,11 @@ class OSM extends PopupTemplate { } } +const _WIKIPEDIA_CACHE = {} + class Wikipedia extends PopupTemplate { - async renderBody(feature) { - const body = document.createElement('div') - const wikipedia = feature.properties.wikipedia - if (!wikipedia) return 'No data' + async callWikipedia(wikipedia) { + if (wikipedia && _WIKIPEDIA_CACHE[wikipedia]) return _WIKIPEDIA_CACHE[wikipedia] // Wikipedia value should be in form of "{locale}:{title}", according to https://wiki.openstreetmap.org/wiki/Key:wikipedia const [locale, page] = wikipedia.split(':') const url = `https://${locale}.wikipedia.org/w/api.php?action=query&format=json&origin=*&pithumbsize=500&prop=extracts|pageimages&titles=${page}` @@ -255,15 +255,29 @@ class Wikipedia extends PopupTemplate { const response = await request.get(url) if (response?.ok) { const data = await response.json() + _WIKIPEDIA_CACHE[wikipedia] = data + return data + } + } + + async renderBody(feature) { + const body = document.createElement('div') + const wikipedia = feature.properties.wikipedia + if (!wikipedia) return '' + const data = await this.callWikipedia(wikipedia) + if (data) { const page = Object.values(data.query.pages)[0] - const title = page.title - const extract = page.extract - const thumbnail = page.thumbnail.source - body.appendChild( - Utils.loadTemplate( - `

${title}

${extract}
` - ) + const title = page.title || feature.getDisplayName() + const extract = page.extract || '' + const thumbnail = page.thumbnail?.source + const [content, { image }] = Utils.loadTemplateWithRefs( + `

${title}

${extract}
` ) + if (thumbnail) { + image.src = thumbnail + image.hidden = false + } + body.appendChild(content) } return body }