diff --git a/src/main.js b/src/main.js index 78549b3..2b48bb6 100644 --- a/src/main.js +++ b/src/main.js @@ -168,16 +168,23 @@ const getNoxyUrl = (type, url, id, relay) => { return link; } -function linkPreview(href, id, relay) { - if ((/\.(gif|jpe?g|png)$/i).test(href)) { - return elem('div', {}, - [elem('img', {className: 'preview-image-only', loading: 'lazy', src: getNoxyUrl('data', href, id, relay).href})] - ); - } +const fetchQue = []; +let fetchPending; +const fetchNext = (href, id, relay) => { const noxy = getNoxyUrl('meta', href, id, relay); const previewId = noxy.searchParams.toString(); - fetch(noxy.href) - .then(data => data.json ? data.json() : data) + if (fetchPending) { + fetchQue.push({href, id, relay}); + return previewId; + } + fetchPending = fetch(noxy.href) + .then(data => { + if (data.status === 200) { + return data.json(); + } + // fetchQue.push({href, id, relay}); // could try one more time + return Promise.reject(data); + }) .then(meta => { const container = document.getElementById(previewId); container.append(elem('a', {href, rel: 'noopener noreferrer', target: '_blank'}, [ @@ -189,7 +196,24 @@ function linkPreview(href, id, relay) { ])); container.classList.add('preview-loaded'); }) + .finally(() => { + fetchPending = false; + if (fetchQue.length) { + const {href, id, relay} = fetchQue.shift(); + return fetchNext(href, id, relay); + } + }) .catch(console.warn); + return previewId; +}; + +function linkPreview(href, id, relay) { + if ((/\.(gif|jpe?g|png)$/i).test(href)) { + return elem('div', {}, + [elem('img', {className: 'preview-image-only', loading: 'lazy', src: getNoxyUrl('data', href, id, relay).href})] + ); + } + const previewId = fetchNext(href, id, relay); return elem('div', { className: 'preview', id: previewId