diff options
| author | Tomas Bankauskas <tbdizainas@gmail.com> | 2024-04-09 12:40:31 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-09 12:40:31 +0300 |
| commit | 3bebbf00e6f4d33bfacbf38eef73a9ad7f0d9d58 (patch) | |
| tree | 2fc74dae2e0d554325ec634891f5726d063e7529 /src/pages/image-cdn.astro | |
| parent | 4df0e4a754ea5e1fb0d05a4e08a53cd49e9fce16 (diff) | |
Added Image CDN page (#4)
* added image-cdn
* updated image-cdn page, colors, etc
* added Markdown.astro and updated styles
Diffstat (limited to 'src/pages/image-cdn.astro')
| -rw-r--r-- | src/pages/image-cdn.astro | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/src/pages/image-cdn.astro b/src/pages/image-cdn.astro new file mode 100644 index 0000000..aa1b4e6 --- /dev/null +++ b/src/pages/image-cdn.astro @@ -0,0 +1,105 @@ +--- +import { Image } from 'astro:assets'; +import Layout from '../layouts/Layout.astro'; +import Markdown from '../components/Markdown.astro'; +import corgi from './_assets/corgi.jpg'; + +const sampleImageSrc = '/images/corgi.jpg'; + +const astroImageSnippet = ` +When running on Netlify, \`next/image\` is automatically set-up to use Netlify Image CDN for optimized images. + +~~~jsx +import Image from 'next/image'; + +// In your component +<Image src="/images/corgi.jpg" alt="Corgi" /* ... additional props */ /> +~~~ +`; + +const originalVsCdnSnippet = ` +In the code below, a regular \`<img>\` tag is used in both cases for a framework-agnostic example. +Other than using \`next/image\` or rolling your own \`<img>\` tags, you can also use the excellent [unpic-img](https://unpic.pics/). + +~~~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="/.netlify/images?url=images/corgi.jpg&w=640 640w, /.netlify/images?url=images/corgi.jpg&w=1280 1280w, /.netlify/images?url=images/corgi.jpg&w=2048 2048w" + sizes="(max-width: 1024px) 100vw, 1024px" + alt="Corgi" +/> +~~~ +`; +--- + +<Layout title="Image CDN"> + <div class="flex flex-col gap-8 sm:gap-16"> + <section> + <h1 class="mb-0">Image CDN</h1> + </section> + <section> + <h2 class="mb-6 text-2xl font-bold sm:text-3xl">Using built-in Astro <Image /> component</h2> + <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?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash">Alvan Nee</a> on + <a + href="https://unsplash.com/photos/long-coated-white-and-brown-dog-lvFlpqEvuRM?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash" + > + Unsplash + </a> + </figcaption> + </figure> + </section> + <section> + <h2 class="mb-6 text-2xl font-bold sm:text-3xl">Original vs. optimized image: can you tell the difference?</h2> + <Markdown content={originalVsCdnSnippet} class="mb-8" /> + <div class="diff aspect-[3/2] rounded-lg border-2 border-white"> + <div class="diff-item-1"> + <div> + <div class="relative"> + <span class="hidden absolute py-1.5 px-2.5 text-sm rounded-lg bg-neutral-900/70 top-2.5 right-2.5"></span> + <img src={`/.netlify/images?url=${sampleImageSrc}`} alt="Corgi" /> + </div> + </div> + </div> + <div class="diff-item-2"> + <div> + <div class="relative"> + <span class="hidden absolute py-1.5 px-2.5 text-sm rounded-lg bg-neutral-900/70 top-2.5 left-2.5"></span> + <img src={sampleImageSrc} alt="Corgi" /> + </div> + </div> + </div> + <div class="diff-resizer"></div> + </div> + </section> + </div> +</Layout> + +<script> + const images = document.querySelectorAll<HTMLImageElement>('.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> |
