Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/withastro/astro/llms.txt

Use this file to discover all available pages before exploring further.

The Astro global object is available in all .astro components and provides access to request data, routing information, and utilities for rendering.

Properties

Astro.props
Record<string, any>
Component props passed from parent components or from getStaticPaths().
---
// MyComponent.astro
const { title, description } = Astro.props;
---
<h1>{title}</h1>
<p>{description}</p>
Astro.request
Request
A standard Request object containing information about the current request.
---
const userAgent = Astro.request.headers.get('user-agent');
const method = Astro.request.method;
---
Astro.url
URL
A URL object constructed from the current request URL. Equivalent to new URL(Astro.request.url).
---
const pathname = Astro.url.pathname;
const searchParams = Astro.url.searchParams;
const origin = Astro.url.origin;
---
<p>Current path: {pathname}</p>
Astro.params
Record<string, string | undefined>
An object containing the values of dynamic route segments matched for this request.
---
// src/pages/blog/[slug].astro
const { slug } = Astro.params;
---
<h1>Post: {slug}</h1>
Astro.site
URL | undefined
The site from your Astro config, parsed as a URL instance. Returns undefined if not set.
---
const siteUrl = Astro.site;
---
<link rel="canonical" href={new URL(Astro.url.pathname, Astro.site)} />
Astro.generator
string
A string representing the Astro version, in the format "Astro v5.x.x".
<meta name="generator" content={Astro.generator} />
Astro.cookies
AstroCookies
Utilities for reading and manipulating cookies in on-demand routes.
---
const sessionId = Astro.cookies.get('session');
Astro.cookies.set('theme', 'dark', {
  path: '/',
  maxAge: 60 * 60 * 24 * 7 // 1 week
});
---
Astro.locals
App.Locals
An object that middleware can use to store extra information related to the request.
---
// Access value set in middleware
const user = Astro.locals.user;
---
<p>Welcome, {user.name}!</p>
Astro.clientAddress
string
The IP address of the client making the request. Only available in on-demand routes.
---
const ip = Astro.clientAddress;
---
Astro.response
ResponseInit & { readonly headers: Headers }
A standard ResponseInit object for modifying the outgoing response.
---
Astro.response.status = 404;
Astro.response.headers.set('X-Custom-Header', 'value');
---
Astro.self
AstroComponentFactory
Allows a component to recursively call itself.
---
const { items } = Astro.props;
---
<ul>
  {items.map((item) => (
    <li>
      {Array.isArray(item) ? (
        <Astro.self items={item} />
      ) : (
        item
      )}
    </li>
  ))}
</ul>
Astro.slots
object
An object containing utility functions for working with slotted children.

Methods

Astro.slots.has
(slotName: string) => boolean
Check whether content for a slot name exists.
---
const hasHeader = Astro.slots.has('header');
---
{hasHeader && <header><slot name="header" /></header>}
<slot />
Astro.slots.render
(slotName: string, args?: any[]) => Promise<string>
Asynchronously renders the contents of a slot to a string of HTML.
---
let html = '';
if (Astro.slots.has('default')) {
  html = await Astro.slots.render('default');
}
---
<Fragment set:html={html} />
Astro.currentLocale
string | undefined
The current locale computed from the URL of the request. Only available when i18n routing is configured.
---
const locale = Astro.currentLocale; // 'en', 'fr', etc.
---
Astro.preferredLocale
string | undefined
The best match between visitor’s browser language preferences and supported locales. Only available in on-demand routes.
---
const preferred = Astro.preferredLocale;
---
Astro.preferredLocaleList
string[] | undefined
List of all locales that are both requested by the browser and supported by the site. Only available in on-demand routes.
---
const locales = Astro.preferredLocaleList;
---
Astro.isPrerendered
boolean
Whether the current route is prerendered or not.
---
if (!Astro.isPrerendered) {
  // Use on-demand features
}
---
Astro.routePattern
string
The route pattern for the current route, stripped of the srcDir and pages folder.
---
// src/pages/index.astro -> '/'
// src/pages/blog/[slug].astro -> '/blog/[slug]'
const pattern = Astro.routePattern;
---
Astro.originPathname
string
The original pathname before any rewrites were applied. Useful for tracking the original URL.
---
const original = Astro.originPathname;
const current = Astro.url.pathname;
---

Methods

Astro.redirect
(path: string, status?: number) => Response
Create a response that redirects to another page.
---
import type { APIContext } from 'astro';

export function GET({ redirect }: APIContext) {
  return redirect('/login', 302);
}
---
Astro.rewrite
(rewritePayload: RewritePayload) => Promise<Response>
Serve content from a different URL or path without redirecting the browser.
---
export async function GET(context) {
  return context.rewrite('/new-page');
}
---
Astro.callAction
<TAction>(action: TAction, input: Parameters<TAction>[0]) => Promise<ActionReturnType<TAction>>
Call an Action handler directly from your Astro component.
---
import { actions } from 'astro:actions';

const result = await Astro.callAction(actions.getPost, { postId: 'test' });
---
Astro.getActionResult
<TAction>(action: TAction) => ActionReturnType<TAction> | undefined
Get the result of an Action submission when using a form POST.
---
import { actions } from 'astro:actions';

const result = Astro.getActionResult(actions.myAction);
---
{result?.data && <p>Success: {result.data}</p>}
{result?.error && <p>Error: {result.error.message}</p>}

Session

Astro.session
AstroSession | undefined
Utilities for handling sessions in on-demand rendered routes.
---
// Get session data
const userId = await Astro.session.get('userId');

// Set session data
await Astro.session.set('userId', '123');

// Destroy session
await Astro.session.destroy();
---

CSP (Content Security Policy)

Astro.csp
object | undefined
Utilities to control CSP headers. Only available when CSP is enabled in config.

Methods

Astro.csp.insertDirective
(directive: string) => void
Add a specific CSP directive to the route being rendered.
---
Astro.csp?.insertDirective("default-src 'self' https://example.com");
---
Astro.csp.insertStyleResource
(payload: string) => void
Set the resource for the style-src directive.
---
Astro.csp?.insertStyleResource('https://styles.cdn.example.com/');
---
Astro.csp.insertStyleHash
(hash: string) => void
Insert a single style hash to the route being rendered.
---
Astro.csp?.insertStyleHash('sha256-1234567890abcdef');
---
Astro.csp.insertScriptResource
(resource: string) => void
Set the resource for the script-src directive.
---
Astro.csp?.insertScriptResource('https://scripts.cdn.example.com/');
---
Astro.csp.insertScriptHash
(hash: string) => void
Insert a single script hash to the route being rendered.
---
Astro.csp?.insertScriptHash('sha256-1234567890abcdef');
---

Build docs developers (and LLMs) love