Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Responsive Images #124

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions library/foundations/scripts/save-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ const findAssetSizeAndDimensions = async (url: string) => {
.then((response) => {
console.info('downloaded. converting to webp');
return sharp(Buffer.from(response.data))
.resize({
// max width for now
width: 800,
height: 800,
fit: 'inside',
withoutEnlargement: true,
})
.webp()
.toBuffer({ resolveWithObject: true })
.catch((e) => {
Expand All @@ -64,6 +71,12 @@ const filterUnique =
return acc;
};

// Promise to delay items in batches, e.g. to avoid upstream rate-limiting
const batchDelay = (requestIndex: number, batchSize = 20) =>
new Promise<void>((resolve) => {
setTimeout(() => resolve(), Math.floor(requestIndex / batchSize) * 20);
});

loadLayersFile(snapshotFileName)
.then(({ layers, order }) => {
return order
Expand All @@ -85,11 +98,13 @@ loadLayersFile(snapshotFileName)
.then((assets) =>
// for every asset, download it and convert it to webp
Promise.all(
assets.map(({ value, ...rest }) =>
findAssetSizeAndDimensions(value).then((metadata) => ({
...rest,
...metadata,
}))
assets.map(({ value, ...rest }, i) =>
batchDelay(i)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

batch asset requests to avoid upstream (likely rate-limiting) timeouts.
Screenshot 2023-07-19 at 10 26 58 AM

.then(() => findAssetSizeAndDimensions(value))
.then((metadata) => ({
...rest,
...metadata,
}))
)
)
)
Expand Down