2022-06-16 09:12:25 -05:00
|
|
|
import './shim.js';
|
|
|
|
|
|
|
|
import type { SSRManifest } from 'astro';
|
|
|
|
import { App } from 'astro/app';
|
|
|
|
|
|
|
|
type Env = {
|
|
|
|
ASSETS: { fetch: (req: Request) => Promise<Response> };
|
|
|
|
};
|
|
|
|
|
|
|
|
export function createExports(manifest: SSRManifest) {
|
2022-06-30 21:29:59 -05:00
|
|
|
const app = new App(manifest, false);
|
2022-06-16 09:12:25 -05:00
|
|
|
|
|
|
|
const fetch = async (request: Request, env: Env) => {
|
|
|
|
const { origin, pathname } = new URL(request.url);
|
|
|
|
|
|
|
|
// static assets
|
|
|
|
if (manifest.assets.has(pathname)) {
|
|
|
|
const assetRequest = new Request(`${origin}/static${pathname}`, request);
|
|
|
|
return env.ASSETS.fetch(assetRequest);
|
|
|
|
}
|
|
|
|
|
2022-07-22 15:30:17 -05:00
|
|
|
let routeData = app.match(request, { matchNotFound: true });
|
|
|
|
if (routeData) {
|
2022-07-19 15:11:53 -05:00
|
|
|
Reflect.set(
|
|
|
|
request,
|
|
|
|
Symbol.for('astro.clientAddress'),
|
|
|
|
request.headers.get('cf-connecting-ip')
|
|
|
|
);
|
2022-07-22 15:30:17 -05:00
|
|
|
return app.render(request, routeData);
|
2022-06-24 14:30:47 -05:00
|
|
|
}
|
|
|
|
|
2022-06-16 09:12:25 -05:00
|
|
|
return new Response(null, {
|
|
|
|
status: 404,
|
|
|
|
statusText: 'Not found',
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return { default: { fetch } };
|
|
|
|
}
|