Astro Container API (experimental)
Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
Hinzugefügt in:
astro@4.9.0
Beta
The Container API allows you to render Astro components in isolation.
This experimental server-side API unlocks a variety of potential future uses, but is currently scoped to allow unit testing of .astro
component output. This API allows you to create a new container, and render an Astro component returning a string or a Response
.
This API is experimental and subject to breaking changes, even in minor or patch releases. Please consult the Astro CHANGELOG for changes as they occur. This page will always be updated with the most current information for the latest version of Astro.
create()
Section titled create()Creates a new instance of the container.
It accepts an object with the following options:
streaming
option
Section titled streaming optionType: boolean
Enables rendering components using HTML streaming.
renderers
option
Section titled renderers optionType:: AstroRenderer[]
;
A list of client renderers required by the component. Use this if your .astro
component renders any UI framework components using an official Astro integration (e.g. React, Vue, etc.). For each framework rendered, you must provide an object stating the integration package name
, as well as both its client
and server
rendering script
The following example provides the necessary object to render an Astro component that renders a React component:
renderToString()
Section titled renderToString()This function renders a specified component inside a container. It takes an Astro component as an argument and it returns a string that represents the HTML/content rendered by the Astro component.
Under the hood, this function calls renderToResponse
and calls Response.text()
.
It also accepts an object as a second argument that can contain a number of options.
renderToResponse()
Section titled renderToResponse()It renders a component, and it returns a Response
object.
It also accepts an object as a second argument that can contain a number of options.
Rendering options
Section titled Rendering optionsBoth renderToResponse
and renderToString
accept an object as their second argument:
These optional values can be passed to the rendering function in order to provide additional information necessary for an Astro component to properly render.
slots
Section titled slotsType: Record<string, any>
;
An option to pass content to be rendered with <slots>
.
If your Astro component renders one default slot, pass an object with default
as the key:
If your component renders named slots, use the slot names as the object keys:
You can also render components in cascade:
request
option
Section titled request optionType: Request
An option to pass a Request
with information about the path/URL the component will render.
Use this option when your component needs to read information like Astro.url
or Astro.request
.
You can also inject possible headers or cookies.
params
option
Section titled params optionType: Record<string, string | undefined>
;
An object to pass information about the path parameter to an Astro component responsible for generating dynamic routes.
Use this option when your component needs a value for Astro.params
in order to generate a single route dynamically.
locals
options
Section titled locals optionsType: App.Locals
An option to pass information from Astro.locals
for rendering your component.
Use this option to when your component needs information stored during the lifecycle of a request in order to render, such as logged in status.
routeType
option
Section titled routeType optionType: "page" | "endpoint"
An option available when using renderToResponse
to specify that you are rendering an endpoint:
To test your endpoint on methods such as POST
, PATCH
, etc., use the request
option to call the correct function: