-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
SSR fetch without "res.json()" makes client call endpoint again #842
Comments
Curious about what the behaviour should be here? The assumption was that if you don't consume the body with |
So I had this issue when wanting to set a cookie using fetch but didn't care about the response. (Setting cookie with fetch during SSR doesn't work btw, that depending on how you look at it also could be considered an issue). I'm not going to use this approach, but thought that it was worth reporting anyway.
I would say that the question wasn't if I wanted the response inlined, I just didn't want to do the request twice. Once during SSR, and then again by the client. A scenario where you don't care about the response could also be analytics, send some data like page viewed, but not caring about the response. |
Another side effect of this, depending on wether you transform the response or not (with res.json(), or whatever) // load() at /src/routes/xx/[slug]/index.svelte
const res = await fetch(`/xx/${page.params.slug}.json`);
if (res.redirected) { // We are not allowed
console.log('should be redirected'); // The console is triggered in browser but not in SSR
return {
status: 303,
redirect: '/no-rights' // This has no effect for not matching SSR and browser returned objects
};
} If the api response was:
The server gets as the response: {
url: undefined,
status: 303,
statusText: '',
headers: { location: '/no-rights' },
counter: undefined,
highWaterMark: undefined
} The browser gets: {
body: ReadableStream
bodyUsed: false
headers: Headers // This do not contain location
ok: true
redirected: true
status: 200
statusText: "OK"
type: "basic"
url: "http://localhost:3000/no-rights"
} Things gets even more messy if I use Because of this and how different the params in the response are, to cover all the cases I can only do this: const res = await fetch(`/xx/${page.params.slug}.json`);
res.text(); // with this, first page is directly redirected, then back routes to the same path will make the screen blink as browsers fetch again
// without .text() page always blinks to fetch from browser.
// handler of load for all the cases
// left side ssr mode - right side browser mode
if (!res.ok && res.headers.has('location') || res.redirected) {
return {
status: 303,
redirect: res.headers.get('location') || res.url // ssr vs browser
};
} The result of this is not nice as the route is not really protected and in some cases the browser still needs to load this again and the delay makes the screen to blink and expose the page momentanealy. |
Hi, any plan for fixing this issue? |
I think I'm running into this issue as well, trying to do a client side fetch with .blob() |
Closing as duplicate of #8302 - we will document this behavior, but in short there's nothing we can do about this here. We need some hook to serialize the response. |
Describe the bug
If not calling
res.json()
in theload()
function, thesvelte-data
-script is not added during SSR, so the client will also do the same fetch.Logs
--
To Reproduce
Expected behavior
The client wouldn't do the fetch again
Information about your SvelteKit Installation:
Diagnostics
Your browser: Safari
Your adapter: Node
Severity
Low, easily worked around.
The text was updated successfully, but these errors were encountered: