Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Cluster2a opened this issue Nov 23, 2023 · 3 comments
Open

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

Cluster2a opened this issue Nov 23, 2023 · 3 comments

Comments

@Cluster2a
Copy link

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
Copy link

kyngs commented Dec 17, 2023

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
Copy link
Author

@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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants