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
Prefetch Strategies
Hover Strategy
Hover Strategy
Prefetch when the user hovers over a link (default):Best for: Desktop navigation where hover intent is clear.
Tap Strategy
Tap Strategy
Prefetch when the user taps/clicks a link:Best for: Mobile devices or slow connections.
Viewport Strategy
Viewport Strategy
Prefetch when links enter the viewport:Best for: Lists of links where users are likely to click visible items.
Load Strategy
Load Strategy
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
client:load
Highest priority. Load and hydrate immediately on page load.
Use for: Critical interactive elements above the fold.
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.client:visible
Load once the component enters the user’s viewport.
Use for: Below-the-fold interactive content.
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
Remove Unused Dependencies
Remove Unused Dependencies
Audit your dependencies regularly:Remove packages you’re not using:
Use Dynamic Imports
Use Dynamic Imports
Import heavy libraries only when needed:
Optimize Dependencies
Optimize Dependencies
Configure Vite to optimize specific dependencies:
astro.config.mjs
Caching Strategies
Static Asset Caching
Configure your hosting provider to cache static assets:netlify.toml
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
Use the Image component
Always use
<Image> for local and remote images to get automatic optimization.Advanced Optimizations
Edge Rendering
Edge Rendering
Deploy to edge networks for faster response times:Edge rendering reduces latency by serving content from locations close to users.
astro.config.mjs
Partial Hydration
Partial Hydration
Only hydrate the interactive parts of your page:
Service Workers
Service Workers
Implement offline support and advanced caching:
public/sw.js