img - cache | works

This commit is contained in:
babayaga
2025-08-26 16:12:08 +02:00
parent 8998614cba
commit 808495f2a0
6 changed files with 25 additions and 20 deletions
+8 -1
View File
@@ -5,13 +5,20 @@ import getImageSources from "./getImageSources.js";
import getProcessedImage from "./getProcessedImage.js";
import getArtDirectedImages from "./getArtDirectedImages.js";
import pMap from "p-map";
// Caching moved to plugin level for proper store population
import { join, parse, relative } from "node:path";
import fs from "node:fs";
const imagesData = new Map();
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// Cache helpers moved to plugin level
function getDefaultImage() {
const cwd = process.cwd();
const filepath = join(cwd, "public", "images", "default.png");
const src = join("/", relative(cwd, filepath));
return { src, base: "default" };
}
export default async function ({
src,
@@ -13,10 +13,6 @@ const { getImageDetails } = await (sharp
export default async function getProcessedImage(src, transformConfigs) {
throwErrorIfUnsupported(src, extname(src).slice(1));
if(src.includes('nature')) {
// debugger
}
let base;
if (src.match("(http://|https://|data:image/).*")) {
@@ -21,11 +21,11 @@ import {
const { fileTypeFromBuffer } = await import("file-type");
const defaults = {
maxRedirects: 10,
retries: 2,
baseDelay: 100,
maxRedirects: 3,
retries: 3,
baseDelay: 250,
maxTotalTime: 5000,
devTimeout: 20000,
devTimeout: 10000,
};
class NetworkError extends Error {
+4 -4
View File
@@ -1,12 +1,12 @@
export default {
"environment": "dev",
"environment": "build",
"isSsrBuild": false,
"projectBase": "",
"publicDir": "C:\\Users\\zx\\Desktop\\polymech\\site-min\\public\\",
"rootDir": "C:\\Users\\zx\\Desktop\\polymech\\site-min\\",
"mode": "dev",
"outDir": "dist",
"assetsDir": "/_astro",
"mode": "production",
"outDir": "C:\\Users\\zx\\Desktop\\polymech\\site-min\\dist\\",
"assetsDir": "_astro",
"sourcemap": false,
"assetFileNames": "/_astro/[name]@[width].[hash][extname]"
}
@@ -44,6 +44,7 @@ export default {
},
"astro:build:done": async function closeBundle() {
console.time('[imagetools] build:done');
const { default: astroViteConfigs } = await import(
// @ts-ignore
"../astroViteConfigs.js"
@@ -96,6 +97,7 @@ export default {
try {
// await retryWithBackoff(async () => {
const start = Date.now();
await saveAndCopyAsset(
hash,
image,
@@ -105,6 +107,8 @@ export default {
assetPath,
isSsrBuild
);
const end = Date.now();
// console.log(`[imagetools] saveAndCopyAsset for ${assetPath} took ${end - start}ms`);
// });
} catch (error) {
console.error(`Failed to process image ${assetPath} after retries:`, error);
@@ -115,6 +119,7 @@ export default {
{ concurrency: 1 }
);
}
console.timeEnd('[imagetools] build:done');
},
},
};
+4 -7
View File
@@ -105,15 +105,12 @@ export default async function load(id) {
image: loadedImage,
config,
type,
});
})
const dataUri = `data:${type};base64,${(
buffer || (await getCachedBuffer(hash, image))
).toString("base64")}`;
store.set(hash, dataUri);
console.log('store.set', hash);
return `export default "${dataUri}"`;
).toString("base64")}`
store.set(hash, dataUri)
return `export default "${dataUri}"`
}
} else {
const sources = await Promise.all(