Skip to content
App
Markdown

Astro

The @pilvia/astro adapter — prerendered pages become assets, on-demand routes run in the isolate.

Astro sites run on Pilvia App through our own adapter, @pilvia/astro. Astro 6 and Astro 7 are supported from @pilvia/astro 0.5.0; an Astro 5 site stays on 0.4.x. Prerendered pages and assets are served by the host; on-demand pages, endpoints and Astro Actions run in an isolate next to your data.

bun add @pilvia/astro
// astro.config.mjs
import { defineConfig } from 'astro/config';
import pilvia from '@pilvia/astro';

export default defineConfig({
  adapter: pilvia(),
});
pilvia.app.json
{
  "name": "my-site",
  "auth": "public",
  "build": "dist/pilvia.build.json",
  "egress": { "allow": ["api.eu.mailgun.net"] }
}
astro build
pilvia app deploy

That is the whole change, and switching back to another adapter is the same one line in the other direction.

What the adapter does

  • Builds the server half for a web-platform isolate — every dependency bundled, since there is no node_modules at runtime.
  • Emits the platform contract, export default { fetch(request, env, ctx) }, from astro/app. Nothing platform-specific ends up in your code.
  • Writes dist/pilvia.build.json after the build: where the server entry and client assets are, and which routes Astro renders on demand — derived from Astro's own route manifest. pilvia app deploy reads it via "build"; nobody hand-writes a routes list.
  • Surfaces the platform's capability object as Astro.locals.env (context.locals.env in endpoints, middleware and Actions): env.user, data bindings, env.kv and env.cache — whatever the manifest grants, typed. PilviaKv and PilviaCache are exported for your own helpers. Astro.clientAddress is the real client address.
  • Surfaces the platform's ctx as Astro.locals.ctx (since 0.6.0), so Astro.locals.ctx.waitUntil(promise) finishes work after the response: see work after the response. Astro's own route cache gets the same waitUntil for its background revalidation.
  • Keeps Astro sessions in env.kv unless you configure a driver of your own.
  • Answers your config redirects as real 301s on a server build.

A fully prerendered site with no Actions deploys as a plain static site — no isolate at all. Requests route asset → declared route → fallback as everywhere on the platform, so a prerendered page is served without starting your code.

Redirects

With output: "server", every entry in astro.config.mjs → redirects is a declared route, and the isolate answers it with a 301 (308 for methods other than GET), from @pilvia/astro 0.4.1. Before that, a redirect whose destination was a prerendered page was itself prerendered as a meta-refresh page and served as a 200.

With the default output: "static", redirects stay Astro's meta-refresh pages, so a static site still deploys with no isolate. To get real 301s, set output: "server" and mark your pages export const prerender = true.

A redirect keeps the request's query string, from @pilvia/astro 0.4.2: /old?gclid=abc answers Location: /new?gclid=abc, so ad and campaign parameters survive it. A destination that carries a query of its own keeps that one instead, and a fragment in the destination stays after the query.

To redirect a whole subtree, use Astro's rest parameter:

redirects: {
  "/old-section/[...path]": "/new-section",
},

/old-section/* is Cloudflare _redirects syntax. Astro never matches it, so on Pilvia App such a path answers 404.

Sessions

Astro.session works with no driver of your own, from @pilvia/astro 0.4.0. Sessions live in the app's env.kv, so they need the same grant:

pilvia.app.json
{ "name": "my-site", "auth": "public", "build": "dist/pilvia.build.json", "kv": true }

A session write is durable before the response leaves, and across replicas it follows env.kv's consistency contract. Each session is the key astro-session:<id>. A session nobody writes to for thirty days is dropped from the store — or after Astro's session.ttl or the cookie's maxAge, if either is longer — and every write starts the period again. To set it yourself:

// astro.config.mjs
import pilvia, { sessionDriver } from '@pilvia/astro';

export default defineConfig({
  adapter: pilvia(),
  session: { driver: sessionDriver({ ttl: 7 * 24 * 60 * 60 }) },
});

A site that never touches Astro.session never reaches the store and needs no grant. Without the grant, the first session read fails with an error that names the missing line.

Configuration and secrets

The isolate has no environment variables, and astro:env secrets are not wired. A server-side key is compiled in at build time with Vite's define, from the environment of the machine that builds:

astro.config.mjs
import { defineConfig } from 'astro/config';
import { loadEnv } from 'vite';
import pilvia from '@pilvia/astro';

// "" reads unprefixed names too: this one is for the server only.
const { MAILGUN_KEY = '' } = loadEnv('production', process.cwd(), '');

export default defineConfig({
  adapter: pilvia(),
  vite: { define: { __MAILGUN_KEY__: JSON.stringify(MAILGUN_KEY) } },
});

Server code reads it as the global __MAILGUN_KEY__. Use it in server code only: define replaces the name wherever it appears, a client script included. The value is stored with the release, and a GitHub build has no variables of its own, so a site that needs a key deploys from the CLI.

Why an adapter of our own

We do not emulate other platforms' runtimes. A build made for another vendor's adapter depends on that vendor's runtime APIs, and a compatibility shim chasing someone else's platform is a promise we refuse to make. What stays universal is the web-standard fetch-handler shape — any framework reaches the platform through it. adapter: pilvia() is the supported path for Astro; adapters for other frameworks follow when real projects bring them.

Limits, honestly

  • Secrets do not arrive at runtime and astro:env secrets are not wired; see Configuration and secrets.
  • Images are passthrough at runtime — sharp is native code and cannot run in the isolate. Prerendered pages keep build-time optimization.
  • Node built-ins: the isolate is a web-platform runtime, not Node. Buffer is provided; other node:* modules are not.
  • Locale data for English, Finnish and Swedish only: Intl and the toLocale* methods format those three properly, and any other locale falls back to neutral formats; see Runtime compatibility.

These are engine gaps, not product decisions — each one closes as the engine catches up, and none of the workarounds live in your code.

What the isolate itself provides, and the short list of stand-ins the platform adds for every app, is in Runtime compatibility.

Type to search. ↑↓ to move, Enter to open, Esc to close.