How best to persist state between client renders, e.g. appContext #705
-
I've been trialling out vite-plugin-ssr as a possible replacement to my existing toolchain. VPS has really impressed me with it's features, and it's documentation for those features. Really amazing work. Thank-you to all the contributors. One thing I haven't worked out how to do yet is to replicate things I'd normally create in my single JavaScript entry, like a persistent api client / store. This would usually happen before fanning out to separate pages. (e.g. render-entry.ts and client-entry.ts) The docs mention that pageContext is deliberately not what I'm looking for:
The docs also describe that persisting state is why you would choose client-side routing:
So my question is, how? If I do want appContext how should I do it? Any help or advice would be appreciated please. Some ideas I went looked at:
async function render(pageContext: PageContextClient, appContext: AppContextClient) {
...
}
If I understand correctly, I think the docs suggest this solution of singleton modules. So maybe that is the preferred method. if (pageContext.isHydration) {
// `pageContext.initialState` is available here
initClientSide(pageContext.initialState)
}
if(!window.globalStore) {
window.globalStore = createStore()
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Usually folks use state management tools such as Pinia for Vue, and for React Redux or its many alternatives. These tools handle the global client-side state on the user's behalf. But, if you wish to handle your global state manually yourself, then there is nothing wrong with using the global scope Let me know if that answers your question. I'm glad you're enjoying VPS. |
Beta Was this translation helpful? Give feedback.
Usually folks use state management tools such as Pinia for Vue, and for React Redux or its many alternatives.
These tools handle the global client-side state on the user's behalf. But, if you wish to handle your global state manually yourself, then there is nothing wrong with using the global scope
window
– just make sure to use a unique prefix such aswindow.__my_global_scope.someGlobalData
to avoid conflicts with libraries and browser extensions. (You can also use aSymbol()
to avoid conflict with certainty.)Let me know if that answers your question. I'm glad you're enjoying VPS.