1

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.

1 Answers1

0

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

lxhom
  • 650
  • 4
  • 15
  • 1
    This is happening in production during the build. At least, that's what I thought, since I'm using cloudflare pages. And platform.env is supposed to have my R2 bindings. I'm now using fetch to pull files from my R2 bucket, but that's not a nice solution. – user21409596 Mar 18 '23 at 05:52
  • "This is happening in production during the build": Build and production both run on CF architecture, but run on two entirely different servers: The CI/CD server near you build your artifacts once (so your SK app) and that server deploys it to the CF edge network, so those might *seem* like they're the same server, they are not. You either have to do it like this or disable prerendering :/ – lxhom Mar 18 '23 at 14:14