Astro has been gaining a lot of attention lately, and I wanted to explore what it has to offer. As someone who dislikes managing servers and prefers using platforms like Netlify and Vercel, Astro seemed like an interesting solution with its unique approach.
Here are the key reasons why I find Astro appealing:
- Static site generation: Astro generates static sites, eliminating the need for server management. This aligns perfectly with my preference for static sites.
- Framework-agnostic: Unlike other JavaScript frameworks that are tied to specific libraries, Astro allows flexibility by supporting different frameworks for different sites. This can be useful in various scenarios where mixing frameworks is required.
- Simplicity: Astro emphasizes simplicity, making it easy to work with and understand.
- Minimal JavaScript in the browser: Astro follows an “HTML-first, JavaScript-only-as-needed” approach, where the majority of the work is done at build time. This results in fewer JavaScript dependencies in the browser, providing improved performance.
Astro stands out in the JavaScript space for being framework-agnostic. While frameworks like Next.js, Gatsby, and SvelteKit are built on specific libraries, Astro allows me to use React, Svelte, or Vue interchangeably across different sites. This versatility enables me to leverage my Astro expertise while building with different frameworks or even combining them for different sections of a site.
Astro adopts an HTML-first approach, where the final build is a static HTML site. Only the necessary JavaScript libraries are loaded in the browser. This approach capitalizes on the benefits of composition and allows for the reuse of components built with other libraries such as React, Vue, or Svelte.
Additionally, Astro incorporates valuable concepts from other projects, such as frontmatter, built-in MDX support, scoped CSS, and filesystem-based routing. These features enhance the development experience and empower developers to create efficient and organized projects.
Astro was publicly launched in June 2021 and has since generated significant buzz. To understand more about Astro and its capabilities, I decided to install a few Astro sites using the Astro installer. By starting with a minimal template and gradually exploring more complex examples, I aimed to grasp the breadth of Astro’s capabilities.
To get started, I created an empty folder and ran the following command:
npm init astro
This command initiated the Astro installer, which presented me with different starter template options. I chose the “Minimal” template to create a simple site. After the template was set up, I ran the following commands:
npm install
npm run dev
These commands installed the necessary dependencies and launched the local development server on http://localhost:3000/. The “Minimal” template generated a minimalistic site that showcased the basic features of Astro.
Next, I decided to create another site using the “Blog” template to explore its capabilities further. I repeated the installation process, picking the “Blog” template, and made some additional configurations. The resulting project structure in Finder showcased how Astro organized its files.
By default, Astro templates utilize Preact, a lightweight alternative to React. However, Astro also provides the option to choose other frameworks like React, Solid, Svelte, or Vue. This flexibility allows developers to select the framework that best suits their project’s requirements.
As I explored a Svelte-enabled instance, I noticed a widget at the bottom of the page, which showcased a Svelte component. This feature highlighted Astro’s seamless integration with different frameworks and reinforced the concept of reusable components.
To view and edit the Astro code, I installed the Astro extension for VS Code. This extension ensured that syntax highlighting was applied correctly to my “.astro” files, enhancing the development experience.
Astro’s building blocks are “.astro” files, which are categorized as either pages or components. Pages represent routes within the site, such as “/” or “/blog/hello-world,” while components are reusable pieces of code used by the pages.
The project structure typically consists of “src/pages” for pages, “src/components” for components, and a “public” folder for accessible assets like images.
While there’s more to explore, such as Astro’s unique file organization and its novel approach to building websites, I’ll cover these topics in future blog posts as part of a short series on Astro.
Stay tuned for upcoming posts in the Astro series:
- Astro Components
- Building composable layouts in Astro
- Astro Props
- Moving a simple site to Astro
- Fetching data from the network in Astro
Tags: Astro, static site generator, framework-agnostic, JavaScript, serverless, performance optimization.