Islands and deployment

Choose static or on-demand rendering

Start from static output and add a server adapter when request-time behavior is a real requirement.

Astro prerenders every route by default. The frontmatter runs once, during the build, and the result is a folder of HTML files. Any static host can serve them. No server runs when a visitor arrives.

That is the right default for the notes site. The content changes when we deploy, and not before.

When static is not enough

Some pages can’t be built ahead of time, because the answer depends on who is asking. A page that reads a session cookie. A dashboard for a signed-in user. A stock count that changes every minute. A form that receives a POST.

For those routes, you want on-demand rendering: the frontmatter runs on a server, once per request.

It takes two steps. First, install an adapter for the platform you deploy to:

npx astro add cloudflare

There are official adapters for Cloudflare, Netlify, Vercel, and Node.js. The command installs the package and adds it to astro.config.mjs.

Then opt out of prerendering on the one route that needs it:

---
export const prerender = false

const session = Astro.cookies.get('session')
---

<p>{session ? 'Signed in' : 'Guest'}</p>

Every other route in the project stays static. Only this page runs on the server. Notice that Astro.cookies only makes sense here. A static page has no request to read a cookie from.

Flipping the default

If most of your routes are dynamic, set output: 'server' in the config. Now every route renders on demand, and you opt back in to static with export const prerender = true on the pages that don’t need a server, like an about page.

My advice is to stay in the default static mode until you are sure most pages need a server. Switching to 'server' adds no features. It only changes the default.

What you are trading

The choice changes two things.

The first is the age of the data. A static route can only show what existed at build time. An on-demand route shows what exists right now.

The second is what can break. A static site is a folder of files, and a CDN serving files rarely goes down. An on-demand route depends on a runtime, environment variables, a database, and latency. Every visit runs code, and every visit can fail.

A common mistake

Add export const prerender = false without an adapter and the build stops with NoAdapterInstalled: Cannot use server-rendered pages without an adapter. The fix is the astro add command above. Astro won’t guess your platform.

Decide per route, not per project. For each dynamic route, write down the request-specific input that forces it to exist. For the notes site, that list is empty, so it stays fully static.

Lesson completed