kit icon indicating copy to clipboard operation
kit copied to clipboard

adapter-cloudflare emulated bindings not always available

Open lknlknim opened this issue 1 year ago • 6 comments

Describe the bug

My work flow was using miniflare to polyfill the cloudflare platform environment in handle server hooks for local development.

For some of the people may concern it's easy to use wrangler-proxy or cfw-bindings-wrangler-bridge for remote data. Combining with import.meta.env.MODE it's very convenient to switch between dev, preview, live mode for local and remote data.

In the recent adapter update it's doing mostly what miniflare did. I got 2 projects 1 using authjs and another using lucia auth. In the lucia auth project I can remove the manually implemented miniflare part and rely on the adapter for local development. For remote data the bridge is still needed. In the auth js project it's broken as soon as I try to login. It's complaining about platform is undefined and it's trying to access platform.env. Those are implemted in handle server hooks, not sevltekit endpoints.

The lucia auth project is facing the same if I try to move the /google and /google/callback sveltekit endpoints into handle server hooks. It'll complain about platform is undefined.

I'm not familiar to how and when adapter inject the platform into the event object. From my simple testing it seems to inject the platform only when there is +server.ts as a sveltekit endpoint with known route. And no platform is injected when the endpoints logic is implemented in the handle server hooks directly.

Reproduction

Not available in near future

Logs

No response

System Info

-

Severity

annoyance

Additional Information

No response

lknlknim avatar Mar 17 '24 14:03 lknlknim

Ran into the same issue. Did some digging, platform is set here,

https://github.com/sveltejs/kit/blob/6a5d40ef098f63e7e9209bca2058ccf1820bc060/packages/kit/src/runtime/server/respond.js#L295

but it is only called if route is set

https://github.com/sveltejs/kit/blob/6a5d40ef098f63e7e9209bca2058ccf1820bc060/packages/kit/src/runtime/server/respond.js#L216

so

I'm not familiar to how and when adapter inject the platform into the event object. From my simple testing it seems to inject the platform only when there is +server.ts as a sveltekit endpoint with known route. And no platform is injected when the endpoints logic is implemented in the handle server hooks directly.

seems to be the correct assumption

valterkraemer avatar Mar 18 '24 12:03 valterkraemer

Similar issue for me, and I created a simple repro that may help. See branch https://github.com/sdarnell/cf-svelte/tree/bugs/missing-platform

This works fine when directed to the regular page, but it you try to access a a non existent page, e.g. '/wibble' or in another case accessing the favicon seemed to cause the problem.

In the error case, the console will output:

building false url https://localhost:5173/wibble event.platform.env.DB undefined
Missing platform DB

sdarnell avatar Mar 24 '24 18:03 sdarnell

I have the same issue. Thanks for the workaround, @sdarnell!

mariusei avatar Jun 08 '24 21:06 mariusei

Same problem. No platform object when running with npm run dev on 404 pages. Everywhere else it works great.

stephen-trippett avatar Jun 11 '24 18:06 stephen-trippett

This bit my ass as well today, but I solved it with a catch-all route at src/routes/[...catchAll]/+server.ts returning 404.

import { error, type RequestHandler } from '@sveltejs/kit'

export const GET: RequestHandler = async () => {
    error(404, 'Not found')
}

Also fixes silly things like favicon.ico... 🙈

ChristianJacobsen avatar Jun 11 '24 19:06 ChristianJacobsen

@ChristianJacobsen Good idea! I slightly improved on it by doing a src/routes/[...catchAll]/+page.server.ts instead. This way it uses the normal +error.svelte file as a template.

// NOTE: This file only exists to catch 404 routes so the platform binding works properly in dev mode. It can be deleted when this bug is fixed: https://github.com/sveltejs/kit/issues/11996
import { error } from "@sveltejs/kit";
import type { PageServerLoad } from "./$types";

export const load: PageServerLoad = async () => {
	error(404, { message: "Not found" });
};

stephen-trippett avatar Jun 12 '24 01:06 stephen-trippett

It looks to me like config is unused and perhaps should be removed while prerender should be false when no route is found unless it's a SPA fallback page

benmccann avatar Jul 06 '24 15:07 benmccann