How do we use different load()
for clients and servers?
#13257
-
Hi, I'm new to Svelte. One of my projects is switching from React to Svelte for better performance. Thanks for all the work! Currently, we can load data in Now let's consider a use case where we need to calculate something based on a user's account. A client can always store identity information using Web APIs. A server can identify a client using cookies and query the database for extra information. However, the cookies (and everything we can use to identify a user on the server, e.g. I haven't come up with any efficient strategy for this use case using the SvelteKit framework. Here are my ideas:
I would appreciate it for suggestions & references to existing solutions! Footnotes
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hi, welcome to svelte^^ In that case you can not really use server load functions at all i assume since you need to check on the client before requesting from the server. Check out this example in sveltelab: https://www.sveltelab.dev/ss4xsa91vlq3l8v?files=.%2Fsrc%2Froutes%2Fdata%2F%2Bpage.ts This approach still works with ssr but will always do a DB request if the I hope this is helpful to you! |
Beta Was this translation helpful? Give feedback.
-
Thank you @david-plugge for your response! It feels warm to have an active community. I believe that you understood my use case well and your example definitely solves the problem for the project. Thank you again! Here's more to the discussion: It is almost perfect though. Let's demonstrate the data path of your solution here:
In comparison, the most ideal data path for the server side is:
The ideal path reduces one How does it sound? Is the performance or productivity gain worth it? |
Beta Was this translation helpful? Give feedback.
The fetch function that is passed into load functions is doing a bit of magic here. Take a look at https://svelte.dev/docs/kit/load#Making-fetch-requests (3rd bullet point). No additional network requests are made.