From 710420a414be3731b127c13b4c6cbb0028c57422 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 7 Jan 2024 20:49:36 +0100 Subject: [PATCH] viewer: switch to modern fetch API, do not cache metadata and entity files --- viewer/MinedMap.js | 106 +++++++++++++++++++++------------------------ 1 file changed, 49 insertions(+), 57 deletions(-) diff --git a/viewer/MinedMap.js b/viewer/MinedMap.js index 856578b..2eaca22 100644 --- a/viewer/MinedMap.js +++ b/viewer/MinedMap.js @@ -284,76 +284,71 @@ function createSign(sign, back) { return wrapper; } -function loadSigns(signLayer) { - const xhr = new XMLHttpRequest(); - xhr.onload = function () { - const res = JSON.parse(this.responseText); - const groups = {}; - - // Group signs by x,z coordinates - for (const sign of res.signs) { - const key = coordKey([sign.x, sign.z]); - const group = groups[key] ??= []; - group.push(sign); - } +async function loadSigns(signLayer) { + const response = await fetch('data/entities.json', {cache: 'no-store'}); + const res = await response.json(); - for (const [key, group] of Object.entries(groups)) { - const el = document.createElement('div'); + const groups = {}; - let material; - let kind; + // Group signs by x,z coordinates + for (const sign of res.signs) { + const key = coordKey([sign.x, sign.z]); + const group = groups[key] ??= []; + group.push(sign); + } - // Sort from top to bottom - group.sort((a, b) => b.y - a.y); + for (const [key, group] of Object.entries(groups)) { + const el = document.createElement('div'); - for (const sign of group) { - el.appendChild(createSign(sign, false)); + let material; + let kind; - if (sign.back_text) - el.appendChild(createSign(sign, true)); + // Sort from top to bottom + group.sort((a, b) => b.y - a.y); - material ??= sign.material; - kind ??= sign.kind; - } + for (const sign of group) { + el.appendChild(createSign(sign, false)); - // Default material - material ??= 'oak'; + if (sign.back_text) + el.appendChild(createSign(sign, true)); - const [x, z] = key.split(',').map((i) => +i); + material ??= sign.material; + kind ??= sign.kind; + } - const popup = L.popup().setContent(el); + // Default material + material ??= 'oak'; - popup.on('add', () => { - params.marker = [x, z]; - updateHash(); - }); - popup.on('remove', () => { - params.marker = null; - updateHash(); - }); + const [x, z] = key.split(',').map((i) => +i); - const marker = L.marker([-z-0.5, x+0.5], { - icon: signIcon(material, kind), - }).addTo(signLayer).bindPopup(popup); + const popup = L.popup().setContent(el); - markers[coordKey([x, z])] = marker; + popup.on('add', () => { + params.marker = [x, z]; + updateHash(); + }); + popup.on('remove', () => { + params.marker = null; + updateHash(); + }); - if (params.marker && x === params.marker[0] && z === params.marker[1]) - marker.openPopup(); - } - } + const marker = L.marker([-z-0.5, x+0.5], { + icon: signIcon(material, kind), + }).addTo(signLayer).bindPopup(popup); + + markers[coordKey([x, z])] = marker; - xhr.open('GET', 'data/entities.json', true); - xhr.send(); + if (params.marker && x === params.marker[0] && z === params.marker[1]) + marker.openPopup(); + } } window.createMap = function () { - const xhr = new XMLHttpRequest(); - xhr.onload = function () { - const res = JSON.parse(this.responseText), - mipmaps = res.mipmaps, - spawn = res.spawn, - features = res.features || {}; + (async function () { + const response = await fetch('data/info.json', {cache: 'no-store'}); + const res = await response.json(); + const {mipmaps, spawn} = res; + const features = res.features || {}; const updateParams = function () { const args = parseHash(); @@ -497,8 +492,5 @@ window.createMap = function () { updateHash(); }; - }; - - xhr.open('GET', 'data/info.json', true); - xhr.send(); + })(); }