gornostay25 / svelte-adapter-bun

A SvelteKit adapter for Bun
MIT License
534 stars 33 forks source link

fetch on server not including cookies (+page.server.ts) #42

Open Cluster2a opened 12 months ago

Cluster2a commented 12 months ago

Hey,

heaving the following code, the cookies on node are included.

const headers: HeadersInit = {
    contentType: 'application/json',
    Accept: 'application/json'
};

const response = await fetch(`${variables.API_URL}/dashboard`, {
    headers,
    credentials: 'include'
});

After switching to bun, the cookies are not included anymore. I tried to set the ORIGIN and add them manually via header, but for some reason no cookies at all reaches the backend (same tld).

Is there any way to work around this?

kyngs commented 11 months ago

Hey, do you use the fetch provided by the load function? If yes, you must manually inject the cookies in hooks.server.ts. For example:

export const handleFetch: HandleFetch = async ({event, request, fetch}) => {
    if (request.url.startsWith(PUBLIC_API_URL)) {
        request.headers.set("cookie", event.request.headers.get("cookie") ?? "");
    }

    return fetch(request);
}

I'm kinda surprised it has worked for you in node tbh, it didn't for me. At least not when using the provided fetch.

Cluster2a commented 11 months ago

@kyngs, sorry for the confusion, I am not talking about the hooks - there I needed the add the cookies manually - even on node.

I am talking about a fetch within a +page.server.ts ts (load()): image

Using the node adapter my cookies are passed to the backend, via credentials: 'include'.

Cluster2a commented 10 months ago

According to the sveltekit documentation the cookies should be passed from the client to the server fetch: https://kit.svelte.dev/docs/load#cookies

KyleFontenot commented 1 month ago

This may be fixed now from Bun's 1.1.27 release. I'd be curious if it does fix this.