close icon
daily.dev platform

Discover more from daily.dev

Personalized news feed, dev communities and search, much better than what’s out there. Maybe ;)

Start reading - Free forever
Start reading - Free forever
Continue reading >

Get to know Astro: The web framework for content-driven websites

Get to know Astro: The web framework for content-driven websites
Author
Nimrod Kramer
Related tags on daily.dev
toc
Table of contents
arrow-down

🎯

Explore the features and benefits of Astro, a web framework for content-driven websites. Learn about its speed, simplicity, flexibility, and focus on content. Discover how Astro stands out from other frameworks and its plans for future evolution.

Astro is a modern web framework designed to help you build fast and efficient websites with a focus on content. It stands out for its use of Island Architecture, allowing parts of your website to be dynamic only when needed. Astro is framework agnostic, meaning it lets you use tools like React, Vue, or Svelte seamlessly. One of its key features is the Zero JavaScript approach for faster page loads. Whether you're just starting or looking to switch to a more performance-oriented framework, Astro offers a blend of simplicity, flexibility, and speed. Here's a quick overview:

  • Speed and Performance: Astro builds fast-loading websites by eliminating unnecessary JavaScript.
  • Simplicity in Development: Getting started with Astro is straightforward, making web development less complex.
  • Flexible Tooling: Use your favorite JavaScript frameworks and libraries within Astro.
  • Content Focus: Optimized for content-driven sites with support for Markdown and MDX.

Astro is evolving, with new features like data fetching and user sessions on the horizon, making it an exciting option for modern web development. It's free and open-source, supported by a vibrant community.

The Rise of Content-First Websites

Lately, more websites focus on sharing information rather than being complex. This change is because people want websites that are:

  • Easy to find on search engines and social media
  • Quick to load
  • Simple to use for everyone

Astro is made for these kinds of websites. It's really good at turning content like blog posts into web pages quickly and cutting down on extra code for better speed.

Astro's Evolution

Astro

Astro started as a simple tool for making static websites but has grown to do a lot more, similar to other tools like Next.js.

It's had some big updates, like Astro 1.6 which made pictures look better and made it easier to use with WordPress. The Astro 2.0 update brought in new features like updating parts of a page without reloading and making it work well with different web development tools.

Astro keeps getting better and is becoming a popular choice for making websites focused on sharing information.

What Makes Astro Unique?

Astro stands out from other web frameworks like Next.js, Remix, and Nuxt.js because it's built differently and is really good for making websites that focus on sharing information.

Zero JS by Default

Astro creates web pages ahead of time that don't need JavaScript to work. This means these pages can show up super fast on your screen. Even though it doesn't use JavaScript at first, Astro can still make pages on-the-go if needed, giving you the best of both worlds.

Islands for Gradual Adoption

Astro introduces something called "islands" which lets you add bits of interactivity to your pages over time. This way, most of your website is just simple web pages, but you can choose to make some parts interactive. This keeps things speedy because only the parts that need to be interactive will use JavaScript.

Comparison to Other Frameworks

Framework Static Rendering Server-side Rendering Islands
Astro Yes Yes Yes
Next.js No Yes No
Nuxt.js No Yes No
Remix No Yes No

Astro mixes together the best parts of making websites ahead of time, creating them on-demand, and adding interactive bits. This makes it a great choice for building websites that are fast and flexible, especially if they're all about sharing content.

Getting Started with Astro

Prerequisites

Before diving into Astro, you'll need a couple of things ready:

  • Node.js - Astro needs Node.js version 14.18.0 or newer. You can grab the latest version from nodejs.org.
  • Code Editor - To write your code, you'll need a code editor. Popular choices are VS Code, Sublime Text, and Atom.
  • Terminal Access - You'll use a terminal for starting projects, adding parts to your site, and checking out your work locally. This could be Command Prompt, Powershell, Terminal, and so on.

Project Setup

Getting your Astro project up and running is straightforward. Just type this command:

npm create astro@latest

This command sets up a new Astro project for you by doing a few things:

  • Starting a new Node.js project
  • Adding the newest Astro version
  • Bringing in any needed Astro parts
  • Creating a basic project layout

After it's done, go into your new project folder and type:

npm install
npm run dev

This gets any other parts you need and opens up your Astro site in your local browser at localhost:3000. Now, you're all set to start creating.

Project Structure

Here's what the basic setup of an Astro project looks like:

├── public/
│   └── favicon.svg
├── src/
│   ├── components/
│   │   └── Card.astro
│   ├── layouts/
│   │   └── Layout.astro
│   └── pages/
│       └── index.astro
└── package.json

And here's a quick rundown of what each part does:

  • public/ - This is where you put stuff like pictures or fonts.

  • src/components/ - This is for bits of your site you'll use over and over, like buttons.

  • src/layouts/ - This is for the overall look of your site.

  • src/pages/ - This is where each page of your site goes. Each one is an .astro file.

This layout helps keep your project neat and organized from the start.

Core Concepts in Astro

Pages and Routing

In Astro, the pages of your website are made with .astro files and placed in the src/pages folder. The name and location of these files decide the web address (URL) where you can find them. For instance, src/pages/about.astro becomes /about on your website.

If you want to organize your pages into sections, you can make folders inside src/pages. So, src/pages/blog/first-post.astro turns into /blog/first-post.

Astro also lets you have pages with changing parts in their URLs, like src/pages/posts/[id].astro. This is useful for when you have many similar pages, like blog posts, that only differ by an ID or name.

Components

Components are bits of your website you can use over and over, like a sign-up button. You make them in .astro files and keep them in src/components. You can then include these components in your pages or other components.

Components can be customized with props and can include slots, which let you insert different content depending on where you use the component. Here's a simple example:

---
// Card.astro

---

<div class="card">
  <slot />
</div>

---

<Card>
  <h2>Hello</h2>
  <p>World!</p>  
</Card>

Layouts, Props, and Slots

Layouts are .astro files that define the common structure of your pages and are stored under src/layouts. They can include slots for content that changes from page to page.

Both pages and layouts can use props, which are like settings you can pass to customize them. Slots let you put content from a child component into a specific place in a parent component. Here's how you might use them:

---
// src/layouts/BaseLayout.astro

const { title } = Astro.props
---

<html>
  <head><title>{title}</title></head> 
  <body>
    <header>My Site</header>
    <slot />
  </body>
</html>

---

// src/pages/about.astro

---
title: About
--- 

<BaseLayout>
  About page content here  
</BaseLayout>

Islands and Hydration

Islands in Astro let you mix parts that are just there to look at (static) with parts that you can interact with (dynamic). You can make a component, like a button that counts clicks, interactive only when needed.

Here's an example with a counter button:

// src/components/Counter.jsx

export default function Counter() {
  const [count, setCount] = useState(0);

  return (
    <>
      <button onClick={() => setCount(prev => prev + 1)}>
        Click {count}  
      </button>
    </>
  )
}
// src/pages/index.astro  

---
import Counter from '../components/Counter.jsx';
---

<html>
  <h1>My Home Page</h1>
  <Counter client:load />
</html>

The client:load part means the Counter component will start working (become interactive) when someone visits the page.

Building Websites with Astro

Strategies and best practices for developing content-driven websites optimized for performance with Astro.

Content Optimization

When you're making websites with Astro, it's important to make sure your content loads quickly and people can find it easily. Here are some straightforward tips:

  • Make images load faster: Astro helps you make your images smaller and load quicker with a special image component. Use the right sizes, load images only when needed, and use a CDN to speed things up.
  • Keep navigation simple: With Astro, you don't need complicated setups to move around your site. A simpler approach means your pages will load quicker.
  • Write in Markdown and MDX: Astro works really well with Markdown and MDX. These formats are quicker than regular HTML.
  • Keep your HTML small: Astro automatically makes your HTML smaller. This is turned on for when your site goes live, but you can turn it off while you're still working on it.
  • Build pages ahead of time: Astro prepares all your pages before anyone visits your site. This means your site shows up super fast when someone goes to it.

Themes and Templates

You don't always have to start from zero. Astro has themes and templates you can use:

  • Astro Themes - Official themes for different kinds of sites, all ready to use with Astro.
  • Theme Repository - Themes made by the Astro community for various needs.
  • Templates - Starting points for your projects, including ways to work with content management systems like Contentful.

These tools help you avoid doing the same work over and over, so you can focus on what's special about your project.

Integrations

Integrations

Astro works really well with other popular tools:

  • React - Use @astrojs/react to add React components to your Astro site.
  • Vue - With @astrojs/vue, you can bring Vue into your Astro project.
  • Svelte - @astrojs/svelte lets you use Svelte components in Astro.
  • Tailwind - Make your site look good consistently with @astrojs/tailwind for Astro and Tailwind CSS.
  • MDX - Astro supports MDX right out of the box, thanks to @astrojs/mdx.

By mixing these tools, you can build your Astro site in a way that suits you, using skills you already have.

sbb-itb-bfaad5b

Advanced Features

Server-side Rendering

Astro can create pages right when someone asks for them, keeping your site fresh and up-to-date. Here's how to do it:

  • Change your Astro settings to output: 'server'
  • Use special methods like getStaticPaths and getStaticProps to decide what content shows up
  • Grab data from online sources or databases when someone visits a page
  • Send back data that can change based on who's looking or what they're doing

This way, Astro keeps its quick loading times but also gets new information when needed.

Image Optimizations

Astro makes sure your images load fast and look good on any device.

Image Component

  • Automatically changes size, waits to load until needed
  • Supports WebP format for better quality
  • Adjusts for different screen sizes

CDN Integrations

  • Work with services like Cloudinary and Imgix by tweaking settings
  • Netlify Large Media works right away

This means your website loads faster, looks good everywhere, and uses less data.

View Transitions API

Astro lets you add cool effects when moving between pages:

  • Make things fade in or out
  • Slide elements around for a dynamic feel
  • Change layouts smoothly

Usage

const { animate, sequence } = Astro.transitions;

animate({
  from: { opacity: 0 },
  to: { opacity: 1 },
  delay: 500,
  duration: 300,
});

These effects make your site more interesting and engaging.

Deployment & CI/CD

Deployment

You can put your Astro site online with services like:

  • Vercel
  • Netlify
  • Cloudflare Pages

They offer features like showing previews, using your own web address, and fast loading times around the world.

CI/CD

Automate your workflow with tools like:

  • GitHub Actions - Connects with your code updates and checks
  • CircleCI - Lets you set up complex steps
  • Travis CI - Offers lots of ways to build your site

This helps you test, fix, and update your site quickly and easily.

Astro in Action

StarBucks

This project is a remake of the starbucks.com website using Astro, along with Tailwind CSS for the design, and TypeScript for more secure coding.

Here’s what’s special about it:

  • It uses Astro's Island Architecture, which means the website only uses interactive features where they’re really needed.
  • Astro's features for making images load faster are used here.
  • Tailwind CSS helps make the website look consistent and nice.
  • TypeScript is used to catch errors in code early, making the site more reliable.

The person behind this project said:

Astro was great to work with! It made building a quick and attractive site easy. I especially liked how simple it was to work with images and add TypeScript. I’m looking forward to using Astro for more projects.

Developer Experiences

Developer Experiences

Astro’s website has a collection of stories from developers who enjoyed working with Astro.

Here’s what many of them say:

  • Simplicity - Many developers find Astro easier to use than other options. They like that it doesn’t need a lot of setup to get started.
  • Flexibility - Astro lets you use different tools like React and MDX easily. This means developers can use what they already know.
  • Speed - Because Astro builds websites ahead of time, they load super fast. This is good for being found on Google and keeping visitors happy.
  • Community - Astro is a project that anyone can help improve, and it’s got a lot of support on GitHub. There’s also a friendly community on forums and Discord where people can get help.

Brian Holt, a developer, said:

Out of all the tools I’ve used in the past ten years, Astro is the one that makes me most excited about where web development is going.

Conclusion

Key Benefits

Astro has a lot going for it if you're looking to make websites that are all about sharing content quickly and efficiently:

  • Speed and performance - Websites made with Astro are super fast because they don't use unnecessary code, and they handle pictures really well. This means pages can show up on your screen much faster than usual.
  • Simplified development - Starting with Astro is easy. It has a straightforward way of letting you build websites, which means you can create something cool without a lot of hassle.
  • Flexibility - Astro works well with a bunch of other web tools like React, Vue, and Tailwind CSS. This lets you work with what you're familiar with.
  • Content focus - Astro is great for websites that want to share information. It uses special formats like Markdown to help your content get seen by more people.

Overall, Astro makes making websites less complicated without losing out on what you need to make a great site. It's all about giving visitors a fast and enjoyable way to see your content.

What's Next for Astro

Astro is planning to add some new features that sound really useful:

  • Data fetching - Soon, Astro will let you grab data from places like online databases whenever you need it. This means you can have fresh content all the time.
  • User sessions - Astro will also start handling user sessions, which means your website can remember visitors and show them stuff they're interested in.
  • Streaming - Astro is working on a way to let websites update themselves with new content without having to reload everything.
  • Integrations - There are going to be more tools and plugins for Astro, making it even easier to build a wide variety of websites.
  • Community growth - Astro has a big group of users and developers making themes, components, and guides. This community is making it easier for everyone to use Astro.

Astro is all about making the web simpler but still powerful. With these updates, it's looking to make websites that are not only fast but also smart and customizable.

Additional Resources

Here are some great places to learn more about Astro and connect with other people who are using it.

Astro Documentation

Astro Documentation

This is the go-to spot for everything you need to know about Astro. You'll find:

  • Guides on the basics, detailed info on how it works, and how to set things up
  • Step-by-step instructions for making websites with tools like React
  • Updates on new features like making your website faster and how to handle pictures
  • It's easy to search and find what you're looking for

Discord Community

Discord Community

Join Astro's Discord to chat with others, get help, and share what you're working on.

  • It has places for asking questions, showing off your work, and even finding jobs
  • It's linked to Astro's online forums and where you can report issues
  • There are over 10,000 people there talking about Astro
  • It's a great way to get quick help

Tutorials & Courses

Find guides and lessons from other Astro users on various topics.

  • There's a bunch of tutorials out there on blogs and YouTube
  • They cover how to use Astro with things like React, how to make your site look good with Tailwind CSS, and more
  • You'll also find tips on how to put your Astro site online
  • These are super helpful for learning from others who've done it before

What is Astro web framework?

Astro is a tool for making websites that load quickly and don't rely too much on JavaScript for everything. It's designed to make websites faster by preparing pages before someone visits them. Astro lets you add interactive parts only where you need them, using tools like React, Vue, and Svelte. It's a mix of old-school website making and new tech to make web development easier and faster.

How do I know what framework my website is using?

To find out, right-click on a webpage and select "View Page Source". Look for a "generator" tag in the HTML, which is often used by platforms like WordPress. If you don't see it, check the JavaScript files for mentions of React or Vue. Another way is to look at the HTTP response headers for clues. Usually, checking the page source or JavaScript files gives you the answer.

Is Astro worth learning?

Definitely. Astro is great for making websites that are fast and efficient. It's good for people who already know how to use React and similar tools, but it's also easy for beginners. Astro is free, supported by a community, and offers a modern way to build websites. Learning Astro could be a good move if you're into web development.

Is Astro framework free?

Yes, Astro is totally free. It's an open-source project, which means anyone can use it without paying. Companies sponsor it, but you can use Astro for your projects without any cost. There's also an Astro Prime program for extra features, but the main tools you'll need are free.

Related posts

Why not level up your reading with

Stay up-to-date with the latest developer news every time you open a new tab.

Read more