Interface AstroGlobal<Props, Self, Params>

Astro global available in all contexts in .astro files

Astro reference

interface AstroGlobal<Props, Self, Params> {
    clientAddress: string;
    cookies: AstroCookies;
    currentLocale: undefined | string;
    generator: string;
    getActionResult: (<TAccept, TInputSchema, TAction>(action) => undefined | Awaited<ReturnType<TAction["safe"]>>);
    locals: Locals;
    params: Params;
    preferredLocale: undefined | string;
    preferredLocaleList: undefined | string[];
    props: Props;
    redirect: ((path, status?) => Response);
    request: Request;
    response: ResponseInit & {
        headers: Headers;
    };
    rewrite: ((rewritePayload) => Promise<Response>);
    self: Self;
    site: undefined | URL;
    slots: Record<string, undefined | true> & {
        has(slotName): boolean;
        render(slotName, args?): Promise<string>;
    };
    url: URL;
    glob(globStr): Promise<AstroInstance[]>;
    glob<T>(globStr): Promise<MarkdownInstance<T>[]>;
    glob<T>(globStr): Promise<MDXInstance<T>[]>;
    glob<T>(globStr): Promise<T[]>;
}

Type Parameters

Hierarchy (view full)

Properties

clientAddress: string

The address (usually IP address) of the user.

Throws an error if used within a static site, or within a prerendered page.

cookies: AstroCookies

Utility for getting and setting the values of cookies.

currentLocale: undefined | string

The current locale computed from the URL of the request. It matches the locales in i18n.locales, and returns undefined otherwise.

generator: string

Returns a string with the current version of Astro.

Useful for using <meta name="generator" content={Astro.generator} /> or crediting Astro in a site footer.

HTML Specification for generator

Astro reference

getActionResult: (<TAccept, TInputSchema, TAction>(action) => undefined | Awaited<ReturnType<TAction["safe"]>>)

Get an action result on the server when using a form POST. Expects the action function as a parameter. Returns a type-safe result with the action data when a matching POST request is received and undefined otherwise.

Example usage:

import { actions } from 'astro:actions';

const result = await Astro.getActionResult(actions.myAction);

Type declaration

locals: Locals

Object accessed via Astro middleware

params: Params

Parameters passed to a dynamic page generated using getStaticPaths

Example usage: ```astro

export async function getStaticPaths() { return [ { params: { id: '1' } }, ]; }

const { id } = Astro.params;

{id}

```

Astro reference

preferredLocale: undefined | string

The current locale that is computed from the Accept-Language header of the browser (SSR Only).

preferredLocaleList: undefined | string[]

The list of locales computed from the Accept-Language header of the browser, sorted by quality value (SSR Only).

props: Props

List of props passed to this component

A common way to get specific props is through destructuring, ex:

const { name } = Astro.props

Astro reference

redirect: ((path, status?) => Response)

Redirect to another page (SSR Only)

Example usage:

if(!isLoggedIn) {
return Astro.redirect('/login');
}

Astro reference

Type declaration

request: Request

Information about the current request. This is a standard Request object

For example, to get a URL object of the current URL, you can use:

const url = new URL(Astro.request.url);

Astro reference

response: ResponseInit & {
    headers: Headers;
}

Information about the outgoing response. This is a standard ResponseInit object

For example, to change the status code you can set a different status on this object:

Astro.response.status = 404;

Astro reference

Type declaration

rewrite: ((rewritePayload) => Promise<Response>)

It rewrites to another page. As opposed to redirects, the URL won't change, and Astro will render the HTML emitted by the rewritten URL passed as argument.

Example

if (pageIsNotEnabled) {
return Astro.rewrite('/fallback-page')
}

Type declaration

self: Self

The <Astro.self /> element allows a component to reference itself recursively.

Astro reference

site: undefined | URL

Returns a URL object built from the site config option

Astro reference

slots: Record<string, undefined | true> & {
    has(slotName): boolean;
    render(slotName, args?): Promise<string>;
}

Utility functions for modifying an Astro component’s slotted children

Astro reference

Type declaration

url: URL

A full URL object of the request URL. Equivalent to: new URL(Astro.request.url)

Astro reference

Methods