From 34413a1944b3f091a104b8b32e1177fa7b95abae Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 17 Feb 2026 11:14:03 +0000 Subject: [PATCH] Optimize remote image downloads with request coalescing Prevents redundant downloads of the same remote image when referenced by multiple pages concurrently during the build process. Implements a `pendingDownloads` map to cache in-flight requests and return the same promise for subsequent calls. This reduces network bandwidth and speeds up the build for sites with repeated remote images. Co-authored-by: si <18108+si@users.noreply.github.com> --- .../eleventy-plugin-local-images/.eleventy.js | 49 ++++++++++++------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/third_party/eleventy-plugin-local-images/.eleventy.js b/third_party/eleventy-plugin-local-images/.eleventy.js index c9ab055..92dc080 100644 --- a/third_party/eleventy-plugin-local-images/.eleventy.js +++ b/third_party/eleventy-plugin-local-images/.eleventy.js @@ -8,30 +8,43 @@ const metadata = require("../../_data/metadata.json"); let config = { distPath: "_site", verbose: false, attribute: "src" }; +const pendingDownloads = new Map(); + const downloadImage = async (url) => { if (config.verbose) { console.log("eleventy-plugin-local-images: Attempting to copy " + url); } - try { - const imgBuffer = await fetch(url, { redirect: "follow" }) - .then((res) => { - if (!res.ok) { - console.warn( - `[local-images] Skipping remote image ${url} — HTTP ${res.status}` - ); - return null; - } else if (res.status == 200) { - return res; - } else { - throw new Error(`File "${url}" not found`); - } - }) - .then((res) => res.buffer()); - return imgBuffer; - } catch (error) { - console.log(error); + if (pendingDownloads.has(url)) { + return pendingDownloads.get(url); } + + const downloadPromise = (async () => { + try { + const imgBuffer = await fetch(url, { redirect: "follow" }) + .then((res) => { + if (!res.ok) { + console.warn( + `[local-images] Skipping remote image ${url} — HTTP ${res.status}` + ); + return null; + } else if (res.status == 200) { + return res; + } else { + throw new Error(`File "${url}" not found`); + } + }) + .then((res) => (res ? res.buffer() : null)); + return imgBuffer; + } catch (error) { + console.log(error); + } finally { + pendingDownloads.delete(url); + } + })(); + + pendingDownloads.set(url, downloadPromise); + return downloadPromise; }; const getFileType = (filename, buffer) => {