“Astro.clientAddress is not available” means you read the visitor’s IP address on a page that has no visitor: a prerendered page built ahead of time, or an adapter that doesn’t provide the address. Astro has three versions of this error. For prerendered pages the message is “Astro.clientAddress is only available on pages that are server-rendered.” The fix is to render that route on demand with export const prerender = false and an adapter installed.
Key Takeaways
- A prerendered page is built once, with no request and no IP address.
clientAddresscan’t exist there.- Add an adapter and set
export const prerender = falseon the route that needs the IP.- Middleware runs at build time for prerendered pages too; guard IP reads there.
- If the adapter itself lacks support, the message names it and asks you to file an issue with the adapter.
The three messages
| Title | Message | Cause |
|---|---|---|
Astro.clientAddress is not available in prerendered pages. |
Astro.clientAddress is only available on pages that are server-rendered. |
Static build or no adapter |
Astro.clientAddress cannot be used inside prerendered routes. |
Astro.clientAddress cannot be used inside prerendered route src/pages/x.astro. |
Route has prerender true in a server project |
Astro.clientAddress is not available in current adapter. |
Astro.clientAddress is not available in the name adapter. File an issue with the adapter to add support. |
Adapter doesn’t pass the IP |
Fix 1: render the route on demand
Install an adapter for your host, then opt the route out of prerendering:
# Terminal: pick the adapter for your host
npx astro add node
---
// src/pages/whoami.astro
export const prerender = false;
const ip = Astro.clientAddress;
---
<p>Your IP address is {ip}.</p>
Leave the rest of the site static. Only routes with prerender = false run per request. The hybrid rendering tip shows how to mix the two.
API endpoints work the same way, using context.clientAddress:
// src/pages/api/ip.ts
import type { APIRoute } from 'astro';
export const prerender = false;
export const GET: APIRoute = ({ clientAddress }) =>
new Response(JSON.stringify({ ip: clientAddress }), {
headers: { 'Content-Type': 'application/json' },
});
Fix 2: guard middleware
Middleware also runs while prerendering pages at build time. Reading context.clientAddress there throws for every static page. Check isPrerendered first:
// src/middleware.ts
import { defineMiddleware } from 'astro:middleware';
export const onRequest = defineMiddleware((context, next) => {
if (!context.isPrerendered) {
context.locals.ip = context.clientAddress;
}
return next();
});
Fix 3: the adapter doesn’t support it
The official adapters for Node.js, Netlify, Vercel and Cloudflare provide the client address. A community adapter might not. Check its docs or open an issue, as the message suggests. Behind a proxy or CDN, the address may also be the proxy’s IP; your host’s docs explain which forwarded header carries the real one.
Do you need the IP at all?
For rate limiting or region detection, hosts often expose better signals (geolocation headers, bot scores) than a raw IP, and keeping pages static is cheaper and faster. Store IPs only if you have a reason and a privacy policy that covers it.
The get client IP tip covers proxies in more detail, the Astro SSR guide explains adapters, and the official on-demand rendering docs cover prerender. Our Astro config docs show where the adapter goes in a Komal theme; the Astro themes themselves are fully static.
FAQ
Why is Astro.clientAddress undefined in dev?
It throws rather than returning undefined. In dev, it works on routes with prerender = false once an adapter is installed. On prerendered routes it errors because there’s no request to read.
Can I get the visitor’s IP on a static Astro site?
Not at build time. Use an on-demand route or endpoint, or a serverless function your host provides, and call it from the page if needed.
Which adapters support Astro.clientAddress?
The official Node.js, Netlify, Vercel and Cloudflare adapters provide it. If a community adapter doesn’t, the error names that adapter, and the fix belongs in the adapter.