-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.ts
More file actions
43 lines (36 loc) · 1.48 KB
/
server.ts
File metadata and controls
43 lines (36 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/// <reference types="@cloudflare/workers-types" />
import { createRequestHandler, type ServerBuild } from "@remix-run/cloudflare";
// @ts-ignore - the server build is generated by `remix vite:build`
import * as build from "./build/server";
export interface Env {
DB: D1Database;
SESSIONS: KVNamespace;
FLAGS: KVNamespace;
MEDIA: R2Bucket;
ASSETS: Fetcher;
}
const handleRemixRequest = createRequestHandler(build as unknown as ServerBuild);
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
// Webflow Cloud mounts the app at a path prefix (e.g. /my-app). Match
// from /api/ onward so routing works regardless of mount.
const url = new URL(request.url);
const apiIndex = url.pathname.indexOf("/api/");
const isApi = apiIndex !== -1;
// Static assets can't live under /api/* — passing them through env.ASSETS
// would consume the request body (ReadableStream is disturbed), breaking
// POST actions. Only consult ASSETS for non-API requests.
if (!isApi) {
const asset = await env.ASSETS.fetch(request);
if (asset.status !== 404) return asset;
}
// The URL is passed through untouched so Remix's configured basename (set
// from COSMIC_MOUNT_PATH at build time) can match prefixed routes.
return handleRemixRequest(request, {
cloudflare: { env, ctx },
} as unknown as AppLoadContext);
},
};
type AppLoadContext = {
cloudflare: { env: Env; ctx: ExecutionContext };
};