8

Streaming Server Side Render in React 18

 4 months ago
source link: https://github.com/maxam2017/react-18-ssr
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

React 18 + SSR muscle

After React v18 is published, I think it's time to build React app with server-side rendering (SSR) once with those new APIs. Before that, I always use Next.js when it comes to SSR.

Caveat warning

This code isn't production-ready. Use with caution.

What's new

React v18 introduces new SSR Architecture. Thanks to the new Suspense API, the process of SSR isn't waterfall-like anymore. Below are two major unlocked SSR features:

white_check_mark Streaming HTML

white_check_mark Selective Hydration

Want to know more? See reactwg/react-18#37 for details.

Concept

Why do we need SSR? It's an important and very common question in the interview.

In the past, the web app was always rendered on the server in the form of HTML, which someone called Multiple Page Application (MPA). When the user clicked a link, the browser made an HTTP request to the server and waits for receiving the HTML of the next page.

However, when some modern frontend library like React.js comes up, the web app becomes Single Page Application (SPA). What does this mean? It becomes static! We can route between different pages without making requests to the server because everything is bootstrapped on client side.

Although SPA has a better user experience, it doesn't SEO-friendly and also makes the first painting longer. To solve those problems, the concept of Universal App (Hybrid) comes up, it combines the benefit of MPA and SPA, using server side rendering and making client routing works. Sounds great right? Moreover, most famous frameworks, like Next.js (React.js framework), Nuxt.js (Vue.js framework), and Svelte Kit (Svelte.js framework) already use this way to implement web apps.

Implementation

We need a node server to render dehydrated React elements and use ReactDOM.hydrate to make our DOM interactive. There're some steps listed below:

  • setup a koa server (for rendering)
  • use ReactDOMServer to dehydrate
    • renderToString
    • renderToPipeableStream (new in React v18 sparkles)
  • use ReactDOM.hydrate

To here, we have already built the React app with the easiest SSR.

More Features:

Links


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK