Astro SSR with Bun

Bun brings a ton of interesting new APIs to the table, and since migrating my blog to use Bun as a build runtime and package manager, I’ve experimented a little with Bun in server-side rendering. Here’s a quick post on how to setup an Astro project with Bun on the server.

CAUTION
While Bun does support Astro, there are known issues both in dev and in production. Issues should be reported to the Bun project. I don’t recommend using this beyond experimentation & fun projects for the time being.

Add the @astrojs/node adapter

Bun advertises Node API compatibility, which allows us to take advantage of the existing Node adapter.

Terminal window
bun astro add node

You can then run the dev server with the following:

Terminal window
bun --bun astro dev

And build it with:

Terminal window
bun --bun astro build

To run the built server, call the entrypoint:

Terminal window
bun ./dist/server/entry.mjs

Add bun-types

Bun exposes types via the bun-types package. Install the package

Terminal window
bun add bun-types

And add it to your tsconfig.json

tsconfig.json
"compilerOptions": {
"types": ["bun-types"]
}

Using the runtime

You can now use the Bun runtime in your pages (and take advantage of the typed intellisense). For example, you can reference Bun.version to get the current installed version of Bun.

src/pages/index.astro
<h1>{Bun.version}</h1>

Bun Template

You can find my template for Astro + Bun SSR on Github.

A Recipe For Buns

I’ve written a more complete recipe on using Bun with Astro projects, which you can find in the official Astro docs.