Skip to content

Commit 6c4ac00

Browse files
committed
Added auto resizing of the image assets
The images were loading very slowly because they were 1900+ pixels wide but only rendered at 330px. I converted the astro component to load the images from assets, resize them and pass those to the React component.
1 parent 437df53 commit 6c4ac00

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

public/images/aws/elb-load-balancing-featured-image.png renamed to src/assets/images/aws/tutorials/elb-load-balancing-featured-image.png

File renamed without changes.
1.18 MB
Loading
Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,46 @@
11
---
22
import { getCollection } from 'astro:content';
33
import { TutorialsShowcase } from './tutorials/TutorialsShowcase';
4+
import type { ImageMetadata } from 'astro';
5+
import { getImage } from 'astro:assets';
46
57
// Import data for filters
68
import services from '../data/developerhub/services.json';
79
import platforms from '../data/developerhub/platforms.json';
810
import deployments from '../data/developerhub/deployments.json';
911
12+
const images = import.meta.glob<{ default: ImageMetadata }>(
13+
'/src/assets/images/aws/tutorials/*.{jpeg,jpg,png,gif}'
14+
);
15+
1016
const allTutorials = await getCollection('docs', ({ id }) => {
1117
return id.startsWith('aws/tutorials/') && !id.includes('/index');
1218
});
1319
14-
const tutorialData = allTutorials.map(tutorial => {
15-
const title = tutorial.data.title || 'Unknown Tutorial';
16-
const description = tutorial.data.description || `Tutorial: ${title}`;
17-
const slug = tutorial.id; // Use id instead of slug for proper path
18-
19-
return {
20-
title,
21-
description,
22-
slug,
23-
leadimage: tutorial.data.leadimage,
24-
services: tutorial.data.services || [],
25-
platform: tutorial.data.platform || [],
26-
deployment: tutorial.data.deployment || [],
27-
pro: tutorial.data.pro || false,
28-
};
29-
});
20+
const tutorialData = await Promise.all(
21+
allTutorials.map(async (tutorial) => {
22+
const title = tutorial.data.title || 'Unknown Tutorial';
23+
const description = tutorial.data.description || `Tutorial: ${title}`;
24+
const slug = tutorial.id; // Use id instead of slug for proper path
25+
const imagePath = `/src/assets/images/aws/tutorials/${tutorial.data.leadimage}`;
26+
const optimizedLeadImage = await getImage({
27+
src: images[imagePath](),
28+
format: 'png',
29+
width: 330,
30+
});
31+
32+
return {
33+
title,
34+
description,
35+
slug,
36+
leadimage: optimizedLeadImage.src,
37+
services: tutorial.data.services || [],
38+
platform: tutorial.data.platform || [],
39+
deployment: tutorial.data.deployment || [],
40+
pro: tutorial.data.pro || false,
41+
};
42+
})
43+
);
3044
---
3145

3246
<TutorialsShowcase
@@ -35,4 +49,4 @@ const tutorialData = allTutorials.map(tutorial => {
3549
platforms={platforms}
3650
deployments={deployments}
3751
client:load
38-
/>
52+
/>

src/components/tutorials/TutorialsShowcase.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const TutorialCard: React.FC<{
3636
return (
3737
<div className="tutorial-card">
3838
<div className="card-image">
39-
<img src={imagePath} alt={tutorial.title} loading="lazy" />
39+
<img src={tutorial.leadimage} alt={tutorial.title} loading="lazy" />
4040
<div className="card-badges">
4141
{tutorial.pro && <span className="pro-badge">Pro</span>}
4242
</div>

0 commit comments

Comments
 (0)