SSR data loading
How would it be possible to upload a log to the server during server rendering?
Right now there is not easy out-of-box solution.
You can you WebSocket to download it in old manner. Or you can use some other way and then just use store.add to set data manually.
Do you have a idea how to do it better?
Maybe, I want to have SSR client, which takes user subscribes and return log.
Seems, that one instance SSR Client from one ssr server decides the problem. I can load user subscribes from react code and give from ssr client.
What is a problems right now? SSR client will load anything by the same WS way.
Do you need some Promise, which will be resolved when all subscriptions will be loaded?
My code works isomorphic, it loads data isomorphic, but I can't create a Browser Client instance on the server. I need to load the log on the server without changing my client code (in promis, probably), serialize it and embed it in the client. On the client, I want to run it, combine it with the client log and start working with it.
Without data on the server I can not build HTML
but I can't create a Browser Client instance on the server
Why?
You may need a WebSocket implementation:
yarn add ws
global.WebSocket = require('ws')
@nikolay-govorov is that solution fit you?
After the discussion with @droganov I am thinking about this API for SSR:
- Logux Server will have HTTP API. Anyway, we need it for WebSocket fallback.
- Logux Client by default will use HTTP API on missed
global.WebSocket. - Add
client.ignoreSubscription = RegExp | action: Action => booleanto ignore unnecessary subscriptions on the server. - Add
client.isSubscribingto track all current subscriptions. - Check that
CrossTabClientwill work in Node.js without browser API polyfills. - Add the option to
MemoryStoreto pass initial actions to the log. - Add
log.getAll(): Promise<[Action, Meta][]>
Here is an SSR process:
- We load the normal Logux Client on the SSR server. Just need to specify
userIdandtoken. Optionally developer could specifyclient#ignoreSubscriptioninisNodeif-statement. - Logux Client automatically switches to HTTP mode. Collects all subscriptions, sends the single HTTP request to Logux Server and the server responds with the list of actions.
- SSR server waits for
client.isSubscribingand then renders HTML again. - SSR sends the final HTML and
client.log.getAll()to the client. - The client shows HTML, re-initialize Logux Client with received log and rehydrate React.
- Subscriptions on the client subscribing again, but since they have a log, they will send subscription requests with the
sincefield to avoid re-loading existed data.