NML Logo

Exploring Astro: A Fast and Flexible Framework for Building Content-Rich Websites

BY JUSTIN SLACK

Exploring Astro: A Fast and Flexible Framework for Building Content-Rich Websites

In one of our recent knowledge-sharing sessions we dove into Astro, an innovative framework designed to help you build fast, content-focused websites. If you're new to Astro, head over to their official site, Astro.build, to get started. In this post, we'll explore what Astro is, its key features, and a quick demo to showcase its capabilities.

What is Astro?

Astro is an all-in-one web framework aimed at creating fast, content-focused websites. Here are some key highlights:

  • Content-Focused: Ideal for content-rich websites.
  • Server-First: HTML is rendered on the server for faster load times.
  • Zero JS by Default: No unnecessary JavaScript runtime overhead.
  • Edge Ready: Deploy anywhere, including global edge runtimes like Deno or Cloudflare.
  • Customizable: Over 100 integrations available.
  • UI Agnostic: Supports various frameworks like React, Preact, Svelte, Vue, and more.

Component Island Architecture

Astro’s key feature is the component island architecture. This allows interactive UI components to exist on an otherwise static HTML page, providing a significant performance boost.

  • Server-First API Design: Moves hydration off user devices.
  • Interactive Islands: Each component island renders in isolation, making the page fast and efficient.
  • Partial Hydration: Only the necessary components are hydrated, leaving the rest as static HTML.

Creating an Astro Project

Creating an Astro project is straightforward. You can start by running `yarn create astro`, and follow the prompts to set up your project. Astro provides several templates to kickstart your development, including a personal website template with pre-built components and best practices.

Project Structure

An Astro project typically includes:

  • src: Source files, including components and pages.
  • public: Static assets like images and fonts.
  • astro.config.mjs: Configuration settings for Astro.
  • package.json: Project dependencies and scripts.

Building with Astro

Astro components are the building blocks of your project. Each component consists of:

  • Component Script: JavaScript code that runs on the server.
  • Component Template: HTML that defines the component structure.

For example, a simple Astro component might look like this:

---  
// JavaScript code  
const name = "Astro";  
---  
<!DOCTYPE html>
<html>
    <head>
        <title>{name} Demo</title>  
    </head>  
    <body>
         <h1>Welcome to {name}</h1>  
    </body>
</html>

Integrating UI Frameworks

Astro supports integrating various UI frameworks. For example, to use React, you simply add the @astrojs/react integration:

yarn add @astrojs/react  

Then, you can import and use React components within your Astro project.

Fetching Data

Astro supports server-side data fetching using the global fetch function. This allows you to fetch data at build time and render it as static HTML.

---  
const res = await fetch('<https://rickandmortyapi.com/api/character>');  
const data = await res.json();  
---  
<html>
    <body>
       {data.results.map(character => (  
      <div>
         <h2>{character.name}</h2>
         <p>{character.status}</p>
         <p>{character.species}</p>
     </div>
    ))}  
   </body>
</html>

Adding Styles

Astro supports various styling options, including CSS, CSS modules, and even preprocessors like Sass. You can scope styles to specific components or apply global styles as needed.

Demo Time

In our demo, we created a simple Astro project and explored how to:

  • Set up a project structure.
  • Create and import components.
  • Fetch data from an API.
  • Apply styles using CSS and CSS modules.
  • Integrate with React for dynamic components.

Conclusion

Astro is a powerful framework for building fast, content-rich websites with ease. Its component island architecture and server-first approach make it a standout choice for modern web development. Whether you're building a personal blog, a static site, or a complex web application, Astro provides the tools and flexibility to get the job done efficiently.

We hope this session has given you a good introduction to Astro and its capabilities. Feel free to explore further and start building your next project with Astro!

Introduction to Monolithic vs. Microservice Architecture: A Practical Guide

In this post we explore two distinct approaches to software architecture: Monolithic and Microservice.
Continue Reading

Exploring Astro: A Fast and Flexible Framework for Building Content-Rich Websites

In one of our recent knowledge-sharing sessions we dove into Astro, an innovative framework designed to help you build fast, content-focused websites
Continue Reading