Exploring Astro: A Journey into Modern Web Development with Astro JS
Tags: frontend, astro
Published: 2024-05-20 09:51:40

I was today years old when I learned about Astro. I know maybe I live under a rock as I discovered that Astro has been a very famous front-end solution for quite some time.

**What is Astro?**

Astro JS is a modern web framework designed to help developers build faster websites. It allows you to write your website using multiple web technologies like React, Vue, or Svelte, but it sends the final page to the user's browser as mostly static HTML. This makes the page load faster because the browser doesn't have to do as much work to display the page. Astro also only loads the JavaScript needed for interactivity when it's needed, which keeps pages light and fast.

Astro JS is a versatile framework that can be used in various scenarios to enhance web development projects.

1. **Static Websites**: Astro is excellent for building static websites, such as blogs, portfolios, and informational sites. It compiles to static HTML by default, which can be hosted on any static site hosting service, resulting in fast loading times and reduced server costs.

2. **Content-Heavy Sites**: For websites that need to manage and display large amounts of content, like news sites or educational platforms, Astro can be very effective. It allows developers to fetch data at build time and render it into the HTML, ensuring quick load times and a smooth user experience.

3. **E-commerce Platforms**: Astro can be used to build e-commerce sites where performance and speed are critical for user retention and SEO. Astro's efficient handling of JavaScript ensures that the user interface remains responsive and engaging without sacrificing load times.

4. **Multi-Page Applications (MPA)**: For projects where a single-page application (SPA) might be overkill, Astro provides an excellent foundation for building multi-page applications. It allows each page to be independently developed and optimized, using the best-suited frameworks and libraries as needed.

5. **Personalization and Dynamic Elements**: Even though Astro is optimized for static generation, it supports client-side JavaScript to add interactivity and dynamic content as needed. This makes it suitable for sites that require user interaction and personalization without loading unnecessary JavaScript upfront.

6. **Documentation Sites**: Astro's ability to integrate with various front-end frameworks seamlessly makes it ideal for documentation websites that may need interactive examples and demos embedded within the content. This is useful for tech companies and open-source projects.

7. **Progressive Web Applications (PWA)**: Astro's lightweight output and efficient asset handling make it a good choice for developing progressive web applications. PWAs built with Astro can benefit from fast loading times and reliable performance even in poor network conditions.

Astro's flexibility in integrating multiple UI frameworks and its focus on performance optimization make it a compelling choice for a wide range of web development projects.

Let's get our hands dirty enough talking.

```bash 
npm create astro@latest
```

![alt text](https://eneaslari.com/958d6005-2d4d-4335-af5f-cc192807565b.png "new image file")

Then, if you want to follow this tutorial, select the same choices as me in the subsequent questions.

![alt text](https://eneaslari.com/930a71d1-1e53-401b-b9e4-9879c6617e71.png "new image file")

Navigate to the newly created directory and run `npm run dev`.

![alt text](https://eneaslari.com/a9bc6c4b-37a1-4473-97de-aace621a9542.png "new image file")

If all goes well, Astro should now be serving your project on http://localhost:4321/

Astro will listen for live file changes in your src/ directory, so you will not need to restart the server as you make changes during development.

Next, let's install the Astro extension on VSCode.

![alt text](https://eneaslari.com/ae535d6b-af67-4a3e-b6cb-1b53e0dc1a2b.png "new image file")

Now, as you can see, the syntax is highlighted.

![alt text](https://eneaslari.com/6b1c4d7f-59ab-44db-9cda-98f841001841.png "new image file")

Next, we will add a new component. Create a file with the name `Test.astro` under the Components directory.

```bash
---
interface Props {
  name: string
}

const { name } = Astro.props
---

<p>This is a test component with a prop {name}</p>
```

Now, in order to use it in our index file, add the component at index.astro.

```bash
import Test from '../components/Test.astro'
```

Under the `h1` element, add the component.

```bash
<Test name='My name' />
```

You will see:

![alt text](https://eneaslari.com/1c9d3bfe-3df0-4b48-aa36-f4d93e97afa7.png "new image file")

**Adding a New Page**

1. **Create a New File**:
   - Navigate to the `src/pages` directory in your Astro project. This directory is where Astro looks for files to turn into pages on your website.
   - Create a new file with an `.astro` extension for an Astro component or a `.html` extension if you prefer plain HTML. The name you give this file will be used in the URL of your new page. For example, if you create `about.astro`, the URL for this page would be `/about`.

2. **Add Content to the New Page**:
   - Open your new file in a text editor or IDE.
   - You can write HTML directly in this file. If you're using an `.astro` file, you can also utilize components and other features specific to Astro. Here’s a simple example:

   ```astro
   ---
   // This is the front matter section where you can include JavaScript and import components
   ---

   <html>
     <head>
       <title>About Page</title>
     </head>
     <body>
       <h1>Welcome to the About Page</h1>
       <p>This is an example of an Astro page.</p>
     </body>
   </html>
   ```

   - Open your web browser and go to http://localhost:4321/about to see your new page in action.

![alt text](https://eneaslari.com/58c471d7-84a6-4f05-a83d-e19e061f1ece.png "new image file")

Creating a page with dynamic routes in an Astro project allows you to handle multiple URLs with a single file, typically used for cases like blog posts, product pages, or user profiles where the content changes based on some variable (like an ID). Here’s how you can set up dynamic routing in Astro:

**Step 1: Create a Dynamic Route File**

1. **Navigate to the `src/pages` Directory**:
   - This is where Astro looks for files to create pages from.

2. **Create a New File with a Dynamic Segment**:
   - Name the file with brackets to indicate a dynamic segment. For example, if you want to create dynamic blog post pages where each post has a unique ID, you might name your file `[id].astro`. Here, `id` will be the variable holding the dynamic part of the URL.

**Step 2: Access the Route Parameter in Your Page**

1. **Open the New Dynamic Route File**:
   - Edit the file to handle the dynamic content. You can access the dynamic parameter using the `Astro.request` object.

2. **Example Code for a Dynamic Blog Post Page** (`[id].astro`):

```astro
---
import Layout from '../layouts/Layout.astro'
const { id } = Astro.params

export async function getStaticPaths() {
  return [{ params: { id: 'test' } }, { params: { id: 'test2' } }, { params: { id: 'test3' } }]
}
---

<Layout title='Post'>
  <h1 style='color: white'>{id}</h1>
</Layout>
```
Now visit http://localhost:4321/test

And you will see the page.

![alt text](https://eneaslari.com/2f2c3353-d7fa-44e2-b553-8ed12c7e927a.png "new image file")

That's all for now.
We will explore more about Astro as it seems very interesting.