Skip to content

Astro Js - The Ultimate SSG

Published: at 05:59 AM

In the growing world of web development, speed, performance, and simplicity are most important to build a standout website. That’s where Static Site Generation (SSG) stands out as a hybrid approach which gives the benefits of SSR and CSR while resolving the drawbacks of both. As static site generators (SSGs) grow in popularity, Astro.js has come out as a cutting-edge solution that deals with many of the challenges developers face when creating high-performant, fast and content-heavy websites that doesn’t break your browser while loading the content.

This website is also built on Astro.js

Table of contents

Open Table of contents

What is Astro.js ?

Astro is an all-in-one web framework that is suitable for developing high-performance content-driven websites such as blogs, documentation sites, marketing sites, and portfolios. Astro focuses on optimizing web performance by minimizing JavaScript and providing fast, lightweight websites.

Our goal: It should be nearly impossible to build a slow website with Astro. – Team Astro

Features of Astro.js

Astro packs ton of features as a unique static site generator, here are some standout features that make Astro different from other SSGs:

Benefits of Using Astro

Astro stands out from other static site generators (SSGs) because of its focus on performance, flexibility, and ease of use. Let’s explore the major benefits of using Astro.js for static site generation.

Optimized Performance

Astro.js is best known for introducing an innovative frontend architecture to reduce JavaScript overhead and complexity compared to other frameworks which results in a blazing-fast html website with optimized performance. Below is listed how Astro optimizes its performance to peak.

BYOF ( Bring Your Own Framework)

One of the intriguing features of Astro is its flexibility in using your favorite UI framework as a component. Astro supports a variety of popular frameworks including React, Preact, Svelte, Vue, SolidJS, AlpineJS, and Lit with official integrations. Even for framework-based components, Astro will only deliver minimal HTML output, avoiding any unnecessary JavaScript unless it’s explicitly required.

Example :

To use a UI framework component, import it from its relative path in your Astro component or page. Then, use the component alongside other components, HTML elements, and JSX-like expressions in the component template.

---
import AstroComponent from '../components/AstroComponent.astro';
import SvelteComponent from '../components/SvelteComponent.svelte';
import ReactComponent from '../components/ReactComponent.tsx';
---
<html>
  <body>
    <h1>Use React components directly in Astro!</h1>
    <AstroComponent />
    <ReactComponent client:only="react"/>
    <SvelteComponent client:only="svelte"/>
  </body>
</html>

A UI framework component can be made interactive (hydrated) using a client:* directive. which determines when your component’s JavaScript should be sent to the browser.

Astro provides several directives to control how and when the JavaScript for your component islands is loaded. Some most used ones are :

Note: By default, a UI Framework component is not hydrated in the client. If no client:* directive is provided, its HTML is rendered onto the page without JavaScript.

Improved SEO

Astro is designed to deliver SEO-optimized static sites by providing fast page loads, and clean semantic HTML that search engines can easily crawl and index your website Pre-rendered static HTML improves crawling and indexing by search engines, providing better SEO compared to client-side rendered frameworks. This is a big advantage over client-side rendered websites, where content is dynamically generated on the client, sometimes causing issues for search engine bots.

Website speed is one of the most crucial SEO ranking factors and Astro’s architecture ensures that your website loads as quickly as possible. By shipping zero JavaScript by default and using server-side rendering for content, Astro significantly reduces the page load time. Faster sites improve both user experience and Google’s Core Web Vitals scores, which directly impacts SEO rankings.

Astro provides a sitemap plugin that makes generating sitemaps for your website easy, ensuring that all your pages are crawled and indexed by search engines. This feature is crucial for larger websites with multiple pages or dynamic content that need consistent indexing.

Install the sitemap plugin using your preferred package manager

# using npm
npm install @astrojs/sitemap

# using pnpm
pnpm add @astrojs/sitemap

# using yarn
yarn add @astrojs/sitemap

Configure the plugin in the config

# Add the sitemap plugin to astro.config.mjs
import { defineConfig } from 'astro/config';
import sitemap from '@astrojs/sitemap';

export default defineConfig({
  	  site: 'https://example.com',
  integrations: [sitemap()],
});

Content-driven

As built on focus for content-driven websites Astro has various features that allow it to develop performant, powerful, dynamic web applications that still respect your content while delivering unmatched performance that wouldn’t make sense for any application-focused web frameworks to deliver.

Seamless Integration with Markdown

Markdown is a convenient syntax for writing rich text with basic formatting and common elements like headers, lists, and images. Astro has built-in support for Markdown(.md) files which allows users to focus on writing content in Markdown, while still having the power to add interactive features using their favorite frontend frameworks without sacrificing the performance of the site.

Example

---
title: " Astro Blog Post"
layout: "../layouts/BlogLayout.astro"
---

# Welcome to Astro Blog

This is a static blog post written in Markdown, but can include dynamic components when needed:

<InteractiveComponent client:load />

Developer Experience

Last but not the least in this list of benefits of Astro, it provides a seamless and efficient development experience for both beginners and experienced developers with its simple and powerful features.

It provides a quick and easier setup process with built-in templates to get started with Astro in no time.

You can follow up with the below snippet to start your development journey with Astro.

# Create a new Astro project
npm create astro@latest

To start with a template, you can follow up with the below snippet. You can find the templates in the official GitHub of Astro here.

# create a new project with a template
npm create astro@latest -- --template <example-name>

Astro has a built-in cli that helps to develop, build, and preview the project from the terminal. It has a lot of commands, you can check out all the available commands here. Below are some of the most used commands :

# to run the project in development mode
astro dev

# to build the project
astro build

# to preview the build version
astro preview

# to diagnostics the astro files and components
astro check 

# to add integrations to the project
astro add

Astro also provides a unique tool known as “Dev Toolbar”, which is quite useful during the development phase. It has lot of in-built tools such as a page performance audit tool, image audit tool, component inspect tool and system debug tool which helps a lot during the development. It also supports a lot of integrations with the Dev Toolbar which you can check out here.

Conclusion

Astro’s focus on reducing initial client-side javascript load, optimizing assets, and improving Core Web Vitals makes it an ideal choice for developers and businesses seeking high-performance websites that can scale seamlessly. Astro provides the right balance between speed, flexibility, and interactivity, making it a powerful tool in the static site generation space. With Astro, developers can build not just static websites, but highly optimized static sites that deliver exceptional performance, user experience.

Astro packs even more cool features than I can write and explain(lazy as 🤫). But if you’re as much of a geek 🤓 (but not lazy 🙂‍↕️) as I am, check out Astro and explore its awesomeness for yourself! also don’t forget to let me know also 😅.


Previous Post
5 Lazy Ways to Boost Your Coding Productivity