| 1 | // NOTE: a top-level `import { env } from "cloudflare:workers"` works too — |
| 2 | // SSG runs inside workerd via the cloudflare vite plugin's preview mode (see |
| 3 | // src/pages/ssg-env.tsx, which exercises exactly that). This guarded |
| 4 | // dynamic-import variant (the same trick waku's own adapter uses) is kept as |
| 5 | // the portable pattern for modules that must also load outside a Cloudflare |
| 6 | // environment. |
| 7 | const DO_NOT_BUNDLE = ""; |
| 8 | |
| 9 | export async function readEnv(): Promise<Record<string, unknown>> { |
| 10 | try { |
| 11 | const mod = await import( |
| 12 | /* @vite-ignore */ DO_NOT_BUNDLE + "cloudflare:workers" |
| 13 | ); |
| 14 | return mod.env as Record<string, unknown>; |
| 15 | } catch { |
| 16 | return {}; |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | export async function readMessage(): Promise<string> { |
| 21 | const env = await readEnv(); |
| 22 | return String(env.MESSAGE ?? "unset"); |
| 23 | } |