Getting Started with Astro 5.x
Getting Started with Astro 5.x
Astro is a modern static site builder that delivers lightning-fast performance with minimal JavaScript. With the release of Astro 5.x, it’s become even more powerful and developer-friendly.
Why Choose Astro?
Performance First
Astro ships zero JavaScript by default, loading only what’s necessary. This results in near-instant page loads and excellent Lighthouse scores.
Framework Agnostic
You can use React, Vue, Svelte, or plain HTML/CSS/JavaScript components within Astro. Mix and match as needed.
Content Collections
Type-safe content management with automatic TypeScript generation. Perfect for blogs, documentation, and content-heavy sites.
Setting Up Your First Astro Project
Installation
npm create astro@latest my-project
Project Structure
my-project/
├── src/
│ ├── components/
│ ├── layouts/
│ ├── pages/
│ └── content/
├── public/
└── astro.config.mjs
Adding Tailwind CSS
npx astro add tailwind
Key Features of Astro 5.x
1. Content Collections
// src/content/config.ts
import { defineCollection, z } from 'astro:content';
const blog = defineCollection({
schema: z.object({
title: z.string(),
description: z.string(),
publishDate: z.date(),
tags: z.array(z.string()),
}),
});
2. Image Optimization
Astro 5.x includes built-in image optimization with automatic format conversion, sizing, and lazy loading.
3. MDX Support
Write interactive content with React components directly in your Markdown.
4. Partial Hydration
Only hydrate interactive components when needed, keeping the rest static.
Building a Blog with Astro
Creating Blog Posts
- Create Markdown files in
src/content/blog/ - Add frontmatter with metadata
- Use Astro components within your posts
Generating Blog Pages
---
// src/pages/blog/[...slug].astro
import { getCollection } from 'astro:content';
export async function getStaticPaths() {
const posts = await getCollection('blog');
return posts.map(post => ({
params: { slug: post.slug },
props: { post },
}));
}
const { post } = Astro.props;
const { Content } = await post.render();
---
<article>
<h1>{post.data.title}</h1>
<Content />
</article>
Performance Optimization
1. Code Splitting
Astro automatically splits your JavaScript by page.
2. Asset Optimization
Images, CSS, and JavaScript are optimized during build.
3. Prefetching
Intelligent prefetching for faster navigation.
Deployment
GitHub Pages
# .github/workflows/deploy.yml
name: Deploy to GitHub Pages
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- run: npm ci
- run: npm run build
- uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./dist
Vercel/Netlify
Both platforms offer one-click deployment for Astro projects.
Conclusion
Astro 5.x represents a significant step forward in static site generation. Its focus on performance, developer experience, and flexibility makes it an excellent choice for modern web development.
Whether you’re building a personal blog, documentation site, or portfolio, Astro provides the tools you need to create fast, accessible, and maintainable websites.