2022-03-25 11:08:02 -05:00
|
|
|
import { SSRManifest } from 'astro';
|
2022-03-25 11:08:51 -05:00
|
|
|
import type { Handler } from '@netlify/functions';
|
2022-03-25 11:08:02 -05:00
|
|
|
import { App } from 'astro/app';
|
|
|
|
import { polyfill } from '@astrojs/webapi';
|
|
|
|
|
|
|
|
polyfill(globalThis, {
|
|
|
|
exclude: 'window document',
|
|
|
|
});
|
|
|
|
|
|
|
|
interface Args {
|
|
|
|
site?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const createExports = (manifest: SSRManifest, args: Args) => {
|
|
|
|
const app = new App(manifest);
|
|
|
|
const site = new URL(args.site ?? `https://netlify.com`);
|
|
|
|
|
|
|
|
const handler: Handler = async (event) => {
|
|
|
|
const headers = new Headers(event.headers as any);
|
|
|
|
const request = new Request(new URL(event.path, site).toString(), {
|
|
|
|
method: event.httpMethod,
|
2022-03-25 11:08:51 -05:00
|
|
|
headers,
|
2022-03-25 11:08:02 -05:00
|
|
|
});
|
|
|
|
|
2022-03-25 11:08:51 -05:00
|
|
|
if (!app.match(request)) {
|
2022-03-25 11:08:02 -05:00
|
|
|
return {
|
|
|
|
statusCode: 404,
|
2022-03-25 11:08:51 -05:00
|
|
|
body: 'Not found',
|
2022-03-25 11:08:02 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = await app.render(request);
|
|
|
|
const body = await response.text();
|
|
|
|
|
|
|
|
return {
|
|
|
|
statusCode: 200,
|
|
|
|
headers: Object.fromEntries(response.headers.entries()),
|
2022-03-25 11:08:51 -05:00
|
|
|
body,
|
2022-03-25 11:08:02 -05:00
|
|
|
};
|
2022-03-25 11:08:51 -05:00
|
|
|
};
|
2022-03-25 11:08:02 -05:00
|
|
|
|
|
|
|
return { handler };
|
|
|
|
};
|