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 rm(publicPhotoDir, { recursive: true, force: true });
|
||||
await mkdir(publicPhotoDir, { 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 = [];
|
||||
|
||||
for (const entry of catalog) {
|
||||
|
|
@ -192,8 +199,10 @@ const generate = async () => {
|
|||
const variants = outputWidths.map((width) => {
|
||||
const height = Math.round((width / metadata.width) * metadata.height);
|
||||
const filename = `${entry.id}-${hash}-${width}.${format.extension}`;
|
||||
expectedFiles.add(filename);
|
||||
|
||||
return {
|
||||
filename,
|
||||
outputPath: path.join(publicPhotoDir, filename),
|
||||
src: `static/photos/${filename}`,
|
||||
width,
|
||||
|
|
@ -202,7 +211,9 @@ const generate = async () => {
|
|||
});
|
||||
|
||||
await Promise.all(
|
||||
variants.map(async ({ outputPath, width }) => {
|
||||
variants
|
||||
.filter(({ filename }) => !existingFiles.has(filename))
|
||||
.map(async ({ outputPath, width }) => {
|
||||
const pipeline = sharp(input).rotate().resize({
|
||||
width,
|
||||
withoutEnlargement: true,
|
||||
|
|
@ -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 portraits = photos
|
||||
.filter((photo) => photo.orientation === 'portrait')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue