blob: 4875c1afbacdf071ee3b3340bdffcdbf895ae9f5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
---
import { Image } from 'astro:assets';
import corgi from '../assets/corgi.jpg';
import ContextAlert from '../components/ContextAlert.astro';
import Diff from '../components/Diff.astro';
import Markdown from '../components/Markdown.astro';
import Layout from '../layouts/Layout.astro';
import { getNetlifyContext } from '../utils';
const sampleImage = '/images/corgi.jpg';
const ctx = getNetlifyContext();
const forceWebP = ctx === 'dev';
const sampleImageSrcSet = [640, 1280, 2048]
.map((size) => {
return `/.netlify/images?url=${sampleImage}&w=${size}${forceWebP ? '&fm=webp' : ''} ${size}w`;
})
.join(', ');
const sizes = '(max-width: 1024px) 100vw, 1024px';
const astroImageSnippet = `
## Using the built-in Astro <Image /> component
Astro's \`Image\` component will automatically use Netlify Image CDN to serve optimized images.
~~~jsx
---
import { Image } from 'astro:assets';
import corgi from '../assets/corgi.jpg';
---
// Later in your markup...
<Image src={corgi} alt="Corgi" /* ... additional props */ />
~~~
`;
const originalVsCdnSnippet = `
## Original vs. optimized image: can you tell the difference?
In the code below, a regular \`<img>\` tag is used in both cases for a framework-agnostic example.
Note that aside from Astro's \`Image\` or rolling your own \`<img>\` tags, you can also use the excellent [unpic-img](https://unpic.pics/) package.
~~~jsx
// <== On the left, the original image
<img src="/images/corgi.jpg" alt="Corgi" />
// ==> On the right, explicitly using Netlify Image CDN endpoint for a responsive image
<img
srcSet="${sampleImageSrcSet}"
sizes="${sizes}"
alt="Corgi"
/>
~~~
`;
const devModeWarning = `
In local development, optimization is performed locally without automatic format
detection, so format is set to WebP.
`;
---
<Layout title="Image CDN">
<div class="flex flex-col gap-12 sm:gap-16">
<section>
<ContextAlert
addedChecksFunction={(ctx) => {
return ctx === 'dev' ? devModeWarning : null;
}}
class="mb-8"
/>
<h1>Image CDN</h1>
</section>
<section>
<Markdown content={astroImageSnippet} class="mb-8" />
<figure>
<Image src={corgi} alt="Corgi" class="border-2 border-white rounded-lg" />
<figcaption class="mt-2 text-sm italic">
Credit: photo by <a href="https://unsplash.com/@alvannee">Alvan Nee</a> on
<a href="https://unsplash.com/photos/long-coated-white-and-brown-dog-lvFlpqEvuRM"> Unsplash </a>
</figcaption>
</figure>
</section>
<section>
<Markdown content={originalVsCdnSnippet} class="mb-8" />
<Diff class="border-2 border-white rounded-lg image-diff aspect-3/2">
<Fragment slot="item-1">
<div>
<div class="relative">
<span class="hidden absolute py-1.5 px-2.5 text-sm rounded-md bg-gray-900/70 top-2.5 left-2.5"></span>
<img src={sampleImage} alt="Corgi" />
</div>
</div>
</Fragment>
<Fragment slot="item-2">
<div>
<div class="relative">
<span class="hidden absolute py-1.5 px-2.5 text-sm rounded-md bg-gray-900/70 top-2.5 right-2.5"></span>
<img srcset={sampleImageSrcSet} alt="Corgi" />
</div>
</div>
</Fragment>
</Diff>
</section>
</div>
</Layout>
<script>
const images = document.querySelectorAll<HTMLImageElement>('.image-diff img');
images.forEach((image) => {
if (image.complete) {
showImageSize(image);
} else {
image.addEventListener('load', () => {
showImageSize(image);
});
}
});
function showImageSize(image: HTMLImageElement) {
const imageEntry = window?.performance?.getEntriesByName(image.currentSrc)?.[0] as PerformanceResourceTiming;
if (imageEntry && imageEntry.encodedBodySize) {
const imageOverlay = image.previousElementSibling;
imageOverlay.classList.remove('hidden');
imageOverlay.textContent = `Size: ${Math.ceil(imageEntry.encodedBodySize / 1024)}KB`;
}
}
</script>
|