adapter(cloudflare): add supports for the new assets feature

This commit is contained in:
dswbx
2025-01-25 13:40:28 +01:00
parent 392a06c486
commit aa7eac258b
5 changed files with 45 additions and 37 deletions

View File

@@ -11,6 +11,7 @@ export type CloudflareBkndConfig<Env = any> = FrameworkBkndConfig<Context<Env>>
kv?: KVNamespace;
dobj?: DurableObjectNamespace;
};
static?: "kv" | "assets";
key?: string;
keepAliveSeconds?: number;
forceHttps?: boolean;
@@ -29,18 +30,23 @@ export function serve<Env = any>(config: CloudflareBkndConfig<Env>) {
return {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
const url = new URL(request.url);
const manifest = config.manifest;
if (manifest) {
if (config.manifest && config.static === "assets") {
console.warn("manifest is not useful with static 'assets'");
} else if (!config.manifest && config.static === "kv") {
throw new Error("manifest is required with static 'kv'");
}
if (config.manifest && config.static !== "assets") {
const pathname = url.pathname.slice(1);
const assetManifest = JSON.parse(manifest);
const assetManifest = JSON.parse(config.manifest);
if (pathname && pathname in assetManifest) {
const hono = new Hono();
hono.all("*", async (c, next) => {
const res = await serveStatic({
path: `./${pathname}`,
manifest
manifest: config.manifest!
})(c as any, next);
if (res instanceof Response) {
const ttl = 60 * 60 * 24 * 365;