astro/packages/integrations/cloudflare/src/server.directory.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

58 lines
1.3 KiB
TypeScript
Raw Normal View History

import type { SSRManifest } from 'astro';
import { App } from 'astro/app';
import { getProcessEnvProxy } from './util.js';
process.env = getProcessEnvProxy();
export function createExports(manifest: SSRManifest) {
2022-08-08 17:12:48 +00:00
const app = new App(manifest, false);
const onRequest = async ({
request,
next,
...runtimeEnv
}: {
request: Request;
next: (request: Request) => void;
} & Record<string, unknown>) => {
process.env = runtimeEnv.env as any;
const { origin, pathname } = new URL(request.url);
// static assets
if (manifest.assets.has(pathname)) {
const assetRequest = new Request(`${origin}/static/${app.removeBase(pathname)}`, request);
return next(assetRequest);
}
let routeData = app.match(request, { matchNotFound: true });
if (routeData) {
2022-07-19 20:11:53 +00:00
Reflect.set(
request,
Symbol.for('astro.clientAddress'),
request.headers.get('cf-connecting-ip')
);
Reflect.set(request, Symbol.for('runtime'), {
...runtimeEnv,
name: 'cloudflare',
next,
});
let response = await app.render(request, routeData);
2022-09-28 20:57:35 +00:00
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 { onRequest };
}