kit icon indicating copy to clipboard operation
kit copied to clipboard

Prerendered Remote Function failing on SSR in Cloudflare Workers @sveltejs/adapter-cloudflare

Open alecbakholdin opened this issue 3 weeks ago • 3 comments

Describe the bug

Prerendered functions don't work with SSR in Cloudflare Workers. When I disable ssr with export const ssr = false, the issue no longer persists.

Reproduction

Repository: https://github.com/alecbakholdin/minimal-cloudflare-remote-fn-test (build files included) Bug in the wild: https://minimal-cloudflare-remote-fn-test.alecbakholdin.workers.dev

  1. Create a remote function (const testFn = export const testFn = prerender(() => { return "testing"; });)
  2. Call remote function in src/routes/+page.svelte with {await testFn()}
  3. npm run build && npx wrangler deploy
  4. Go to the site and see 500. Logs below

Logs

This is the only relevant log I can find

[1;31m[500] GET /[0m
Error: Prerendered response not found
    at _worker.js:3303:25
    at async _worker.js:3312:42
    at async _worker.js:3297:18

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.5 LTS 22.04.5 LTS (Jammy Jellyfish)
    CPU: (24) x64 13th Gen Intel(R) Core(TM) i7-13700K
    Memory: 11.74 GB / 15.47 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 20.19.0 - /home/abakholdin/.nvm/versions/node/v20.19.0/bin/node
    npm: 11.6.2 - /home/abakholdin/.nvm/versions/node/v20.19.0/bin/npm
    pnpm: 10.18.2 - /home/abakholdin/.nvm/versions/node/v20.19.0/bin/pnpm
    Deno: 2.5.6 - /home/abakholdin/.local/share/nvim/mason/bin/deno
  npmPackages:
    @sveltejs/adapter-auto: ^7.0.0 => 7.0.0 
    @sveltejs/adapter-cloudflare: ^7.2.4 => 7.2.4 
    @sveltejs/kit: ^2.48.5 => 2.49.1 
    @sveltejs/vite-plugin-svelte: ^6.2.1 => 6.2.1 
    svelte: ^5.43.8 => 5.45.6 
    vite: ^7.2.2 => 7.2.6

Severity

serious, but I can work around it

Additional Information

I can't reproduce this minimally, but on the project where I discovered this bug, sometimes this would work on first page load but not on reloads or afterwards on client-side navigation.

alecbakholdin avatar Dec 06 '25 18:12 alecbakholdin

Tried looking into it some more on my own. The remote function asset is being uploaded, as seen here: href https://minimal-cloudflare-remote-fn-test.alecbakholdin.workers.dev/_app/remote/62b3f9/testFn

Image

I don't know how to link files but the file at /packages/kit/runtime/app/remote/prerender.js has a segment in it that throws 'Prerendered response not found'. The url that gets passed at the line

						const promise = (cache[key] ??= fetch(new URL(url, event.url.origin).href).then(

is correct (it's https://minimal-cloudflare-remote-fn-test.alecbakholdin.workers.dev/_app/remote/62b3f9/testFn) but returns 404 for the remote function specifically (prerendered pages work) which leads me to believe that the sveltekit internal event.fetch function is not recognizing the remote function url as a prerendered asset for whatever reason. I will try to look into it more, probably on Monday.

alecbakholdin avatar Dec 06 '25 22:12 alecbakholdin

It turns out this happens because for prerendered assets we are making a full HTTPS fetch call but Cloudflare does not allow a worker calling itself through fetch. The solution is to use service bindings by doing env.ASSETS.fetch() for prerendered assets.

alecbakholdin avatar Dec 07 '25 03:12 alecbakholdin

I solved the issue for me specifically but it's not 100% complete. Someone please help me in the referenced PR.

alecbakholdin avatar Dec 07 '25 19:12 alecbakholdin