Getting Started with Astro: A Modern Static Site Generator
Astro is a modern static site generator that’s changing how we think about building content-focused websites. Unlike traditional frameworks that ship entire JavaScript bundles to the client, Astro takes a different approach: it renders your components to static HTML at build time and only hydrates interactive components when needed.
Why Astro?
1. Zero JavaScript by Default
Astro ships zero JavaScript to the client by default. Your components are rendered to static HTML at build time, resulting in incredibly fast page loads. When you do need interactivity, Astro’s partial hydration ensures you only load the JavaScript that’s absolutely necessary.
2. Framework Agnostic
One of Astro’s killer features is its ability to work with multiple frameworks simultaneously. You can use React, Vue, Svelte, Solid, and even vanilla JavaScript components in the same project. This makes it perfect for migrating existing projects or leveraging the best tool for each job.
---
// You can mix and match frameworks!
import ReactComponent from '../components/ReactComponent.jsx';
import VueComponent from '../components/VueComponent.vue';
---
<div>
<ReactComponent client:load />
<VueComponent client:idle />
</div>
3. Built-in Optimizations
Astro automatically optimizes your site with features like:
- Automatic image optimization
- CSS and JavaScript bundling
- Prefetching for faster navigation
- Built-in sitemap generation
Getting Started
Creating a new Astro project is straightforward:
npm create astro@latest
cd my-astro-project
npm run dev
The Component Model
Astro components use a fence (---
) to separate JavaScript logic from the template:
---
// Component Script (runs at build time)
const greeting = "Hello, World!";
const items = ["Astro", "React", "Vue"];
---
<!-- Component Template -->
<div>
<h1>{greeting}</h1>
<ul>
{items.map(item => <li>{item}</li>)}
</ul>
</div>
Partial Hydration
Astro’s partial hydration is controlled through client directives:
client:load
- Hydrates immediately on page loadclient:idle
- Hydrates once the browser is idleclient:visible
- Hydrates when the component enters the viewportclient:media
- Hydrates based on a media query
<!-- This component only becomes interactive when visible -->
<InteractiveComponent client:visible />
Content Collections
Astro’s Content Collections API provides type-safe markdown content management:
import { defineCollection, z } from 'astro:content';
const blog = defineCollection({
schema: z.object({
title: z.string(),
date: z.date(),
tags: z.array(z.string()),
}),
});
export const collections = { blog };
Performance Benefits
Sites built with Astro consistently achieve perfect Lighthouse scores:
- First Contentful Paint: < 1s
- Time to Interactive: < 2s
- Cumulative Layout Shift: 0
Conclusion
Astro represents a paradigm shift in how we build content-focused websites. By shipping less JavaScript and leveraging static generation, it delivers exceptional performance without sacrificing developer experience. Whether you’re building a blog, documentation site, or marketing pages, Astro provides the tools you need to create fast, modern websites.
Give Astro a try for your next project – your users (and their devices) will thank you!