-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
feat: implement prototype of safe server stores #12215
base: main
Are you sure you want to change the base?
Conversation
|
Posting this early example as a prototype explaining how it works, but the code needs to be updated in the |
A web platform equivalent of |
Agreed with this. As much as I would love to an easy to solution to this so I can stop using |
We don't need I think there's no logical reason to not add this feature as it's practically the most complained about feature in SvelteKit |
You don't need to use This feature brings the svelte docs more inline with how svelte works as a SPA, and will hopefully reduce confusion, and improve the learning curve with svelte while simultaneously reducing the divide between |
Ah yea I meant that the solution needs to be platform agnostic |
Yes, it is imo, is there any platforms that don't have support for |
Context
The unsafe behavior of global variables during SSR (particularly global stores) has been one of the most persistently complained about issues with SvelteKit (#4339).
The way stores are currently implemented result in state being leaked across requests which presents a very high security risk especially for users that are unaware of this difference of behavior in client-side vs server-side apps.
Solution
Using
AsyncLocalStorage
allows us to isolate the stores between server-side requests in Svelte.Docs: https://nodejs.org/api/async_context.html
Clarification: This is only a server-side feature, and is not necessary on the browser side of things as the client is naturally isolated from other clients - web support for
AsyncLocalStorage
is not requiredBenefits
hooks
Cannot access 'x' before initialization
My thoughts
unshared_writable
function inside of the Kit repository, but this behavior should be implemented in the Svelte repository itself, with a way for the server to replace thegetStore
function with another function which usesAsyncLocalStorage.getStore()
from the serverindex.js
codeinitialValue
is reused in a shallow way (so only primitive types are isolated). we could implement behavior to do a deep clone with structuredClone, alternatively, the syntax ofwritable
could bewritable(() => initial_value)
such that it is a function that returns a fresh object, but this would change the syntax and be a breaking change with the existing store functions insvelte/store
Support
This appears to be supported by all major server runtimes:
node:async_hooks
node:async_hooks
node:async_hooks
node:async_hooks
node:async_hooks
I believe they all use the same import module