Choose Theme

Getting Started with Astro

· 3 min read · #Astro #Tutorial #Web Development
--

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:

  1. Zero JS by default - Ship only the JavaScript you need
  2. Island Architecture - Isolated component hydration
  3. UI-agnostic - Use React, Vue, Svelte, or just HTML
  4. 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:

Terminal window
# Create a new project
npm create astro@latest
# Navigate to your project
cd my-astro-site
# Start the dev server
npm run dev

Adding Tailwind CSS

Tailwind integrates seamlessly with Astro:

Terminal window
npx astro add tailwind

This command will:

  • Install Tailwind CSS
  • Create a tailwind.config.mjs file
  • Update your Astro config

Pro tip: Use @tailwindcss/typography for 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 numbers
function fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
console.log(fibonacci(10)); // 55

Components 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!