Shared singleton store instance on server, handling dynamic user locales on SSR
See original GitHub issueHello. I’ve noticed that this library creates a single $locale store shared in sveltekit server instance. As per sveltekit documentation
Mutating any shared state on the server will affect all clients, not just the current one.
As I understand, in init
as per
<script>
init({
fallbackLocale: 'en',
initialLocale: getLocaleFromNavigator($session.acceptedLanguage)
});
</script>
you set current user’s request locale in this single store instance. I don’t think this is desired behavior. This could probably cause racing conditions between multiple SSR requests. Store should be created per request on SSR, probably in the highest level component, or am I missing something?
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:10 (3 by maintainers)
Top Results From Across the Web
Issues · cibernox/svelte-intl-precompile · GitHub
Cannot find module '$locales' or its corresponding type declarations ... Shared singleton store instance on server, handling dynamic user locales on SSR.
Read more >Server-Side Rendering (SSR) - Vue.js
The pattern declares shared state in a JavaScript module's root scope. This makes them singletons - i.e. there is only one instance of...
Read more >[feat] helper for creating stores · Issue #7105 · sveltejs/kit · GitHub
Describe the problem You can't just blindly create a store in SSR or it'll end up being a global store shared by all...
Read more >Writing Universal Code - Quasar Framework
Instead of directly creating a Router and Vuex Store instances, ... For tasks shared between server and client but use different platform APIs, ......
Read more >Next.js: The Good, Bad and Ugly - An Idiosyncratic Blog
Out of the box, it provides features like server-side rendering (SSR), static-site generation (SSG), incremental-static-regeneration (ISR), ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I don’t agree with your reasoning here. No one reported race-conditions on some different repository, how is that an argument.
Here is an official discussion on svelte kit where someone mentions data leaking: https://github.com/sveltejs/kit/discussions/4339#discussioncomment-3258927 Still, it’s anecdotal, just like you saying that you don’t think anyone has reported race conditions. It might be possible, it might be not. Please note that it might be hard to determine that such issue even exists in your production application if there is no one reporting such a mismatch, if you don’t have dedicated analytics for this or you don’t encounter it yourself. That is the nature of such issues. I don’t have any evidence to confirm the issue exists or not as my app is not in a production yet and I can’t perform any experiment myself. However I do think that if svelte documentation mentions explicitly not to keep shared state like this evidence is not really necessary, it’s just something you should not do period. If you do you might potentially encounter issues, which is the case here.
Here is official svelte kit paragraph saying explicitly that those stores should not be global:
https://kit.svelte.dev/docs/load#shared-state
Here is similar issue to this one on the library you mentioned: https://github.com/kaisermann/svelte-i18n/issues/165 As per this issue, the owner admits that he doesn’t really know if the library is compatibile with sveltekit. It’s still open: https://github.com/kaisermann/svelte-i18n/issues/166
If the goal of this library is to be compatible with svelte kit, this issue should be resolved.
The race condition seems possible in theory but I haven’t been able to reproduce it. If this repo is affected by this, i believe the svelte-i18n should too as our approach to setting the current locale is the same, and I don’t think anyone has reported race conditions.
I have the hunch that while possible in theory, the single threaded nature of javascript makes it not happen in practice.