2022-06-16 14:12:25 +00: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-07-01 02:29:59 +00:00
|
|
|
const app = new App(manifest, false);
|
2022-06-16 14:12:25 +00: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (app.match(request)) {
|
2022-07-19 20:11:53 +00:00
|
|
|
Reflect.set(
|
|
|
|
request,
|
|
|
|
Symbol.for('astro.clientAddress'),
|
|
|
|
request.headers.get('cf-connecting-ip')
|
|
|
|
);
|
2022-06-16 14:12:25 +00:00
|
|
|
return app.render(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 404
|
2022-06-24 19:30:47 +00:00
|
|
|
const _404Request = new Request(`${origin}/404`, request);
|
|
|
|
if (app.match(_404Request)) {
|
|
|
|
return app.render(_404Request);
|
|
|
|
}
|
|
|
|
|
2022-06-16 14:12:25 +00:00
|
|
|
return new Response(null, {
|
|
|
|
status: 404,
|
|
|
|
statusText: 'Not found',
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return { default: { fetch } };
|
|
|
|
}
|