Getting Started with Astro JS: A Walkthrough of Its Installation and Core Features

July 18, 2023

Getting Started with Astro JS A Walkthrough of Its Installation and Core Features

Research is done by Chris

In the beginning, static HTML websites were all we had. But now, enter Astro—a new JS multi-page application framework that has been gaining incredible traction within the web development community over the past few months.

The Astro framework primarily makes use of technologies such as Angular, React, and Node.js to create unique web experiences for users. It’s simple to get started with Astro—all you need is a basic understanding of HTML, CSS, and JavaScript (or one of the frameworks mentioned).

So, if you want to get started on this new web framework, this blog post will provide you with an overview of the installation process and some of the core features.

Let’s begin!

What is Astro JS?

Astro, the ultimate website builder, is specifically designed to create captivating and content-rich websites. Astro has got you covered whether you’re building marketing sites, single-page applications, documentation sites, blogs, portfolios, or even e-commerce sites!

On the flip side, most modern web frameworks are all about creating web applications. These different frameworks are perfect for building those fancy, application-like experiences right in your browser.

Now, Astro takes a different approach. Astro works by giving you mind-blowing content experiences. That’s why Astro creates certain tradeoffs and offers unbeatable performance features that other web frameworks focused on applications can’t match.

Why Use Astro?

As a Javascript framework, Astro offers a number of advantages over other web development technologies. That’s why it’s become popular for creating websites.

If you are thinking of building a website with rich user experiences, then Astro is worth considering. Here are some of the benefits that you should know about it:

Fast loading websites

Astro is a powerhouse, delivering static and server-rendered content that’s tailor-made for content-first, SEO-friendly websites. Whether it’s a captivating blog, immersive courseware, or a compelling marketing site, you can make it an Astro site.

Startechup UI UX Design Service

UI agnostic

Unlike Next.js, which is limited to React, or Nuxt.js, which demands Vue expertise, Astro liberates you from any UI framework constraints. You have the freedom to use none, craft your own, or explore the plethora of supported integrations.

Pre-made themes

Astro offers an incredible range of pre-designed themes and templates to kickstart your creativity in no time!

Easy to understand

No need to dive into React or Vue to begin your Astro journey. With its templating engine resembling plain HTML and clean code separation from presentation, you can start with an empty project and build it up at your own pace.

Batteries are included

Astro offers a range of features that align with our expectations for a modern JavaScript/TypeScript framework. These include top-level awaits, support for Markdown and MDX, and the inclusion of ESM.

Uses islands architecture

The island architecture empowers you to seamlessly blend static content, server-rendered, and client-rendered components on a single page, fostering interactive sites with ease. What’s more, you can even mix and match diverse frameworks on the same page, unlocking endless possibilities for crafting captivating microfrontends.

When Should You Not Use Astro?

Even being one of the popular UI frameworks, there are certain scenarios where using Astro in your projects may not be suitable.

Here are some situations to consider:

Small, static websites

If you’re working on a small project that consists of only a few static pages without complex functionality or dynamic content, using Astro may introduce unnecessary overhead. In such cases, a simpler static site generator or even plain HTML/CSS might be more appropriate.

Projects heavily reliant on server-side rendering (SSR)

Astro primarily focuses on static site generation and client-side rendering (CSR). Astro may not be the best fit if your project heavily relies on server-side rendering, such as for server-generated dynamic content or server-specific operations. Other frameworks or libraries specialized in SSR, like Next.js or Nuxt.js, might be more suitable.

Tight integration with backend frameworks

If your project involves tight integration with a backend framework or relies on server-side technologies like Node.js, Express.js, or Django, Astro’s static site generation approach might not align well with the backend requirements. In such cases, a web framework that offers strong backend integration, such as Next.js, might be a better choice.

Performance-critical applications

While Astro strives to provide efficient rendering and optimal performance, certain performance-critical applications with specific requirements may need lower-level control over optimizations. If you have an application with strict performance criteria or unique rendering needs, Astro’s abstractions may limit your ability to fine-tune performance optimizations.

Projects with extensive third-party integrations

If your project relies heavily on third-party libraries, frameworks, or plugins that are not compatible with Astro or lack support for Astro’s build system, it may cause conflicts or compatibility issues. Ensure that the ecosystem around your project supports Astro before committing to it.

How to Install Astro JS?

The easiest way to get the hang of Astro is to dive right in and start using it. If you don’t know how to start, here’s a quick guide on how to install and get started with Astro.

As long as you have these prerequisites, you can get up and running with Astro in no time:

  • Node.js – v16.12.0 or higher. (as of Astro v2.4.5)
  • Text editor – We recommend VS Code with our Official Astro extension.
  • Terminal – Astro is accessed through its command-line interface (CLI).

1. Run the Setup Wizard

npm create astro latest

After entering, the Astro create command will guide you through some initial setup:

2. It will ask for the location and name of your project. It gives a random project name by default.

astro create command

3. It will then ask if you want to use a template, sample files, or an empty project.

new project name in Astro

4. After that, it will ask if you want to install dependencies.

astro template

5. Then, it will ask if you plan to write Typescript. If yes, it will ask you how strict.

write TypeScript

6. Lastly, it will ask if you want to initialize a new git repository.

initialize a new git repository
git repository response

7. You can now run the project on your local using:

npm run dev

The Features of Astro Pages

Astro is not just your average website JS framework. It’s a performant powerhouse that puts content first and takes a UI-agnostic approach.

Let’s dive into some of the key features that make Astro truly outshine the competition:

Templates

As a highly popular web framework for content, Astro provides templates for those who want to get started coding right away and skip the setting up process. It’s got a few options like a Blog site, Documentation template, Portfolio, or an empty template.

Astro templates

Themes

Much like WordPress or other CMS, Astro has a handful of themes that you can use for free. Contributed by its growing community, you’ll never go out of style.

astro themes

Integrations

Astro Integrations empowers you to elevate the functionality and behavior of your project effortlessly. With utmost ease, you can craft a tailored integration, leverage official integrations, or tap into the creative power of community-built integrations.

astro integrations

Here are the official integrations you can use:

Structure

The same as other Javascript Frameworks, every Astro project root should include the following directories and files:

  • src/* – Your project source code (components, pages, styles, etc.)
  • public/* – Your non-code, unprocessed assets (fonts, icons, etc.)
  • package.json – A project manifest.
  • astro.config.mjs – An Astro configuration file. (recommended)
  • tsconfig.json – A TypeScript configuration file. (recommended)

Astro Components

An Astro component uses the file extension: .astro

They are HTML-only templating components with no client-side runtime. Just like other frameworks, Astro components are reusable.

An Astro component is made up of two main parts: the Component Script and the Component Template.

Component Script

  • You can use the component script to write any JavaScript code that you need to render your template. Astro uses three dashes (—) in between the javascript code to differentiate it from the content. This is written at the topmost of the file.

Component Template

  • The component template is below the Component Script and determines the HTML output of your component. You can use HTML tags, imported components, Astro directives, and Javascript expressions.

Reusable components are called “layouts”. We conventionally use the term “layout” for Astro components that provide common UI elements shared across pages, such as headers, navigation bars, and footers. Layout components are commonly placed in a src/layouts directory in your project for organization, but this is not a requirement.

Props

An Astro component can define and accept props. These props then become available to the component template for rendering HTML. Props can be retrieved using Astro.props.

Slots

The <slot /> element is a placeholder for external HTML content, allowing you to inject child elements from other files into your component template.

Pages

Pages are files that live in the src/pages/ subdirectory of your Astro project. They are responsible for handling routing, data loading, and overall page layout for every page on your website.

Supported file types: .astro, .md / .mdx, .html, .js / .ts

Astro leverages a routing strategy called file-based routing. Each file in your src/pages/ directory becomes an endpoint on your site based on its file path. For example, if you have an About page, about.astro, you can access it automatically using www.example.com/about.

Read more about Routing in Astro.

How to Migrate Your Projects to Astro

Astro has a handful of guides for migrating your existing project from another platform or framework to Astro.

Here are the frameworks and platforms with available guides: 

astro guides

More will be available as the growing Astro community contributes.

Check out the Migration guides here!

If you opt to switch to Astro, you will have to make some adjustments, and the previous themes you had from the other platform will not be carried out to Astro. Instead, you will have to create a new Astro project and select from the available templates or themes provided by the community and Astro.

You can use these commands to use the templates or themes:

astro commands

Although switching to Astro will be a huge change, it will serve you a lot of benefits in the long run, especially on SEO, site speed, and Google page results.

Need an Astro Team for Your Project?

Astro has a growing community of web developers, engineers, and design professionals who can help you create the perfect website. With their help, you can build something that’s tailored to your specific needs.

StarTechUP is a network of experienced professionals specializing in Astro projects. From custom design to complete website builds, our team will help you create something that looks great and works like a charm.

We also offer a full range of services for custom software, backend and frontend engineering, and DevOps. So no matter what your project needs, our team is here to help you make it a success!

Contact us today for more information about how we can help you create something amazing with Astro.

About the author: Andrea Jacinto - Content Writer

A content writer with a strong SEO background, Andrea has been working with digital marketers from different fields to create optimized articles which are informative, digestible, and fun to read. Now, she's writing for StarTechUP to deliver the latest developments in tech to readers around the world. View on Linkedin

MORE INSIGHTS