Avoid Leaking User Data In Your SvelteKit App

  Рет қаралды 4,447

Joy of Code

Joy of Code

Күн бұрын

Пікірлер: 41
@stoched
@stoched 4 күн бұрын
For everyone that is confused here are the key takeaways: 1. Avoid accessing & mutating state in server environments (mainly your load functions) 2. Use contexts (setContext() + getContext()) to pass data down component trees to avoid prop drilling It is definitely a little confusing at first but once you understand that there are points in SvelteKit where that code gets executed on both the server and the client it starts to make sense. If you access/set some kind of state (think about global stores or some static class you made so you can access state anywhere) when the code is running on the server, you can accidentally leak data to other people. If you have SSR enabled, all of the top level code of your blocks in each page and component are executed on both the server AND the client. By using setContext('MY_COOL_CONTEXT', value: 0); that initial value is set on the server then on the client it is set AGAIN but the values aren't tied together. So once the page/component is rendered if you getContext('MY_COOL_CONTEXT') and update it, that change does NOT reflect on the server. Use caution if you immediately update context values on render instead of doing it in response to events like clicking on something or typing a value somewhere. If you absolutely have to then make sure the functions that update that state have proper guard clauses to only do it when you're in client land (typeof window === 'undefined') or certain clientside libraries are initialized - just something to make sure you don't inadvertently update state on the server. This is a where $effect() can be useful since that only executes on the client. Hope that helps! I agree that they should add a dedicated page in the docs on best practices with updating state and context. They have a small tid bit of info here svelte.dev/docs/svelte/context#Encapsulating-context-interactions but their description is very brief. This is definitely how you want to set and get context safely though. Unrelated but there are a bunch of "gotchas" when it comes to the new $state() runes where you can accidentally make values non-reactive. I wish they'd make a documentation page on this too. Just be careful if you have $state({ ... }) with some object then later destructure it. You can use the $inspect(myStatefulObject) rune to keep tabs on things in DEV environments. @JoyofCodeDev Nice video I really like when you jump in to Svelte's code to show how they are doing it. Definitely helps with understanding what's going on and what behaviors to expect from using different svelte features!
@JoyofCodeDev
@JoyofCodeDev 4 күн бұрын
the most important thing to know is: - the `load` function inside `+page.ts` runs on the server and client, unlike `+page.server.ts`, so it can be abused to keep the user state in sync on both sides - you should always return the data from the `load` function, and then update the state on the client because it's unique to the user
@ariel_chess
@ariel_chess 4 күн бұрын
They have to update the documentation about this
@joshuamacmaster5758
@joshuamacmaster5758 2 күн бұрын
I think the biggest thing no one mentions is that the context api creates a NEW state tree per REQUEST. I'm not sure why this isn't mentioned often because it makes understanding why it's used so much easier (beyond the component drilling benefit)
@thedelanyo
@thedelanyo 3 күн бұрын
7:47 . I think with cookies, to some extent servers are statefull. That is why user auth model is shared between the server and client these days. Another example is theme setting, and any user specific setting and a cookie can hold.
@MirSowasVonEgal
@MirSowasVonEgal 24 минут бұрын
How can I make the user in a component server side rendered without passing it as a prop? Is there a way to to this or was there a way explained? Context API, global stores and $page.data is all only csr.
@Michael_moa
@Michael_moa 3 күн бұрын
I'd love a blog or video on invalidate and invalidateAll, there isn't much content in this.
@josevilca3487
@josevilca3487 4 күн бұрын
The only thing that i really miss from svelte 4 is the auto update data, when it is updated by url state, now i need to use effect to update if the data changes. Please do a video for this
@C4CH3S
@C4CH3S 4 күн бұрын
Ja-Son
@couragic
@couragic 4 күн бұрын
ZHISON 🤔
@marcialabrahantes3369
@marcialabrahantes3369 4 күн бұрын
sounds French to me
@cole5981
@cole5981 3 күн бұрын
kzbin.info/www/bejne/q4OQl2eYZaxsotE
@EliSpizzichino
@EliSpizzichino 3 күн бұрын
@@cole5981 turns out it's the correct way of saying it!
@good-dev-student
@good-dev-student 3 күн бұрын
Thank you
@EasyViber
@EasyViber 3 күн бұрын
znači žava skript i žson ili je samo žson sa ž?
@AlexvanderValk
@AlexvanderValk 3 күн бұрын
I've solved this by subscribing to the Page store inside my custom store, instead of using the $effect to update my custom store. Any downsides to this approach?
@JoyofCodeDev
@JoyofCodeDev Күн бұрын
yeah since the page store uses the Context API
@AlexvanderValk
@AlexvanderValk Күн бұрын
@@JoyofCodeDev Thanks for the reply. It works for me right now so i left as it. It's great to see that many people find this concept of server / client side confusing. I thought it was just me.
@good-dev-student
@good-dev-student 3 күн бұрын
i have opened so many videos even i already watched to get ADS 😘😘 and support you for the new next videos love shared using signals of love ❤❤😍😍
@JLarky
@JLarky 3 күн бұрын
JSON
@zeyaddessouki890
@zeyaddessouki890 4 күн бұрын
Great Content. can yu upload you code so that ONe can look at it again for better understanding, thx :)
@EliSpizzichino
@EliSpizzichino 3 күн бұрын
I've still to understand the purpose of +page.ts why both on server and client?. I've recently tried to use it and realized it overcast the +page.server.ts 's load function... When to use it and what's the purpose/difference then running only on the server? I find it confusing, like there is no clear separation on .svelte either. Another thing I find confusing is why the data props are not reactive, I have to declare another variable one by one it's tedious and verbose...
@JoyofCodeDev
@JoyofCodeDev Күн бұрын
the short answer is because of server-side rendering
@marcialabrahantes3369
@marcialabrahantes3369 4 күн бұрын
Jay-son - unless you're French? 🤔😉
@Wombosvideo
@Wombosvideo 3 күн бұрын
Or, hear me out, the inventor of JSON meant for it to be pronounced the way Joy of Code pronounces it.
@HughBone
@HughBone 3 күн бұрын
@@WombosvideoAINT NO WAY
@WaxN-ey6vj
@WaxN-ey6vj 4 күн бұрын
In short, just return data instead of using state.
@oszi7058
@oszi7058 2 күн бұрын
So basically just use your brain!
@StarsOfMinecrafttr
@StarsOfMinecrafttr 3 күн бұрын
bruh this is nonsense tho, how can you leak user data, if you intentionally do share it you can but, that wouldnt be leaking you dont have to do that just use states bruh, if you login via ssr it might but still it shouldnt.
@JoyofCodeDev
@JoyofCodeDev Күн бұрын
I just showed you how
@fottymutunda6320
@fottymutunda6320 4 күн бұрын
I am confused by what I just watched
@JoyofCodeDev
@JoyofCodeDev 4 күн бұрын
about what exactly? 😄
@didiercatz
@didiercatz 4 күн бұрын
then watch it again
@fottymutunda6320
@fottymutunda6320 3 күн бұрын
@@JoyofCodeDev the pinned comment clarified it for me 😅
@Cazineer
@Cazineer 4 күн бұрын
What the hell did I just watch…
@natedunn3933
@natedunn3933 4 күн бұрын
A KZbin video. Hope this helps.
Understanding Effects In Svelte 5 And When To Use Them
27:31
Joy of Code
Рет қаралды 9 М.
Master The Svelte Context API
18:07
Joy of Code
Рет қаралды 7 М.
Why no RONALDO?! 🤔⚽️
00:28
Celine Dept
Рет қаралды 102 МЛН
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 20 МЛН
Sigma Kid Mistake #funny #sigma
00:17
CRAZY GREAPA
Рет қаралды 17 МЛН
We stopped using serverless. The results are insane.
18:27
Theo - t3․gg
Рет қаралды 135 М.
This New Svelte Feature Prevents Your App From Exploding
7:31
Joy of Code
Рет қаралды 1,5 М.
Different Ways To Share State In Svelte 5
28:41
Joy of Code
Рет қаралды 10 М.
Gitlab DELETING Production Databases | Prime Reacts
17:27
ThePrimeTime
Рет қаралды 353 М.
Advent of Neovim: Why Neovim?
10:21
TJ DeVries
Рет қаралды 35 М.
Using React Libraries Inside Svelte
16:55
Joy of Code
Рет қаралды 6 М.
The Svelte 5 Guide For Beginners
21:41
Joy of Code
Рет қаралды 34 М.
Why no RONALDO?! 🤔⚽️
00:28
Celine Dept
Рет қаралды 102 МЛН