#images - cdn vs local src

4 messages · Page 1 of 1 (latest)

sour pasture
#

how do people generally do their responsive images / CDN for an astro build?

A) pull full image in from CDN -> generate sizes for <Image /> -> store resulting generated images in netlify dist

B) reference image from CDN -> create own srcset etc using CDN's own transforms

lost slateBOT
#

Hi @sour pasture. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Both approaches you mentioned are valid ways to handle responsive images and CDNs in Astro, but the most common and recommended method aligns more closely with option B. Let me explain:

Astro provides built-in support for image optimization and CDNs, which makes it easier to work with responsive images from CDNs. The recommended approach is to use Astro's <Image /> component with a CDN that supports on-the-fly image transformations.

Here's how it typically works:

  1. You reference the image from your CDN using its URL.
  2. You use Astro's <Image /> component, passing the CDN URL as the src.
  3. Astro generates the appropriate srcset and sizes attributes, utilizing the CDN's image transformation capabilities.

This approach is supported by Astro's image service API, which allows for external services (like CDNs) to handle image transformations. As mentioned in the Image Service API documentation:

"External services point to URLs and can add support for services such as Cloudinary, Vercel, or any RIAPI-compliant server."

For example, if you're using Netlify (which has built-in CDN capabilities), Astro provides integration that automatically uses Netlify's Image CDN. As stated in the Netlify integration guide:

"This adapter by default uses the Netlify Image CDN to transform images on-the-fly without impacting build times."

To use this approach, you would typically:

#
  1. Configure your Astro project to use the appropriate image service (e.g., Netlify's Image CDN).
  2. Use the <Image /> component in your Astro files, referencing the CDN URL.
  3. Astro will automatically generate the appropriate srcset and sizes attributes, leveraging the CDN's transformation capabilities.

This method (similar to your option B) is generally preferred because:

  1. It leverages the CDN's capabilities for on-the-fly transformations, reducing build times.
  2. It doesn't require storing multiple versions of each image in your project, saving space.
  3. It allows for more flexible and dynamic image optimization based on the client's needs.

Remember to authorize your CDN's domain in your Astro configuration to enable image optimization for your CDN URLs.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: