You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -42,14 +42,13 @@ Returns an [`App` instance](#the-app-instance) that includes methods to work wit
42
42
43
43
```js
44
44
import { createApp } from"astro/app/entrypoint";
45
-
importhttpfrom"http";
46
45
47
46
constapp=createApp();
48
47
49
-
addEventListener("fetch", event=> {
50
-
event.respondWith(
51
-
app.render(event.request)
52
-
);
48
+
addEventListener("fetch", (event)=> {
49
+
if (eventinstanceofFetchEvent) {
50
+
event.respondWith(app.render(event.request));
51
+
}
53
52
});
54
53
```
55
54
@@ -90,8 +89,15 @@ The `createApp()` function returns a class instance with the following methods.
90
89
91
90
Calls the Astro page that matches the [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/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](/en/guides/endpoints/#server-endpoints-api-routes) that do not render pages.
Determines whether a request is matched by the Astro app's routing rules.
105
111
106
-
```js
107
-
if(app.match(request)) {
108
-
constresponse=awaitapp.render(request);
109
-
}
112
+
```ts {6,11}
113
+
import { createApp } from"astro/app/entrypoint";
114
+
115
+
const app =createApp();
116
+
117
+
exportconst handle =async (request:Request) => {
118
+
if (app.match(request)) {
119
+
const response =awaitapp.render(request);
120
+
returnresponse;
121
+
} else {
122
+
returnnewResponse("Not Found", { status: 404 });
123
+
}
124
+
};
110
125
```
111
126
112
127
You can usually call `app.render(request)` without using `.match` because Astro handles 404s if you provide a `404.astro` file. Use `app.match(request)` if you want to handle 404s in a different way.
113
128
114
129
By default, prerendered routes aren't returned, even if they are matched. You can change this behavior by using `true` as the second argument.
Returns an [instance of the Astro logger](/en/reference/integrations-reference/#astrointegrationlogger) available to the adapter's runtime environment.
125
140
126
-
```js "logger"
127
-
constlogger=app.getAdapterLogger();
128
-
try {
129
-
/* Some logic that can throw */
130
-
} catch {
131
-
logger.error("Your custom error message using Astro logger.");
132
-
}
141
+
```ts {9}
142
+
import { createApp } from"astro/app/entrypoint";
143
+
144
+
const app =createApp();
145
+
146
+
exportconst handle =async () => {
147
+
try {
148
+
/* Some logic that can throw */
149
+
} catch {
150
+
app.adapterLogger.error("Your custom error message using Astro logger.");
151
+
}
152
+
};
133
153
```
134
154
135
155
##### `app.getAllowedDomains()`
@@ -164,10 +184,18 @@ Returns a generator that yields individual cookie header values from a `Response
164
184
165
185
The following example appends a `Set-Cookie` header for each header obtained from a response:
166
186
167
-
```js
168
-
for (constsetCookieHeaderofapp.setCookieHeaders(response)) {
@@ -265,8 +293,15 @@ Whether or not to automatically add all cookies written by [`Astro.cookie.set()`
265
293
266
294
When set to `true`, they will be added to the `Set-Cookie` header of the response as comma-separated key-value pairs. You can use the standard `response.headers.getSetCookie()` API to read them individually.
@@ -281,9 +316,16 @@ The client IP address that will be made available as [`Astro.clientAddress`](/en
281
316
282
317
The example below reads the `x-forwarded-for` header and passes it as `clientAddress`. This value becomes available to the user as `Astro.clientAddress`.
@@ -297,16 +339,23 @@ The [`context.locals` object](/en/reference/api-reference/#locals) used to store
297
339
298
340
The example below reads a header named `x-private-header`, attempts to parse it as an object, and passes it to `locals`, which can then be passed to any [middleware function](/en/guides/middleware/).
If not provided, Astro will fallback to its default behavior for fetching error pages.
@@ -360,14 +416,18 @@ Adapters can pass this through to let runtime cache providers schedule work such
360
416
361
417
The following example forwards a runtime's `waitUntil()` implementation to [`app.render()`](#apprender) in an [adapter server entrypoint](/en/reference/adapter-reference/#building-a-server-entrypoint):
362
418
363
-
```js {8}
364
-
import { createApp } from'astro/app/entrypoint';
419
+
```ts {12}
420
+
import { createApp } from"astro/app/entrypoint";
421
+
422
+
// Context is provided by the runtime. This is a minimal example of what it can look like.
@@ -383,12 +443,18 @@ export async function handler(event, context) {
383
443
384
444
Defines the information about a route. This is useful when you already know the route to render. Doing so will bypass the internal call to [`app.match()`](#appmatch) to determine the route to render.
0 commit comments