Astro Container API (experimental)
The Container API allows to render Astro components in isolation.
Added in:astro@4.9.0
Beta
create()
Section titled create()Creates a new instance of the container.
It accepts an object with the following options
streaming
option
Section titled streaming optionEnables rendering components using the streaming.
Type: boolean
renderers
option
Section titled renderers optionA list of client renderers. Add a renderer if your Astro component renders some client components such as React, Preact, etc.
Type:: AstroRenderer[]
;
For example, if you’re rendering a Astro component that renders a React component, you’ll have to provide the following renderer:
astroConfig
option
Section titled astroConfig optionA subset of the Astro user configuration.
Type: AstroContainerUserConfig
renderToString()
Section titled renderToString()It renders a component, 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 accepts a number of options.
renderToResponse()
Section titled renderToResponse()It renders a component, and it returns a Response
object.
It accepts a number of options.
Rendering options
Section titled Rendering optionsBoth renderToResponse
and renderToString
accept an object as their second argument:
slots
Section titled slotsType: Record<string, any>
;
Use this options if your component needs to render some slots.
If your components renders one slot, pass an object with default
as key:
If your component renders named slots, use the keys of the object to name the slots:
You can also render components in cascade:
request
option
Section titled request optionType: Request
The request is used to understand which path/URL the component is about to render.
Use this option in case 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>
;
Use this option in case your component needs to read Astro.params
. As opposed to getStaticPaths
, you must provide only one item of the params returned by that function.
locals
options
Section titled locals optionsType: App.Locals
Use this option to stub the Astro.locals
object:
routeType
option
Section titled routeType optionType: "page" | "endpoint"
Useful in case you’re rendering an endpoint. In this case, you want to use renderToResponse
:
If your endpoint needs to be tested on methods such as POST
, PATCH
, etc., you’ll have to use the request
option to signal Astro to call the correct function: