Skip to main content

Overview

Astro is designed for performance by default with zero JavaScript shipped to the client unless you explicitly add it. This guide covers techniques to further optimize your site for maximum speed.

Image Optimization

Images are often the largest assets on a webpage. Astro provides built-in image optimization through the <Image> component.

Using the Image Component

src/components/Hero.astro

Benefits of Image Optimization

Automatic Resizing

Images are resized to specified dimensions, reducing file size.

Format Conversion

Convert to modern formats like WebP and AVIF automatically.

Lazy Loading

Images load only when they enter the viewport.

Responsive Images

Serve appropriately sized images based on device and screen size.

Remote Images

Optimize images from external sources:

Content Collections with Images

src/content.config.ts
src/layouts/BlogPost.astro

Prefetching

Prefetch pages before users navigate to them for instant page transitions.

Enabling Prefetch

1

Configure prefetch in astro.config.mjs

astro.config.mjs
2

Add data attributes to links

Prefetch Strategies

Prefetch when the user hovers over a link (default):
Best for: Desktop navigation where hover intent is clear.
Prefetch when the user taps/clicks a link:
Best for: Mobile devices or slow connections.
Prefetch when links enter the viewport:
Best for: Lists of links where users are likely to click visible items.
Prefetch immediately when the page loads:
Best for: Critical next steps in user flows.

Programmatic Prefetching

Code Splitting

Astro automatically code-splits your JavaScript to load only what’s needed.

Client Directives

Control when JavaScript loads for interactive components:

Directive Priority

1

client:load

Highest priority. Load and hydrate immediately on page load. Use for: Critical interactive elements above the fold.
2

client:idle

Load once the page is done with initial load and the requestIdleCallback event has fired. Use for: Non-critical widgets and chat boxes.
3

client:visible

Load once the component enters the user’s viewport. Use for: Below-the-fold interactive content.
4

client:media

Load only when a media query matches. Use for: Mobile-only or desktop-only components.
5

client:only

Skip server-side rendering entirely. Hydrate only on client. Use for: Components that rely heavily on browser APIs.

Asset Optimization

CSS Optimization

Astro automatically:
  • Scopes CSS to prevent conflicts
  • Minifies CSS in production
  • Removes unused CSS
  • Bundles CSS efficiently
src/components/Card.astro

Font Optimization

Optimize web font loading:
src/components/BaseHead.astro

Script Optimization

Optimize third-party scripts:

Build Optimization

Minimize Bundle Size

Audit your dependencies regularly:
Remove packages you’re not using:
Import heavy libraries only when needed:
Configure Vite to optimize specific dependencies:
astro.config.mjs

Caching Strategies

Static Asset Caching

Configure your hosting provider to cache static assets:
For Netlify:
netlify.toml
For Vercel:
vercel.json

Performance Monitoring

Lighthouse Scores

Run Lighthouse audits regularly:

Core Web Vitals

Monitor key metrics:

LCP

Largest Contentful PaintTarget: < 2.5sOptimize images and fonts.

FID

First Input DelayTarget: < 100msMinimize JavaScript.

CLS

Cumulative Layout ShiftTarget: < 0.1Reserve space for images.

Best Practices Checklist

1

Use the Image component

Always use <Image> for local and remote images to get automatic optimization.
2

Enable prefetching

Configure prefetch for smoother navigation between pages.
3

Choose appropriate client directives

Use client:idle or client:visible for non-critical components.
4

Optimize fonts

Preload critical fonts and use font-display: swap.
5

Lazy load below-the-fold content

Use loading="lazy" for images and client:visible for components.
6

Minimize third-party scripts

Defer or dynamically load analytics and other third-party code.
7

Monitor performance

Regularly check Lighthouse scores and Core Web Vitals.

Advanced Optimizations

Deploy to edge networks for faster response times:
astro.config.mjs
Edge rendering reduces latency by serving content from locations close to users.
Only hydrate the interactive parts of your page:
Implement offline support and advanced caching:
public/sw.js