Skip to content

Performance Optimization

This guide documents our established patterns for optimizing performance, using real examples from our codebase.

Use this flowchart to determine the appropriate optimization strategy:

Key Decision Points:

  • Data Loading: Server vs Client-side data fetching
  • UI Type: Static vs Interactive components
  • Asset Type: Images, Scripts, or Styles
  • Loading Priority: Critical vs Non-critical

Common Pitfalls:

  • Client-side data transforms
  • Eager loading of below-fold content
  • Missing image optimization
  • Unoptimized bundle sizes

Use server-side data loading and transformation whenever possible:

// Good: Server-side transform
---
const data = await fetchData();
const processed = transformOnServer(data);
---
<Component data={processed} />
// Avoid: Client-side transform
<Component
data={data}
client:load
transform={transformOnClient}
/>

Strategic component hydration:

// Critical above-fold content
<Header client:load />
// Below-fold interactive content
<Comments client:visible />
// Non-critical features
<ShareButtons client:idle />

Image optimization with Imgix:

<ImgixImage
src={src}
width={800}
height={600}
sizes="(min-width: 1024px) 800px, 100vw"
/>

Route-based code splitting:

pages/[slug].astro
const { Comment } = await import('../components/Comment');

Use performance monitoring to track:

  1. Core Web Vitals

    • LCP (Largest Contentful Paint)
    • FID (First Input Delay)
    • CLS (Cumulative Layout Shift)
  2. Custom Metrics

    • Component hydration timing
    • Data loading performance
    • Asset loading timing

When optimizing performance:

  1. Data Strategy

    • Server-side transforms where possible
    • Appropriate loading patterns
    • Caching strategy defined
    • Error handling in place
  2. Component Strategy

    • Correct hydration directive
    • Loading states implemented
    • Error boundaries set up
    • Bundle impact considered
  3. Asset Strategy

    • Images optimized
    • Scripts properly loaded
    • Styles optimized
    • Resources prioritized
  4. Monitoring Strategy

    • Core Web Vitals tracked
    • Custom metrics defined
    • Alerts configured
    • Regular review process