forked from withastro/astro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.advanced.ts
51 lines (40 loc) · 1.26 KB
/
server.advanced.ts
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import type { SSRManifest } from 'astro';
import { App } from 'astro/app';
import { getProcessEnvProxy } from './util.js';
process.env = getProcessEnvProxy();
type Env = {
ASSETS: { fetch: (req: Request) => Promise<Response> };
name: string;
};
export function createExports(manifest: SSRManifest) {
const app = new App(manifest);
const fetch = async (request: Request, env: Env, context: any) => {
process.env = env as any;
const { pathname } = new URL(request.url);
// static assets fallback, in case default _routes.json is not used
if (manifest.assets.has(pathname)) {
return env.ASSETS.fetch(request);
}
let routeData = app.match(request, { matchNotFound: true });
if (routeData) {
Reflect.set(
request,
Symbol.for('astro.clientAddress'),
request.headers.get('cf-connecting-ip')
);
Reflect.set(request, Symbol.for('runtime'), { env, name: 'cloudflare', ...context });
let response = await app.render(request, routeData);
if (app.setCookieHeaders) {
for (const setCookieHeader of app.setCookieHeaders(response)) {
response.headers.append('Set-Cookie', setCookieHeader);
}
}
return response;
}
return new Response(null, {
status: 404,
statusText: 'Not found',
});
};
return { default: { fetch } };
}