DEV Community

Cover image for Exploring Astro: Your New Favorite Web Framework
Marco Pineda
Marco Pineda

Posted on

Exploring Astro: Your New Favorite Web Framework

Hey there! If you’re into building modern websites, you’ve probably heard about Astro. (I hadn't until about two weeks ago) It’s a new kid on the block, and it’s turning heads for all the right reasons...or is it? It's healthy to be a skeptic. Astro makes it super easy to build fast, lightweight websites—and today, I’m going to give you the lowdown on why it’s awesome, how it stacks up against Next.js, and even walk you through a quick example to get started.


So, What Exactly is Astro?

Astro is a web framework that’s all about speed and simplicity. Unlike some other frameworks that load up your site with a ton of JavaScript, Astro takes a "static-first" approach. It serves most of your site as plain old HTML and only adds JavaScript for the interactive bits. Cool, right?

Here are some of Astro’s highlights:

  • Static-first: Your site is mostly HTML by default, so it’s fast out of the gate.
  • Mix and match frameworks: Want to use React, Vue, and Svelte in one project? No problem!
  • Blazing fast: Less JavaScript means faster load times.
  • Markdown-friendly: Writing content in Markdown feels natural and smooth.

Astro vs. Next.js: The Showdown

Next.js is a big deal in the React world, and for good reason. It’s powerful, versatile, and great for dynamic apps. But Astro brings some fresh ideas to the table, especially for sites that are more content-focused. Let’s break it down:

Why You’ll Love Astro:

  1. Way less JavaScript: Astro ships less JavaScript to the browser, which means faster loading times.
  2. Use what you love: Mix and match components from different libraries like React, Vue, or Svelte.
  3. Perfect for content-heavy sites: If you’re building a blog or a marketing site, Astro’s static-first vibe is a dream.
  4. Markdown made easy: Writing content in Markdown is seamless, and Astro handles it beautifully.

Where Next.js Shines:

  1. Mature ecosystem: Next.js has been around longer, so it has more plugins and tools.
  2. Dynamic power: If your app needs a lot of interactivity or server-side magic, Next.js is still the champ.
  3. Big community: There are tons of resources and people to help you out.

Why Should You Try Astro?

If you’re building something like a blog, a documentation site, or a marketing page, Astro’s focus on speed and simplicity is hard to beat. It’s perfect for static sites where performance matters most. That said, if you’re working on a super dynamic app, Next.js might still be your go-to.


Let’s Build Something with Astro!

Let’s see Astro in action. We’re going to create a simple blog homepage.

Step 1: Setup

  1. First, install Astro:
   npm create astro@latest
   cd my-astro-blog
   npm install
Enter fullscreen mode Exit fullscreen mode
  1. Start the dev server:
   npm run dev
Enter fullscreen mode Exit fullscreen mode

Step 2: Make a Homepage

  1. Create a new file at src/pages/index.astro:
   ---
   import BlogPost from '../components/BlogPost.astro';

   const posts = [
     { title: 'Welcome to Astro', description: 'Learn about Astro, the new static site builder.' },
     { title: 'Markdown Made Easy', description: 'Write your content seamlessly in Markdown.' },
   ];
   ---

   <html>
     <head>
       <title>My Astro Blog</title>
     </head>
     <body>
       <h1>My Astro Blog</h1>
       <ul>
         {posts.map(post => (
           <li>
             <BlogPost title={post.title} description={post.description} />
           </li>
         ))}
       </ul>
     </body>
   </html>
Enter fullscreen mode Exit fullscreen mode
  1. Now, make a BlogPost component in src/components/BlogPost.astro:
   ---
   export interface Props {
     title: string;
     description: string;
   }

   const { title, description } = Astro.props;
   ---

   <article>
     <h2>{title}</h2>
     <p>{description}</p>
   </article>
Enter fullscreen mode Exit fullscreen mode
  1. Fire up your browser and go to http://localhost:3000. Boom! You’ve got a simple blog homepage.

Final Thoughts

Astro is an pretty decent alternative to traditional frameworks like Next.js, or Gatsby, especially for static sites. Astro focuses on speed and ease of use which makes it a great choice for blogs, documentation, or any project where performance is key. Give it a shot—you might just find your new favorite tool for a personal portfolio!

Top comments (0)