Getting Started with Astro
Astro is a modern web framework that helps you build faster websites with less JavaScript. It’s perfect for content-focused sites like blogs, documentation, and portfolios.
Why Astro?
Astro brings several advantages to the table:
- Zero JS by default - Ship only the JavaScript you need
- Island Architecture - Isolated component hydration
- UI-agnostic - Use React, Vue, Svelte, or just HTML
- Fast builds - Optimized for speed
Performance First
Astro’s approach to performance is simple: ship less JavaScript. By default, Astro strips all JavaScript from your final build and only hydrates interactive components when needed.
// Example: A simple Astro component---interface Props { title: string; description?: string;}
const { title, description = 'Default description' } = Astro.props;---
<div class="card"> <h2>{title}</h2> {description && <p>{description}</p>}</div>Setting Up Your First Project
Getting started with Astro is straightforward. Here’s what you need to do:
# Create a new projectnpm create astro@latest
# Navigate to your projectcd my-astro-site
# Start the dev servernpm run devAdding Tailwind CSS
Tailwind integrates seamlessly with Astro:
npx astro add tailwindThis command will:
- Install Tailwind CSS
- Create a
tailwind.config.mjsfile - Update your Astro config
Pro tip: Use
@tailwindcss/typographyfor beautiful prose styling. It’s perfect for blog posts and markdown content!
Working with Markdown
Astro has first-class support for Markdown and MDX. You can write your content in .md or .mdx files and Astro will automatically convert them to HTML.
Here’s an example of inline code: const greeting = 'Hello, Astro!'
Code Blocks
Astro uses Shiki for syntax highlighting, which means your code blocks look amazing out of the box:
// Function to calculate fibonacci numbersfunction fibonacci(n) { if (n <= 1) return n; return fibonacci(n - 1) + fibonacci(n - 2);}
console.log(fibonacci(10)); // 55Components and Islands
One of Astro’s killer features is Islands Architecture. This allows you to:
- Build your site with any UI framework
- Only hydrate interactive components
- Keep the rest of your site static
---import Counter from '../components/Counter.tsx';---
<div> <h1>My Page</h1> <!-- This component is interactive --> <Counter client:load /></div>Conclusion
Astro is a powerful tool for building modern websites. Its focus on performance and developer experience makes it a joy to work with.
Key takeaways:
- Astro ships zero JavaScript by default
- Use any UI framework you like
- Perfect for content-focused sites
- Great developer experience
Ready to build something awesome? Check out the official documentation to learn more!