Astro render context
Esta página aún no está disponible en tu idioma.
When rendering a page, Astro provides a runtime API specific to the current render. This includes useful information such as the current page URL as well as APIs to perform actions like redirecting to another page.
In .astro components, this context is available from the Astro global object. Endpoint functions are also called with this same context object as their first argument, whose properties mirror the Astro global properties.
Some properties are only available for routes rendered on demand or may have limited functionality on prerendered pages.
The Astro global object is available to all .astro files. Use the context object in endpoint functions to serve static or live server endpoints and in middleware to inject behavior when a page or endpoint is about to be rendered.
The context object
Section titled “The context object”The following properties are available on the Astro global (e.g. Astro.props, Astro.redirect()) and are also available on the context object (e.g. context.props, context.redirect()) passed to endpoint functions and middleware.
props is an object containing any values that have been passed as component attributes.
---const { title, date } = Astro.props;---<div> <h1>{title}</h1> <p>{date}</p></div>---import Heading from '../components/Heading.astro';---<Heading title="My First Post" date="09 Aug 2022" />The props object also contains any props passed from getStaticPaths() when rendering static routes.
---export function getStaticPaths() { return [ { params: { id: '1' }, props: { author: 'Blu' } }, { params: { id: '2' }, props: { author: 'Erika' } }, { params: { id: '3' }, props: { author: 'Matthew' } } ];}
const { id } = Astro.params;const { author } = Astro.props;---import type { APIContext } from 'astro';
export function getStaticPaths() { return [ { params: { id: '1' }, props: { author: 'Blu' } }, { params: { id: '2' }, props: { author: 'Erika' } }, { params: { id: '3' }, props: { author: 'Matthew' } } ];}
export function GET({ props }: APIContext) { return new Response( JSON.stringify({ author: props.author }), );}See also: Data Passing with props
params
Section titled “params”params is an object containing the values of dynamic route segments matched for a request. Its keys must match the parameters in the page or endpoint file path.
In static builds, this will be the params returned by getStaticPaths() used for prerendering dynamic routes:
---export function getStaticPaths() { return [ { params: { id: '1' } }, { params: { id: '2' } }, { params: { id: '3' } } ];}const { id } = Astro.params;---<h1>{id}</h1>import type { APIContext } from 'astro';
export function getStaticPaths() { return [ { params: { id: '1' } }, { params: { id: '2' } }, { params: { id: '3' } } ];}
export function GET({ params }: APIContext) { return new Response( JSON.stringify({ id: params.id }), );}When routes are rendered on demand, params can be any value matching the path segments in the dynamic route pattern.
---import { getPost } from '../api';
const post = await getPost(Astro.params.id);
// No posts found with this IDif (!post) { return Astro.redirect("/404")}---<html> <h1>{post.name}</h1></html>See also: params
Type: URL
astro@1.0.0
url is a URL object constructed from the current request.url value. It is useful for interacting with individual properties of the request URL, like pathname and origin.
Astro.url is equivalent to doing new URL(Astro.request.url).
url will be a localhost URL in dev mode. When building a site, prerendered routes will receive a URL based on the site and base options. If site is not configured, prerendered pages will receive a localhost URL during builds as well.
<h1>The current URL is: {Astro.url}</h1><h1>The current URL pathname is: {Astro.url.pathname}</h1><h1>The current URL origin is: {Astro.url.origin}</h1>You can also use url to create new URLs by passing it as an argument to new URL().
---// Example: Construct a canonical URL using your production domainconst canonicalURL = new URL(Astro.url.pathname, Astro.site);// Example: Construct a URL for SEO meta tags using your current domainconst socialImageURL = new URL('/images/preview.png', Astro.url);---<link rel="canonical" href={canonicalURL} /><meta property="og:image" content={socialImageURL} />Type: URL | undefined
site returns a URL made from site in your Astro config. It returns undefined if you have not set a value for site in your Astro config.
<link rel="alternate" type="application/rss+xml" title="Your Site's Title" href={new URL("rss.xml", Astro.site)}/>clientAddress
Section titled “clientAddress”Type: string
astro@1.0.0
clientAddress specifies the IP address of the request. This property is only available for routes rendered on demand and cannot be used on prerendered pages.
---export const prerender = false; // Not needed in 'server' mode---
<div>Your IP address is: <span class="address">{Astro.clientAddress}</span></div>export const prerender = false; // Not needed in 'server' modeimport type { APIContext } from 'astro';
export function GET({ clientAddress }: APIContext) { return new Response(`Your IP address is: ${clientAddress}`);}isPrerendered
Section titled “isPrerendered”Type: boolean
astro@5.0.0
A boolean representing whether or not the current page is prerendered.
You can use this property to run conditional logic in middleware, for example, to avoid accessing headers in prerendered pages.
generator
Section titled “generator”Type: string
astro@1.0.0
generator provides the current version of Astro your project is running. This is a convenient way to add a <meta name="generator"> tag with your current version of Astro. It follows the format "Astro v5.x.x".
<html> <head> <meta name="generator" content={Astro.generator} /> </head> <body> <footer> <p>Built with <a href="https://astro.build">{Astro.generator}</a></p> </footer> </body></html>