Skip to main content

Using Bun with Astro and Cloudflare Pages

Updated 6 min readMarkdown

I have a company blog hosted on Cloudflare Pages, which I created based on the Astro template AstroWind. I am always looking to improve the whole development process, so I thought I’d investigate whether Bun provides any benefit, either as a runtime (instead of Node.js) or as a build tool (instead of npm or pnpm).

The source code for the website is on GitHub.

Did Bun Work?

I tested to see if there is any benefit to using Bun as a build tool for Cloudflare Pages. My experiments showed that bun builds my Astro site in 31 seconds, whereas pnpm takes 71 seconds. There is no difference in the rendered output, since Cloudflare Pages uses V8, not bun.

For my purposes it is better to use bun than pnpm, since the build time is more than halved and there is no difference in the output or the Lighthouse scores. I have not tried deploying to a hosting provider such as Kinsta, Render, Northflank, Clever Cloud, Back4App, or Fly.io that uses the bun runtime. The ones I looked at had slower time to first byte, so would not show any benefit. My Lighthouse score for a small static Astro site is currently dominated by browser rendering and network, not delay in the runtime.

Changes you need to make to use Bun as a build tool and package manager

Bun is less forgiving than vite and requires explicit configuration of Tailwind and PostCSS. Once this is done, the same code can be built using pnpm or bun. In addition, I have added a detect-runtime.js file that will ensure that vite is using bun for the build, not node.
It is important to use bun run --bun and not bun run. Please see https://bun.sh/docs/cli/run#bun

How to Build and Deploy with Bun

In order to build this website, install bun, clone the repository from GitHub, change into the directory that git created, then:

bun install
bun run --bun build
bun run --bun preview  #this will allow you to test the site using the bun runtime

When setting up a Cloudflare Pages project, you can elect to automatically deploy from GitHub. If you do that, to deploy it to Cloudflare Pages, just check it in to GitHub, and Cloudflare will start the build process.

You may ask how Cloudflare knows how to use bun instead of npm by looking at this repository. It doesn't. You need to change that in the Settings > Build Configuration for your pages project on the Cloudflare Dashboard. change the Build Command to bun run --bun build

You may ask how Cloudflare knows what version of Bun to use. If you don't specify the BUN_VERSION variable on the Settings for your Pages Project, Cloudflare will use its default version, currently 1.1.33. I would recommend letting Cloudflare choose the version.

Please note that you can't have different build commands for the Production and Preview environments. This is kind of a pain. If you want to have one bun branch and another pnpm branch, you would have to change the build command in the Cloudflare dashboard every time you do a deployment.

The rest of this article discusses the AstroWind template and Cloudflare Pages.

Why choose Astro and AstroWind?

Astro is a modern Jamstack framework. It is particularly well-suited for websites with a lot of static content, although can be made to do anything. I’d suggest reading more at Astro’s website.

AstroWind is a template designed for blogging with Astro. It uses Tailwind CSS and has a modern look.

Choice of hosting provider

I investigated several hosting providers. Eventually I settled on Cloudflare Pages because Cloudflare are experts in DDoS mitigation, and I didn't want to get stuck with a large traffic bill if my website was attacked. This has happened to Netlify customers, for instance.

Cloudflare is among the fastest hosting providers: https://bejamas.io/compare/github-pages-vs-firebase-vs-cloudflare-pages

Firebase is the fastest hosting provider, but this comes at a cost. For me, the 40 ms penalty for using Cloudflare Pages was worth it for the peace of mind of not having to worry about DDoS attacks.

The Largest Contentful Paint (LCP) of this site is 340 milliseconds according to Lighthouse on my 1 Gigabit connection on my desktop computer. In order to achieve this, you need to make a few choices on Cloudflare:

  • Use Cloudflare’s nameservers. You do not have to transfer your domain to Cloudflare, although you could if you do not have a premium domain. In my case, I could not transfer my domain to Cloudflare’s registrar.

  • In your DNS, CNAME both your root and www to your pages url: https://yourpage.pages.dev and make sure they are proxied. Cloudflare is a bit unique as a DNS provider in that they do not allow you to set A records. This is by design. They do not expose these IP addresses. You set the CNAMES and Cloudflare takes care of everything else:

  • Do not have any redirect rules from your root to www. This is important. Any rules will cause another HTTP request, creating unnecessary delay.

  • Do not set cache headers or instructions about Cloudflare’s cache. Let Cloudflare do that.

    In most situations, you should avoid setting up any custom caching on your site. Pages comes with built in caching defaults that are optimized for caching as much as possible, while providing the most up to date content. Every time you deploy an asset to Pages, the asset remains cached on the Cloudflare CDN until your next deployment.

  • In the settings for your domain (not the Astro site), under Speed > optimization > Protocol optimization make sure that HTTP/3 and HTTP/2 to Origin are enabled. You probably can enable everything on the protocols tab.

  • On the Content optimization tab, turn off Speed Brain and Rocket Loader. I haven't found a problem with Cloudflare Fonts or Early Hints, so you can leave those on.

  • Under Caching > Configuration, set the Caching level to Standard.

  • On the same tab, set the Browser Cache TTL to respect existing headers.
    Cloudflare Pages will set these. Because Cloudflare Pages is well aware of your deployment history, it is in a good position to instruct the browser about when to request new assets.

  • On the same tab, set Always Online to be true. Cloudflare will use an internet archive in case your site is down.

  • Under Caching > Tiered Cache, use their Smart Tiered Caching Topology.

  • On your Workers & Pages dashboard, set up Custom Domains for both your root and www.

It might be possible to further improve LCP by preloading the hero image. Astro does not really support this. There are workarounds, but I haven't gotten them to work.

About the Astro Template

The AstroWind template version I used has a bug where it will optimize even images in /public. This is a problem if you need to post your blog entries on LinkedIn, for instance, which does not accept webp files.

I didn't want my blog entries to be displayed by date. I wanted to be able to order them the way I wanted to. So this repository orders blog entries by file name.

A Final Word

Thank you for reading this. I hope it helps you. There are certainly things that could be improved. Please drop me a line if you have any comments or questions.

No comments yet. Be the first to comment.