#Does Astro optimize images from a CDN

3 messages · Page 1 of 1 (latest)

high warren
ivory mapleBOT
#

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

#

Yes, Astro does support optimization of images from a CDN or any remote source. However, to enable this, you need to configure lists of authorized image source URL domains and patterns for image optimization using image.domains and image.remotePatterns in your Astro configuration file. This is an extra layer of safety to protect your site when showing images from an external source. Here is an example of how to do this:

export default defineConfig({ 
  image: { 
    domains: ["astro.build"], 
  }
});

This configuration will only allow remote images from astro.build to be optimized. If you want to allow remote images from HTTPS hosts, you can use the following configuration:

export default defineConfig({ 
  image: { 
    remotePatterns: [{ protocol: "https" }], 
  }
});

Remember that remote images from other sources will not be optimized, but using the <Image /> component for these images will prevent Cumulative Layout Shift (CLS) ^1^.

Also, note that the <Image /> component optimizes your image and infers width and height (of local images) based on the original aspect ratio to avoid CLS. You should use the HTML <img> element when you cannot use the <Image /> component, for example, for unsupported image formats, when you do not want your image optimized by Astro, or to access and change the src attribute dynamically client-side ^2^.


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