Skip to content

Commit eb28757

Browse files
feat: progressive hydration option
1 parent 0a4aabb commit eb28757

File tree

1 file changed

+89
-5
lines changed

1 file changed

+89
-5
lines changed

src/utils/data/concepts.js

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ app.get('*', (req, res) => {
283283
\${scriptTags}
284284
</body>
285285
</html>
286-
\`)
286+
\`);
287287
});
288288
...
289289
@@ -330,8 +330,7 @@ const App = () => (
330330
<Chats />
331331
</div>
332332
);
333-
334-
export default App;`,
333+
...`,
335334
textDescription:`
336335
1. Install [@loadable/babel-plugin](https://www.npmjs.com/package/@loadable/babel-plugin) and setup \`.babelrc\` in order to transpile your server-side code.
337336
\`\`\`
@@ -426,8 +425,7 @@ const App = () => (
426425
<Chats />
427426
</div>
428427
);
429-
430-
export default App;`,
428+
...`,
431429
textDescription:`
432430
1. Install [@loadable/babel-plugin](https://www.npmjs.com/package/@loadable/babel-plugin) and setup \`.babelrc\` in order to transpile your server-side code.
433431
\`\`\`
@@ -455,6 +453,92 @@ textDescription:`
455453
linesInPrimary: [10, 11, 12, 13, 14, 16, 17, 18, 19]
456454
},
457455
{
456+
id: 'progressive-hydration',
457+
optionName: 'Progressive Hydration',
458+
codeSnippet:
459+
`// server/index.js
460+
import express from 'express';
461+
import React from 'react';
462+
import ReactDOMServer from 'react-dom/server';
463+
import App from './App';
464+
465+
const app = express();
466+
467+
app.get('*', (req, res) => {
468+
const html = ReactDOMServer.renderToString(<App />);
469+
470+
res.set('content-type', 'text/html');
471+
res.send(\`
472+
<!DOCTYPE html>
473+
<html>
474+
<head>
475+
</head>
476+
<body>
477+
<div id='root'>\${html}</div>
478+
</body>
479+
</html>
480+
\`);
481+
});
482+
...
483+
484+
// client/index.js
485+
import React from 'react';
486+
import ReactDOM from 'react-dom';
487+
import App from './App';
488+
489+
const rootElement = document.getElementById('root');
490+
491+
ReactDOM.createRoot(rootElement, {
492+
hydrate: true
493+
}).render(<App />);
494+
495+
// client/App.js
496+
import React, { lazy, Suspense } from 'react';
497+
import ErrorBoundary from './ErrorBoundary';
498+
499+
const Messages = lazy(() => import('./Messages'));
500+
const MessageForm = lazy(() => import('./MessageForm'));
501+
const EmojiPicker = lazy(() => import('./EmojiPicker'));
502+
503+
const Chats = () => {
504+
...
505+
return (
506+
<ErrorBoundary>
507+
<div>
508+
<Suspense fallback={<h2>Loading Messages...</h2>}>
509+
<Messages />
510+
</Suspense>
511+
<Suspense fallback={<h2>Loading MessageForm...</h2>}>
512+
<MessageForm />
513+
</Suspense>
514+
{isEmojiPickerOpen && (
515+
<Suspense fallback={<h2>Loading EmojiPicker...</h2>}>
516+
<EmojiPicker />
517+
</Suspense>
518+
)}
519+
</div>
520+
</ErrorBoundary>
521+
);
522+
};
523+
524+
const App = () => (
525+
<div>
526+
...
527+
<Chats />
528+
</div>
529+
);
530+
...`,
531+
textDescription:`
532+
[Concurrent Mode](https://reactjs.org/docs/concurrent-mode-intro.html) enables Progressive Hydration — making individual React components interactive before all JS loads.
533+
534+
#### References:
535+
* [(twitter) Progressive Hydration by Dan Abramov](https://twitter.com/dan_abramov/status/1200111677833973760?s=19)
536+
* [(CodeSandBox) React Progressive Hydration Demo](https://codesandbox.io/s/floral-worker-xwbwv)
537+
* [(Glitch) React Chats App Demo with Progressive Hydration](https://anton-karlovskiy-cra-chats-demo-with-progressive-hydration.glitch.me)
538+
`,
539+
linesInPrimary: [4, 10, 33, 34, 35, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]
540+
},
541+
{
458542
id: 'import-on-visibility',
459543
optionName: 'Import on visibility',
460544
codeSnippet:

0 commit comments

Comments
 (0)