SSR + code-splitting
See original GitHub issueHey @leebenson!
Nice starter kit! I have a similar setup with SSR based on react-router@3
and everything works fine. I aim to refactor in the near future and this setup looks great for that. Wondering what your thoughts are on SSR + code-splitting with v4?
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
React Server Side Code Splitting Made.. Again - ITNEXT
Even more — SSR, without any JS sent to the client, is the best code splitting ever possible, as long all the things...
Read more >Code Splitting, SSR, Lazy loading React components - Medium
It's 2020! Everyone is talking about code splitting and you are pumped up to implement it. But, are you ready? Let's find out!...
Read more >Why code splitting is hard in react server side rendering?
It is basically a way to split code from a large file into smaller code bundles which can be requested on demand or...
Read more >What's the point of SSR when code splitting/lazy loading exists ...
There are definitely cases where CSR with lazy loading and code splitting is too slow, and there are cases where it's perfectly fine....
Read more >Code Splitting in React – Loadable Components to the Rescue
Code splitting is a way to split up your code from a large file into smaller code bundles. These can then be requested...
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
@scf4 - this actually works in production, but not via the new dev server:
test.js
app.js
I’m tracking this in https://github.com/reactql/kit/issues/30
Thanks @kuka.
Code-splitting on the server should actually work out-the-box.
Because we’re using Webpack 2 to generate bundles on both the client and the server, any strategy that uses
require.ensure()
(orimport()
-based Promises - I’m pushing an update for that this morning) should resolve well on the server, too. The reason I opted for server-side bundling is exactly for that reason - it should be possible (for the most part) to write code in one place, and know that it’ll work on the server, too. Same with .css, .json, inline image loading, etc.The one thing that’s currently missing is a built-in HOC for returning a ‘loading’ component whilst split code hasn’t yet returned back from its round-trip, and a recommended pattern in the docs for doing the actual split.
In the meantime, I whipped together this very quick example to demonstrate it working in practice:
Let’s say you have this:
src/test.js
And then inside src/app.js, we modify the
<Message>
component to grab the code-split message, instead of the current GraphQL:Running
npm start
will yield the expected result:And you can see the network request that grabbed this asynchronously:
Now, the neat this is that you also get this on the server, too - running
npm run build-run
will confirm:Browser files generated
And on the server, too
Of course, in this example, the
<Message>
component attempts to set its internal ‘message’ state after theimport()
Promise resolves. At this point, the server has already turned the React chain into HTML and fired it back to the screen.Since
import()
returns a Promise when the code-split data is available, the solution is to use a lib like redial to decorate components with data requirements, and then await the full chain being ready before rendering into HTML. This would work in the same way that Apollo’sgetDataFromTree
does for GraphQL queries.In a (soon-ish) future version, I’ll add those features to the starter kit, and update
src/app.js
to show examples of code splitting at work, and how you can simulate synchronous loading for a server environment.