From e6d425f994f159dd74843b2789f55f408f10fc2b Mon Sep 17 00:00:00 2001 From: Praful Bhawsar Date: Tue, 23 Jan 2024 10:27:20 -0500 Subject: [PATCH] Handle case where cache-control causes CORS issues --- imagebox3.js | 11 +++++++---- imagebox3.mjs | 11 +++++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/imagebox3.js b/imagebox3.js index 2f8b206..e0661cb 100644 --- a/imagebox3.js +++ b/imagebox3.js @@ -211,14 +211,14 @@ var imagebox3 = (() => { return response } - const getImagesInPyramid = async (imageID) => { + const getImagesInPyramid = async (imageID, cache=true) => { // Get all images in the pyramid. tiff[imageID] = tiff[imageID] || {} try { - - tiff[imageID].pyramid = tiff[imageID].pyramid || ( await GeoTIFF.fromUrl(imageID, { headers: {'Cache-Control': "no-cache, no-store"}}) ) + const headers = cache ? { headers: {'Cache-Control': "no-cache, no-store"}} : {} + tiff[imageID].pyramid = tiff[imageID].pyramid || ( await GeoTIFF.fromUrl(imageID, headers) ) const imageCount = await tiff[imageID].pyramid.getImageCount() if (tiff[imageID].pyramid.loadedCount !== imageCount) { @@ -242,7 +242,10 @@ var imagebox3 = (() => { } } catch (e) { - console.error("Couldn't get images", e) + console.error("Couldn't get images", e) + if (cache) { // Retry in case Cache-Control is not part of Access-Control-Allow-Headers in preflight response + await getImagesInPyramid(imageID, !cache) + } } return } diff --git a/imagebox3.mjs b/imagebox3.mjs index 9f1000e..57eaf69 100644 --- a/imagebox3.mjs +++ b/imagebox3.mjs @@ -190,14 +190,14 @@ const imagebox3 = (() => { return response } - const getImagesInPyramid = async (imageID) => { + const getImagesInPyramid = async (imageID, cache=true) => { // Get all images in the pyramid. tiff[imageID] = tiff[imageID] || {} try { - - tiff[imageID].pyramid = tiff[imageID].pyramid || ( await fromUrl(imageID, { headers: {'Cache-Control': "no-cache, no-store"}}) ) + const headers = cache ? { headers: {'Cache-Control': "no-cache, no-store"}} : {} + tiff[imageID].pyramid = tiff[imageID].pyramid || ( await fromUrl(imageID, headers) ) const imageCount = await tiff[imageID].pyramid.getImageCount() if (tiff[imageID].pyramid.loadedCount !== imageCount) { @@ -221,7 +221,10 @@ const imagebox3 = (() => { } } catch (e) { - console.error("Couldn't get images", e) + console.error("Couldn't get images", e) + if (cache) { // Retry in case Cache-Control is not part of Access-Control-Allow-Headers in preflight response + await getImagesInPyramid(imageID, !cache) + } } return }