Skip to content

Building a Static Blog with Netlify and Astro

Published: at 05:07 PM
Loading...

1. Register for Netlify

First, open the official Netlify website click here to jump

Choose a way to register and log in. Here I choose GitHub login, which is the first method.

image-20250126141222970

Then we authorize the use of GitHub login.

image-20250126141411824

After that, some pages for filling in information will pop up. The content inside represents name, plan, role, etc. We can write roughly and check the tags.

image-20250126142026081

If we select the Personal option in How are you planning to use Netlify?, which is the personal project option, at the end we will be asked to create a project name, which can later be used as an identifier in the URL. Here we can design it according to our personalized needs.

image-20250126142352840

After completion, click Continue to deploy, and a popup will appear asking us to set up the first project. Here we click Skip this step for now, which means skip this step.

image-20250126142514856

After the above steps, we have successfully registered for Netlify.

2. Install Astro

First, we visit the official Astro documentation website, click here to jump

Then we click “Install Astro”

image-20250126150134573

On the new page, it shows there are three prerequisites:

image-20250126150232186

According to the prerequisites in the documentation, we need a Node.js environment, a text editor, and a terminal.

The installation and configuration of the Node.js environment can refer to the following tutorial:

Node.js Installation Tutorial, click to jump

The latter two can use the system’s built-in ones. Considering a more comfortable coding environment, we can download and use VSCode, a text editor. The download and installation method can refer to the following tutorial:

VSCode Installation Tutorial, click to jump

After all the above prerequisites are completed, we officially start the Astro installation.

We first create a blank folder

image-20250126151529771

Then enter cmd in the address bar and press Enter

image-20250126151626715

In the popped-up terminal, enter the following command and enter y in the popped-up prompt:

image-20250126152541823

npm create astro@latest

After waiting for a while, it will prompt to select the directory for the new project. We can enter ./glader-blog, which means creating a new folder glader-blog in the current directory and storing the project here.

image-20250126152711786

After that, we press Enter 3 times in a row to enter the project initialization waiting

image-20250126152753352

When the following content is prompted, it means the project initialization has ended. At this time, our folder will have an additional glader-blog directory, and our project is stored in it:

image-20250126153013217

image-20250126153020024

Then we enter the created project and use npm install to install dependencies, as follows:

image-20250126153505274

Astro has a built-in server. After waiting for the installation to complete, we run the command npm run dev to run the server.

image-20250126160811452

We can enter http://localhost:4321 in the browser to access the preview.

After entering this URL in the browser and running, the result is as follows, which indicates that the Astro project was successfully installed.

image-20250126161025204

3. Familiarize with Astro’s Development Environment

For our convenience in writing code, we use VSCode, a text editor. First, we open VSCode and use VSCode to open the newly created project, which is glader-blog, as follows. At this time, the Astro extension installation will pop up in the lower right corner, and we click to install:

image-20250126161445199

If this prompt doesn’t pop up, we can click the extension icon and search to install:

image-20250126161611608

image-20250126161657370

After the installation is complete, we return to the project directory and find an additional npm script option in the lower left corner. We can directly click on the content inside to help us run npm scripts. For example, there is a dev option inside. Clicking this is equivalent to running npm run dev in the project directory just now.

image-20250126161756711

image-20250126162817282

image-20250126162829988

You can see that the effect is the same as entering the command directly in the small black window just now.

4. Write a Simple Blog

Under src/pages/ in the project directory, there is an index.astro file, which is the main page of the website.

image-20250126163327161

We can try to delete all the code in this file, then rewrite a piece of code in this file, the code is as follows:

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" type ="image/svg+xml" href="/favicon.svg" />
    <meta name="viewport" content="width=device-width" />
    <meta name="generator" content = {Astro.generator} >
    <title>Astro</title>
  </head>
  <body>
    <h1>Astro</h1>
  </body>
</html>

At this time, we can find in the browser that the content of the webpage has become the content in our code.

image-20250126163611792

Astro is essentially a static website generator that can be compatible with native HTML code. For the convenience of demonstration, here we first roughly write a main interface of a blog, the code is as follows:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="/styles.css">
  </head>
  <body>
    <!-- Navigation Bar -->
    <header>
      <nav>
        <ul>
          <li><a href="/">Home</a></li>
          <li><a href="/about">About Me</a></li>
          <li><a href="/posts">Blog Posts</a></li>
          <li><a href="/contact">Contact Us</a></li>
        </ul>
      </nav>
    </header>

    <!-- Main Content Area -->
    <main>
      <section class="intro">
        <h1>Welcome to My Blog!</h1>
        <p>Here I record my thoughts and sharing about programming, technology, and life.</p>
      </section>

      <!-- Latest Articles List -->
      <section class="posts">
        <h2>Latest Articles</h2>
        <ul>
          <li><a href="">Test Article 1</a></li>
        </ul>
      </section>
    </main>

    <!-- Footer -->
    <footer>
      <p>&copy; 2025 My Blog. All Rights Reserved.</p>
    </footer>
  </body>
</html>

This is a basic blog structure. You can continue to expand and customize it according to your needs.


Previous Post
Hastily Completed Graduation Project
Next Post
Domain Migration