astro/packages/integrations/netlify/src/netlify-edge-functions.ts

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

31 lines
802 B
TypeScript
Raw Normal View History

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.
2022-04-22 14:04:54 +00:00
if (manifest.assets.has(url.pathname)) {
return;
}
2022-04-19 15:23:07 +00:00
if (app.match(request)) {
const ip = request.headers.get('x-nf-client-connection-ip');
Reflect.set(request, clientAddressSymbol, ip);
return app.render(request);
}
return new Response(null, {
status: 404,
2022-04-19 15:23:07 +00:00
statusText: 'Not found',
});
};
2022-04-19 15:23:07 +00:00
return { default: handler };
}