sveltesveltekitcloudflare-workerscloudflare-pages

Is platform.env available when prerendering with sveltekit & adapter-cloudflare?


I have a +page.server.js file with a export async function load({ platform }) {} function. When I set export const prerender = true at the top, platform.env is undefined. Without prerender it works and I can access my R2 bucket. This is all in production and not local.

I tried multiple combinations and configurations. There is no documentation on this issue, neither from cloudflare nor from sveltekit.


Solution

  • platform.env cannot be pre-rendered, because that is for the env vars on Cloudflare (see the image below) and not your dev environment's env vars (where the pre-rendering happens). To access those, the Svelte compiler would need your Cloudflare credentials. If you need them pre-rendered, add a +layout.server.js file to your source tree and store your variables there.

    enter image description here