| 1 | import { cloudflare } from "@octanejs/adapter-cloudflare"; |
| 2 | import { defineConfig, RenderRoute, ServerRoute } from "@octanejs/vite-plugin"; |
| 3 | |
| 4 | /** |
| 5 | * The Cloudflare request context the adapter forwards as `context.platform` |
| 6 | * in production (`{ env, ctx }`). Octane's dev server supplies no platform |
| 7 | * (bindings are live-only), so handlers tolerate `undefined` in dev. |
| 8 | */ |
| 9 | interface KvBinding { |
| 10 | get(key: string): Promise<string | null>; |
| 11 | put(key: string, value: string): Promise<void>; |
| 12 | } |
| 13 | |
| 14 | interface Platform { |
| 15 | readonly env?: { readonly CACHE?: KvBinding }; |
| 16 | readonly ctx?: { readonly waitUntil?: (promise: Promise<unknown>) => void }; |
| 17 | } |
| 18 | |
| 19 | export default defineConfig({ |
| 20 | adapter: cloudflare(), |
| 21 | router: { |
| 22 | routes: [ |
| 23 | new ServerRoute({ |
| 24 | path: "/api/visits", |
| 25 | methods: ["GET"], |
| 26 | handler: async (context) => { |
| 27 | const platform = context.platform as Platform | undefined; |
| 28 | const cache = platform?.env?.CACHE; |
| 29 | if (cache === undefined) { |
| 30 | // Octane's dev server has no bindings — deploy to try this route. |
| 31 | return Response.json({ visits: null, dev: true }); |
| 32 | } |
| 33 | const visits = Number((await cache.get("visits")) ?? "0") + 1; |
| 34 | await cache.put("visits", String(visits)); |
| 35 | return Response.json({ visits }); |
| 36 | }, |
| 37 | }), |
| 38 | new RenderRoute({ path: "/", entry: ["App", "/src/App.tsx"] }), |
| 39 | ], |
| 40 | }, |
| 41 | }); |