# Deploy 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.

```sh
bun add @pilvia/astro
```

```js
// astro.config.mjs
import { defineConfig } from 'astro/config';
import pilvia from '@pilvia/astro';

export default defineConfig({
  adapter: pilvia(),
});
```

```json title="pilvia.app.json"
{
  "name": "my-site",
  "auth": "public",
  "build": "dist/pilvia.build.json",
  "egress": { "allow": ["api.eu.mailgun.net"] }
}
```

```sh
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`](/auth/overview.md), [data bindings](/data/overview.md),
  [`env.kv` and `env.cache`](/hosting/kv.md) — 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](/reference/runtime.md#work-after-the-response).
  Astro's own route cache gets the same `waitUntil` for its background
  revalidation.
- Keeps [Astro sessions](#sessions) in `env.kv` unless you configure a
  driver of your own.
- Answers your config [`redirects`](#redirects) as real 301s on a server
  build.

A fully prerendered site with no Actions deploys as a plain
[static site](/hosting/static-sites.md) — 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:

```js
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`](/hosting/kv.md), so they need
the same grant:

```json title="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](/hosting/kv.md#the-consistency-contract-stated-openly).
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:

```js
// 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:

```js title="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](/hosting/github.md) 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](#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](/reference/runtime.md#known-gaps).

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](/reference/runtime.md).
