astro/packages/integrations/netlify/src/netlify-edge-functions.ts
2022-09-28 20:57:35 +00:00

36 lines
1,009 B
TypeScript

import type { SSRManifest } from 'astro';
import { App } from 'astro/app';
const clientAddressSymbol = Symbol.for('astro.clientAddress');
export function createExports(manifest: SSRManifest) {
const app = new App(manifest);
const handler = async (request: Request): Promise<Response | void> => {
const url = new URL(request.url);
// If this matches a static asset, just return and Netlify will forward it
// to its static asset handler.
if (manifest.assets.has(url.pathname)) {
return;
}
if (app.match(request)) {
const ip = request.headers.get('x-nf-client-connection-ip');
Reflect.set(request, clientAddressSymbol, ip);
const response = await app.render(request);
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: handler };
}