FIx timeout
This commit is contained in:
parent
2e2b7dc850
commit
3193c509a7
1 changed files with 35 additions and 14 deletions
|
|
@ -169,10 +169,17 @@ const generate = async () => {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
await assertCatalogMatchesFiles(catalog);
|
await assertCatalogMatchesFiles(catalog);
|
||||||
await rm(publicPhotoDir, { recursive: true, force: true });
|
|
||||||
await mkdir(publicPhotoDir, { recursive: true });
|
await mkdir(publicPhotoDir, { recursive: true });
|
||||||
await mkdir(generatedDir, { recursive: true });
|
await mkdir(generatedDir, { recursive: true });
|
||||||
|
|
||||||
|
// Variant filenames embed a hash of their source image, so any file that
|
||||||
|
// already exists is already up to date. Skip re-encoding those and prune
|
||||||
|
// only the ones no longer referenced. Regenerating every variant from
|
||||||
|
// scratch takes minutes and would blow the Playwright preview server's
|
||||||
|
// startup timeout.
|
||||||
|
const existingFiles = new Set(await readdir(publicPhotoDir));
|
||||||
|
const expectedFiles = new Set();
|
||||||
|
|
||||||
const photos = [];
|
const photos = [];
|
||||||
|
|
||||||
for (const entry of catalog) {
|
for (const entry of catalog) {
|
||||||
|
|
@ -192,8 +199,10 @@ const generate = async () => {
|
||||||
const variants = outputWidths.map((width) => {
|
const variants = outputWidths.map((width) => {
|
||||||
const height = Math.round((width / metadata.width) * metadata.height);
|
const height = Math.round((width / metadata.width) * metadata.height);
|
||||||
const filename = `${entry.id}-${hash}-${width}.${format.extension}`;
|
const filename = `${entry.id}-${hash}-${width}.${format.extension}`;
|
||||||
|
expectedFiles.add(filename);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
filename,
|
||||||
outputPath: path.join(publicPhotoDir, filename),
|
outputPath: path.join(publicPhotoDir, filename),
|
||||||
src: `static/photos/${filename}`,
|
src: `static/photos/${filename}`,
|
||||||
width,
|
width,
|
||||||
|
|
@ -202,20 +211,22 @@ const generate = async () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
variants.map(async ({ outputPath, width }) => {
|
variants
|
||||||
const pipeline = sharp(input).rotate().resize({
|
.filter(({ filename }) => !existingFiles.has(filename))
|
||||||
width,
|
.map(async ({ outputPath, width }) => {
|
||||||
withoutEnlargement: true,
|
const pipeline = sharp(input).rotate().resize({
|
||||||
});
|
width,
|
||||||
|
withoutEnlargement: true,
|
||||||
|
});
|
||||||
|
|
||||||
if (format.extension === 'avif') {
|
if (format.extension === 'avif') {
|
||||||
await pipeline.avif(format.options).toFile(outputPath);
|
await pipeline.avif(format.options).toFile(outputPath);
|
||||||
} else if (format.extension === 'webp') {
|
} else if (format.extension === 'webp') {
|
||||||
await pipeline.webp(format.options).toFile(outputPath);
|
await pipeline.webp(format.options).toFile(outputPath);
|
||||||
} else {
|
} else {
|
||||||
await pipeline.jpeg(format.options).toFile(outputPath);
|
await pipeline.jpeg(format.options).toFile(outputPath);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
sources.push({
|
sources.push({
|
||||||
|
|
@ -249,6 +260,16 @@ const generate = async () => {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Drop variants that are no longer referenced: deleted photos, or sources
|
||||||
|
// whose content changed (and so produced a new hash in their filename).
|
||||||
|
await Promise.all(
|
||||||
|
[...existingFiles]
|
||||||
|
.filter((filename) => !expectedFiles.has(filename))
|
||||||
|
.map((filename) =>
|
||||||
|
rm(path.join(publicPhotoDir, filename), { force: true })
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
const firstPhoto = photos[0];
|
const firstPhoto = photos[0];
|
||||||
const portraits = photos
|
const portraits = photos
|
||||||
.filter((photo) => photo.orientation === 'portrait')
|
.filter((photo) => photo.orientation === 'portrait')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue