diff --git a/src/lib/shared/api/mutator/custom-instance.ts b/src/lib/shared/api/mutator/custom-instance.ts index a3e76ed6e..8ad26b848 100644 --- a/src/lib/shared/api/mutator/custom-instance.ts +++ b/src/lib/shared/api/mutator/custom-instance.ts @@ -36,9 +36,12 @@ const tryGetRequestEvent = () => { export const customFetch = async (url: string, options: RequestInit): Promise => { const event = tryGetRequestEvent(); - // Deployed Workers get a Secrets Store binding; local dev and prerender get a plain string - const token = event?.platform?.env.SERVER_API_TOKEN ?? envPrivate.SERVER_API_TOKEN; - const serverApiToken = typeof token === "string" ? token : await token.get(); + // Deployed Workers get a Secrets Store binding (also surfaced through $env/dynamic/private); + // local dev and prerender get a plain string. Prefer the dynamic env: during `vite build` the + // platform proxy exposes a binding whose get() fails because the secret only exists remotely. + const token = + (envPrivate.SERVER_API_TOKEN as App.Platform["env"]["SERVER_API_TOKEN"]) ?? event?.platform?.env.SERVER_API_TOKEN; + const serverApiToken = typeof token === "string" ? token : ((await token?.get()) ?? ""); const requestInit: RequestInit = { ...options,