Skip to content

Building a blog with Astro in 2023

Reinier Kors
Published Oct 23, 2023 – Last Updated Oct 24, 2023

In this blog post I will explain how I built this blog with Astro and Cloudflare Pages. It’s a fast & lightweight static site that scores a ~100% on Google PageSpeed Insights.

It’s a great solution if you want to build a fast blog with a modern framework and deploy it to your own custom domain for free.

I won’t go into great detail about how to customise Astro, but I will explain the basics and provide links to in-depth resources.

Contents

Open Table Of Contents

  1. Why Astro?
  2. Installation
    1. Prerequisites
    2. Theme selection
    3. Installation
  3. Cloudflare Pages
    1. Build Caching
    2. Costs
  4. Performance
    1. Google PageSpeed Insights
  5. Conclusion

Why Astro?

AstroExternal link icon is a modern web framework for building fast and optimised websites. It allows you to write less JavaScript, resulting in faster loading times. Astro achieves this by delivering HTML first, and then loading minimal JavaScript as needed. This approach makes it a great choice for building a blog, as it allows for excellent performance and a great user experience.

Here are some features of Astro that contribute to its suitability for blog creation:

  • Modern web framework: Astro is designed for building fast and optimised websites. It prioritises delivering HTML first and loading minimal JavaScript as needed, resulting in faster loading times and a better user experience.

  • Flexible rendering strategies: Astro supports many popular rendering strategies out of the box, including Static Site Generation (SSG) and Server-Side Rendering (SSR). This flexibility makes it suitable for a wide range of projects.

  • Framework support: With Astro, you can write components using your favourite JavaScript framework, or no framework at all. Astro will render them to static HTML and CSS. This means you can use React, Vue, Svelte, or just plain JavaScript to create components.

  • Great documentation and community: Astro has comprehensive documentation and a supportive community, making it a great choice for both beginners and experienced developers.

Installation

Prerequisites

Before setting up your new Astro project, you will need to install a recent Node.js LTSExternal link icon version. Check the Astro documentationExternal link icon to see the latest required NodeJS version.

While not required, it’s also recommended to use VS CodeExternal link icon as your code editor and use the official Astro extensionExternal link icon.

Theme selection

After installing the prerequisites, I recommend looking for an existing theme to use as a starting point. This will save lot’s of time, as you won’t need to rebuild all features yourself. You can find a list of themes on the Astro websiteExternal link icon.

Here are some of my favourite Astro themes:

Astro Starter Kit: Blog

  • Astro Starter Kit: BlogExternal link icon - Official Blog Starter Kit, has minimal styling, comes with the basics for a blog. I’d recommend this one if you want to start fresh and build something yourself.

AstroPaper

  • Astro PaperExternal link icon - I am using a customised version for this blog, mostly because it comes with Fuzzy Search, RSS feeds, Sitemaps, Dynamic OG image generation & TailwindCSS out of the box.

AstroWind

  • AstroWindExternal link icon comes with lots of features out of the box, including RSS feeds & Astro 3.0 support.

Accessible Astro Starter

  • Accessible Astro StarterExternal link icon is a pre-configured, SEO-optimized, and accessibility-friendly blogging theme. It comes with a variety of accessible components for creating various types of pages.

AstroShip

  • AstroShipExternal link icon is a marketing website template. It’s a great starting point for building a landing page or marketing website.

Astrofy

Let me know if you find good working other themes.

Installation

After picking a theme, you can install Astro and the theme using one of the following commands:

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

# create a new project based on a GitHub repository’s main branch
npm create astro@latest -- --template <github-username>/<github-repo>

This will launch the creation wizard. Follow the steps to setup your new project. Finally, cd to your new folder. You can then run npm run dev to start the development server.

Your new project contains the following 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.
tsconfig.json - A TypeScript configuration file.

Cloudflare Pages

For simplicity, I recommend using Cloudflare PagesExternal link icon to deploy your Astro blog. It’s free, fast and easy to set up. I assume you already have a Cloudflare account, if not, you can sign up hereExternal link icon.

When you’re logged in, go to Workers & Pages in the sidebar. Then click on the Pages tab and select Connect to Git. You will then have to give Cloudflare access to your GitHub account. After that, you can select the repository you want to deploy. You can also choose a branch and a build command.

  • For Astro:, the build command is npm run build. And the build output directory is dist.

Cloudflare Pages - Create an Application

By default, your site gets deployed to a worker subdomain, this will be something like your-site-name.pages.dev. You can change this by going to the Custom Domains tab and adding your own domain. You will have to add a CNAME record to your DNS settings.

Build Caching

Cloudflare Pages - Build Cache

I also recommend enabling build caching. This can speed up your build times significantly. You can do this by going to the Builds & Deployments tab and enabling Build Caching. My own build times went from 48 seconds to 29 seconds after enabling build caching.

Costs

Cloudflare Pages Pricing

Cloudflare Pages is free if you stay within the following limits:

  • 500 builds per month (one for every commit / manual build)
  • 1 build at a time.
  • Unlimited bandwidth
  • Unlimited static requests

This makes it a great choice for building a blog.

Performance

Google PageSpeed Insights

The PageSpeed Score ranges from 0 to 100 points. A higher score is better and a score of 85 or above indicates that the page is performing well. Please note that PageSpeed Insights score fluctuates due to various reasons like network latency etc. Also, the score considers total blocking time and largest contentful paint as the main factors.

Google PageSpeed Insights

When building for production, I also use the excellent JampackExternal link icon, a post-processing tool to optimise the static output Astro produces. Jampack can remove unused CSS, inline critical CSS, and minify HTML, CSS, and JavaScript. This can further improve your PageSpeed score.

Conclusion

In conclusion, Astro is a powerful web framework for building fast, optimised websites. Coupled with Cloudflare Pages for deployment and Jampack for post-processing, you can achieve excellent performance scores. Remember to enable build caching to speed up your build times and monitor your PageSpeed Insights score to ensure your site is performing well. Happy coding!

Comments