This is an announce + help kind of a post.
Till now I had been using ssrwpo:ssr@3.0.0
with awesome results. But it uses electrode-react-ssr-caching
which is not working with Reactv16 yet.
So I started looking at the server-render
package and it too is great. Kudos to @benjamn.
https://blog.meteor.com/meteor-platform-is-still-alive-5f6426644555 helped me understand the process, and then I started tweaking.
This is what I am doing now:
- On the server I have defined the following:
Meteor.subscribe = function() {
return {
ready() {return true;}
};
};
- With hack 1, I am able to run my withTrackers on server too.
withTracker
is run once and only once on the server, hence I can easily set state in it so I use the following:
const CompWithTracker = = withTracker(() => {
// subscribe and get data
if (Meteor.isServer) actionToUpdateStore({type, data});
return data; // this ensures that on the client, the data from state is overwritten by that from meteor.
});
// mapDispatchToProps contains actionToUpdateStore, and mapStateToProps contains the needed states
const CompWithStore = connect(mapStateToProps, mapDispatchToProps)(CompWithTracker);
- Now while rendering, I do double rendering, the first one to set the states, and the second to generate html.
renderToString(<div id='react'><App location={sink.request.url} /></div>); // to set store
const preloadedState = store.getState();
sink.appendToBody(`
<script>
window.__PRELOADED_STATE__ = ${JSON.stringify(preloadedState).replace(/</g, '\\u003c')}
</script>
`);
sink.appendToBody(renderToString(<div id='react'><App location={sink.request.url} /></div>));
The approach works fine, the only visible problem being a lot of server cpu usage.
Pros:
- I don’t have to recreate the whole data methods on the server. Complete de-duplication.
- No extra parsing and checking routes. React Router v4 does everything already.
Cons:
- No caching.
- Poor server perf.
Questions:
- What are the potential pitfalls of this strategy, apart from server performance?
- How can I measure and improve server performance?