astro/packages/integrations/cloudflare/src/server.ts
Okiki Ojo 0cc6ede362
SSR 404 and 500 routes in adapters (#4018)
* fix(WIP): SSR 404 and 500 routes

* Implement the feature

Co-authored-by: Matthew Phillips <matthew@skypack.dev>
2022-07-22 16:30:17 -04:00

39 lines
923 B
TypeScript

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) {
const app = new App(manifest, false);
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);
}
let routeData = app.match(request, { matchNotFound: true });
if (routeData) {
Reflect.set(
request,
Symbol.for('astro.clientAddress'),
request.headers.get('cf-connecting-ip')
);
return app.render(request, routeData);
}
return new Response(null, {
status: 404,
statusText: 'Not found',
});
};
return { default: { fetch } };
}