· Samuel Tian · Website Design

Getting Started with Astro: A Modern Web Framework

Learn how to build fast, modern websites with Astro's component-based architecture and static site generation capabilities.

Learn how to build fast, modern websites with Astro's component-based architecture and static site generation capabilities.

Getting Started with Astro: A Modern Web Framework

Astro is a modern web framework that allows you to build fast, content-focused websites. It’s designed to give you the best of both worlds: the performance of static sites with the flexibility of dynamic applications.

Why Choose Astro?

Astro stands out from other frameworks for several key reasons:

1. Performance First

Astro ships zero JavaScript by default, resulting in incredibly fast loading times. Only the JavaScript you actually need gets sent to the browser.

2. Component Islands

You can use any UI framework (React, Vue, Svelte, etc.) as “islands” within your Astro pages, giving you the flexibility to choose the right tool for each component.

3. Content-Focused

Built specifically for content-heavy websites like blogs, documentation sites, and marketing pages.

Setting Up Your First Astro Project

Let’s walk through creating a new Astro project:

# Create a new project
npm create astro@latest my-astro-site

# Navigate to the project
cd my-astro-site

# Install dependencies
npm install

# Start the development server
npm run dev

Project Structure

A typical Astro project has the following structure:

my-astro-site/
├── src/
│   ├── components/
│   ├── layouts/
│   ├── pages/
│   └── content/
├── public/
├── astro.config.mjs
└── package.json

Creating Your First Page

In Astro, pages are created in the src/pages/ directory. Here’s a simple example:

---
// Frontmatter goes here
const title = 'Hello, Astro!';
---

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>{title}</title>
  </head>
  <body>
    <h1>{title}</h1>
    <p>Welcome to your first Astro page!</p>
  </body>
</html>

Using Components

Components in Astro use the .astro extension and can be used in any page:

---
// src/components/Header.astro
const { title } = Astro.props;
---

<header>
  <h1>{title}</h1>
  <nav>
    <a href="/">Home</a>
    <a href="/about">About</a>
    <a href="/contact">Contact</a>
  </nav>
</header>

Content Collections

Astro’s content collections provide type-safe content management:

// src/content/config.ts
import { defineCollection, z } from 'astro:content';

const blog = defineCollection({
  schema: z.object({
    title: z.string(),
    author: z.string(),
    publishDate: z.date(),
  }),
});

export const collections = { blog };

Deployment

Astro sites can be deployed to any static hosting service:

  • Netlify : Automatic deployments from Git
  • Vercel : Optimized for Astro projects
  • GitHub Pages : Free hosting for open source projects

Performance Benefits

Astro’s performance advantages include:

  • Zero JavaScript by default : Pages load instantly
  • Automatic image optimization : Built-in responsive images
  • CSS optimization : Automatic CSS bundling and minification
  • Preloading : Smart resource preloading

Conclusion

Astro is an excellent choice for building modern, performant websites. Its component-based architecture, zero-JavaScript default, and excellent developer experience make it a compelling option for content-focused projects.

Whether you’re building a blog, documentation site, or marketing page, Astro provides the tools you need to create fast, maintainable websites that your users will love.


Ready to start building with Astro? Check out our Astro development services to get professional help with your next project.

Related Articles

You might also like
← Back to Insights