-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.ts
More file actions
29 lines (24 loc) · 782 Bytes
/
handler.ts
File metadata and controls
29 lines (24 loc) · 782 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import React from "react";
import routes from "./routes.ts";
import * as Responses from "./Responses.ts";
export interface HandlerCache {
match(
request: RequestInfo | URL,
options: CacheQueryOptions & { entryPoint: string },
): Promise<Response | undefined>;
}
export interface HandlerOptions {
cache: HandlerCache;
}
export default async function handler(
request: Request,
{ cache }: HandlerOptions,
): Promise<Response> {
for (const { urlPattern, functionComponent, entryPoint } of routes) {
if (!urlPattern.test(request.url)) continue;
return entryPoint === undefined
? Responses.page(React.createElement(functionComponent))
: await cache.match(request, { entryPoint }) ?? Responses.notFound();
}
return Responses.notFound();
}