Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: document locals in adapter API #3685

Merged
merged 11 commits into from
Jul 18, 2023
20 changes: 18 additions & 2 deletions src/content/docs/en/reference/adapter-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,30 @@ export function start(manifest) {

The following methods are provided:

##### `app.render(request)`
##### `app.render(request, routeData, locals)`

This method calls the Astro page that matches the request, renders it, and returns a Promise to a [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) object. This also works for API routes, that do not render pages.
This method calls the Astro page that matches the request, renders it, and returns a Promise to a [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) object. This also works for API routes that do not render pages.

```js
const response = await app.render(request);
```

The method accepts a mandatory `request` argument, and two other optionals arguments: [`routeData`](en/reference/integrations-reference/#routedata-type-reference) and [`locals`](https://docs.astro.build/en/guides/middleware/#locals).
sarah11918 marked this conversation as resolved.
Show resolved Hide resolved

The example below reads a header named `x-private-header`, which attempts to parse it as an object and pass it to `locals`, which can then be passed to any [middleware function](/en/guides/middleware/).
ematipico marked this conversation as resolved.
Show resolved Hide resolved

```js
const privateHeader = request.headers.get("x-private-header");
let locals = {};
try {
if (privateHeader) {
locals = JSON.parse(privateHeader);
}
} finally {
const response = await app.render(request, undefined, locals);
}
```

##### `app.match(request)`

This method is used to determine if a request is matched by the Astro app's routing rules.
Expand Down