React SSR

React is very popular JavaScript web UI framework. A React application is “compiled” into an HTML and JavaScript static web site. The web UI is rendered through the generated JavaScript code. However, it is often too slow and resource consuming to execute the complex generated JavaScript entirely in the browser to build the interactive HTML DOM objects. React Server Side Rendering (SSR) delegates the JavaScript UI rendering to a server, and have the server stream rendered HTML DOM objects to the browser. The WasmEdge JavaScript runtime provides a lightweight and high performance container to run React SSR functions on edge servers.

Server-side rendering (SSR) is a popular technique for rendering a client-side single page application (SPA) on the server and then sending a fully rendered page to the client. This allows for dynamic components to be served as static HTML markup. This approach can be useful for search engine optimization (SEO) when indexing does not handle JavaScript properly. It may also be beneficial in situations where downloading a large JavaScript bundle is impaired by a slow network. — from Digital Ocean.

In this article, we will show you how to use the WasmEdge QuickJS runtime to implement a React SSR function. Compared with the Docker + Linux + nodejs + v8 approach, WasmEdge is much lighter (1% of the footprint) and safer, provides better resource isolation and management, and has similar non-JIT (safe) performance.

We will start from a complete tutorial to create and deploy a simple React SSR web application from the standard template. Then, we will discuss two modes of SSR: static and streaming rendering. Static rendering is easy to understand and implement. Streaming rendering, on the other hand, provides better user experience since the user can see partial results while waiting in front of the browser. We will walk through the key code snippets for both static and streaming rendering.

Getting started

Step 1 — Create the React App

First, use npx to create a new React app. Let’s name the app react-ssr-example.

  1. npx create-react-app react-ssr-example

Then, cd into the directory for the newly created app.

  1. cd react-ssr-example

Start the new app in order to verify the installation.

  1. npm start

You should see the example React app displayed in your browser window. At this stage, the app is rendered in the browser. The browser runs the generated React JavaScript to build the HTML DOM UI.

Now in order to prepare for SSR, you will need to make some changes to the app’s index.js file. Change ReactDOM’s render method to hydrate to indicate to the DOM renderer that you intend to rehydrate the app after it is rendered on the server. Replace the contents of the index.js file with the following.

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import App from './App';
  4. ReactDOM.hydrate(
  5. <React.StrictMode>
  6. <App />
  7. </React.StrictMode>,
  8. document.getElementById('root')
  9. );

Note: you should import React redundantly in the src/App.js, so the server will recognize it.

  1. import React from 'react';
  2. //...

That concludes setting up the application, you can move on to setting up the server-side rendering functions.

Step 2 — Create an WasmEdge QuickJS Server and Render the App Component

Now that you have the app in place, let’s set up a server that will render the HTML DOM by running the React JavaScript and then send the rendered elements to the browser. We will use WasmEdge as a secure, high-performance and lightweight container to run React JavaScript.

Create a new server directory in the project’s root directory.

  1. mkdir server

Then, inside the server directory, create a new index.js file with the server code.

  1. import * as React from 'react';
  2. import ReactDOMServer from 'react-dom/server';
  3. import * as std from 'std';
  4. import * as http from 'wasi_http';
  5. import * as net from 'wasi_net';
  6. import App from '../src/App.js';
  7. async function handle_client(cs) {
  8. print('open:', cs.peer());
  9. let buffer = new http.Buffer();
  10. while (true) {
  11. try {
  12. let d = await cs.read();
  13. if (d == undefined || d.byteLength <= 0) {
  14. return;
  15. }
  16. buffer.append(d);
  17. let req = buffer.parseRequest();
  18. if (req instanceof http.WasiRequest) {
  19. handle_req(cs, req);
  20. break;
  21. }
  22. } catch (e) {
  23. print(e);
  24. }
  25. }
  26. print('end:', cs.peer());
  27. }
  28. function enlargeArray(oldArr, newLength) {
  29. let newArr = new Uint8Array(newLength);
  30. oldArr && newArr.set(oldArr, 0);
  31. return newArr;
  32. }
  33. async function handle_req(s, req) {
  34. print('uri:', req.uri)
  35. let resp = new http.WasiResponse();
  36. let content = '';
  37. if (req.uri == '/') {
  38. const app = ReactDOMServer.renderToString(<App />);
  39. content = std.loadFile('./build/index.html');
  40. content = content.replace('<div id="root"></div>', `<div id="root">${app}</div>`);
  41. } else {
  42. let chunk = 1000; // Chunk size of each reading
  43. let length = 0; // The whole length of the file
  44. let byteArray = null; // File content as Uint8Array
  45. // Read file into byteArray by chunk
  46. let file = std.open('./build' + req.uri, 'r');
  47. while (true) {
  48. byteArray = enlargeArray(byteArray, length + chunk);
  49. let readLen = file.read(byteArray.buffer, length, chunk);
  50. length += readLen;
  51. if (readLen < chunk) {
  52. break;
  53. }
  54. }
  55. content = byteArray.slice(0, length).buffer;
  56. file.close();
  57. }
  58. let contentType = 'text/html; charset=utf-8';
  59. if (req.uri.endsWith('.css')) {
  60. contentType = 'text/css; charset=utf-8';
  61. } else if (req.uri.endsWith('.js')) {
  62. contentType = 'text/javascript; charset=utf-8';
  63. } else if (req.uri.endsWith('.json')) {
  64. contentType = 'text/json; charset=utf-8';
  65. } else if (req.uri.endsWith('.ico')) {
  66. contentType = 'image/vnd.microsoft.icon';
  67. } else if (req.uri.endsWith('.png')) {
  68. contentType = 'image/png';
  69. }
  70. resp.headers = {
  71. 'Content-Type': contentType
  72. };
  73. let r = resp.encode(content);
  74. s.write(r);
  75. }
  76. async function server_start() {
  77. print('listen 8002...');
  78. try {
  79. let s = new net.WasiTcpServer(8002);
  80. for (var i = 0; ; i++) {
  81. let cs = await s.accept();
  82. handle_client(cs);
  83. }
  84. } catch (e) {
  85. print(e);
  86. }
  87. }
  88. server_start();

The server renders the <App> component, and then sends the rendered HTML string back to the browser. Three important things are taking place here.

  • ReactDOMServer’s renderToString is used to render the <App/> to an HTML string.
  • The index.html file from the app’s build output directory is loaded as a template. The app’s content is injected into the <div> element with an id of "root". It is then sent back as HTTP response.
  • Other files from the build directory are read and served as needed at the requests of the browser.

Step 3 — Build and deploy

For the server code to work, you will need to bundle and transpile it. In this section, we will show you how to use webpack and Babel. In this next section, we will demonstrate an alternative (and potentially easier) approach using rollup.js.

Create a new Babel configuration file named .babelrc.json in the project’s root directory and add the env and react-app presets.

  1. {
  2. "presets": [
  3. "@babel/preset-env",
  4. "@babel/preset-react"
  5. ]
  6. }

Create a webpack config for the server that uses Babel Loader to transpile the code. Start by creating the webpack.server.js file in the project’s root directory.

  1. const path = require('path');
  2. module.exports = {
  3. entry: './server/index.js',
  4. externals: [
  5. {"wasi_http": "wasi_http"},
  6. {"wasi_net": "wasi_net"},
  7. {"std": "std"}
  8. ],
  9. output: {
  10. path: path.resolve('server-build'),
  11. filename: 'index.js',
  12. chunkFormat: "module",
  13. library: {
  14. type: "module"
  15. },
  16. },
  17. experiments: {
  18. outputModule: true
  19. },
  20. module: {
  21. rules: [
  22. {
  23. test: /\.js$/,
  24. use: 'babel-loader'
  25. },
  26. {
  27. test: /\.css$/,
  28. use: ["css-loader"]
  29. },
  30. {
  31. test: /\.svg$/,
  32. use: ["svg-url-loader"]
  33. }
  34. ]
  35. }
  36. };

With this configuration, the transpiled server bundle will be output to the server-build folder in a file called index.js.

Next, add the svg-url-loader package by entering the following commands in your terminal.

  1. npm install svg-url-loader --save-dev

This completes the dependency installation and webpack and Babel configuration.

Now, revisit package.json and add helper npm scripts. Add dev:build-server, dev:start-server scripts to the package.json file to build and serve the SSR application.

  1. "scripts": {
  2. "dev:build-server": "NODE_ENV=development webpack --config webpack.server.js --mode=development",
  3. "dev:start-server": "wasmedge --dir .:. wasmedge_quickjs.wasm ./server-build/index.js",
  4. // ...
  5. },
  • The dev:build-server script sets the environment to "development" and invokes webpack with the configuration file you created earlier.
  • The dev:start-server script runs the WasmEdge server from the wasmedge CLI tool to serve the built output. The wasmedge_quickjs.wasm program contains the QuickJS runtime. Learn more

Now you can run the following commands to build the client-side app, bundle and transpile the server code, and start up the server on :8002.

  1. npm run build
  2. npm run dev:build-server
  3. npm run dev:start-server

Open http://localhost:8002/ in your web browser and observe your server-side rendered app.

Previously, the HTML source in the browser is simply the template with SSR placeholders.

  1. Output
  2. <div id="root"></div>

Now, with the SSR function running on the server, the HTML source in the browser is as follows.

  1. Output
  2. <div id="root"><div class="App" data-reactroot="">...</div></div>

Step 4 (alternative) — build and deploy with rollup.js

Alternatively, you could use the rollup.js tool to package all application components and library modules into a single file for WasmEdge to execute.

Create a rollup config for the server that uses Babel Loader to transpile the code. Start by creating the rollup.config.js file in the project’s root directory.

  1. const {babel} = require('@rollup/plugin-babel');
  2. const nodeResolve = require('@rollup/plugin-node-resolve');
  3. const commonjs = require('@rollup/plugin-commonjs');
  4. const replace = require('@rollup/plugin-replace');
  5. const globals = require('rollup-plugin-node-globals');
  6. const builtins = require('rollup-plugin-node-builtins');
  7. const plugin_async = require('rollup-plugin-async');
  8. const css = require("rollup-plugin-import-css");
  9. const svg = require('rollup-plugin-svg');
  10. const babelOptions = {
  11. babelrc: false,
  12. presets: [
  13. '@babel/preset-react'
  14. ],
  15. babelHelpers: 'bundled'
  16. };
  17. module.exports = [
  18. {
  19. input: './server/index.js',
  20. output: {
  21. file: 'server-build/index.js',
  22. format: 'esm',
  23. },
  24. external: [ 'std', 'wasi_net','wasi_http'],
  25. plugins: [
  26. plugin_async(),
  27. babel(babelOptions),
  28. nodeResolve({preferBuiltins: true}),
  29. commonjs({ignoreDynamicRequires: false}),
  30. css(),
  31. svg({base64: true}),
  32. globals(),
  33. builtins(),
  34. replace({
  35. preventAssignment: true,
  36. 'process.env.NODE_ENV': JSON.stringify('production'),
  37. 'process.env.NODE_DEBUG': JSON.stringify(''),
  38. }),
  39. ],
  40. },
  41. ];

With this configuration, the transpiled server bundle will be output to the server-build folder in a file called index.js.

Next, add the dependent packages to the package.json then install with npm.

  1. "devDependencies": {
  2. //...
  3. "@rollup/plugin-babel": "^5.3.0",
  4. "@rollup/plugin-commonjs": "^21.0.1",
  5. "@rollup/plugin-node-resolve": "^7.1.3",
  6. "@rollup/plugin-replace": "^3.0.0",
  7. "rollup": "^2.60.1",
  8. "rollup-plugin-async": "^1.2.0",
  9. "rollup-plugin-import-css": "^3.0.3",
  10. "rollup-plugin-node-builtins": "^2.1.2",
  11. "rollup-plugin-node-globals": "^1.4.0",
  12. "rollup-plugin-svg": "^2.0.0"
  13. }
  1. npm install

This completes the dependency installation and rollup configuration.

Now, revisit package.json and add helper npm scripts. Add dev:build-server, dev:start-server scripts to the package.json file to build and serve the SSR application.

  1. "scripts": {
  2. "dev:build-server": "rollup -c rollup.config.js",
  3. "dev:start-server": "wasmedge --dir .:. wasmedge_quickjs.wasm ./server-build/index.js",
  4. // ...
  5. },
  • The dev:build-server script sets the environment to "development" and invokes webpack with the configuration file you created earlier.
  • The dev:start-server script runs the WasmEdge server from the wasmedge CLI tool to serve the built output. The wasmedge_quickjs.wasm program contains the QuickJS runtime. Learn more

Now you can run the following commands to build the client-side app, bundle and transpile the server code, and start up the server on :8002.

  1. npm run build
  2. npm run dev:build-server
  3. npm run dev:start-server

Open http://localhost:8002/ in your web browser and observe your server-side rendered app.

Previously, the HTML source in the browser is simply the template with SSR placeholders.

  1. Output
  2. <div id="root"></div>

Now, with the SSR function running on the server, the HTML source in the browser is as follows.

  1. Output
  2. <div id="root"><div class="App" data-reactroot="">...</div></div>

In the next two sections, we will dive deeper into the source code for two ready-made sample applications for static and streaming rendering of SSR.

Static rendering

The example_js/react_ssr folder in the GitHub repo contains the example’s source code. It showcases how to compose HTML templates and render them into an HTML string in a JavaScript app running in WasmEdge.

The component/Home.jsx file is the main page template in React.

  1. import React from 'react';
  2. import Page from './Page.jsx';
  3. class Home extends React.Component {
  4. render() {
  5. const { dataList = [] } = this.props;
  6. return (
  7. <div>
  8. <div>This is home</div>
  9. <Page></Page>
  10. </div>
  11. );
  12. }
  13. };
  14. export default Home;

The Home.jpx template includes a Page.jpx template for part of the page.

  1. import React from 'react';
  2. class Page extends React.Component {
  3. render() {
  4. const { dataList = [] } = this.props;
  5. return (
  6. <div>
  7. <div>This is page</div>
  8. </div>
  9. );
  10. }
  11. };
  12. export default Page;

The main.js file calls React to render the templates into HTML.

  1. import React from 'react';
  2. import {renderToString} from 'react-dom/server';
  3. import Home from './component/Home.jsx';
  4. const content = renderToString(React.createElement(Home));
  5. console.log(content);

The rollup.config.js and package.json files are to build the React SSR dependencies and components into a bundled JavaScript file for WasmEdge. You should use the npm command to build it. The output is in the dist/main.js file.

  1. npm install
  2. npm run build

To run the example, do the following on the CLI. You can see that the templates are successfully composed into an HTML string.

  1. $ cd example_js/react_ssr
  2. $ wasmedge --dir .:. ../../target/wasm32-wasi/release/wasmedge_quickjs.wasm dist/main.js
  3. <div data-reactroot=""><div>This is home</div><div><div>This is page</div></div></div>

Note: the --dir .:. on the command line is to give wasmedge permission to read the local directory in the file system for the dist/main.js file.

Streaming rendering

The example_js/react_ssr_stream folder in the GitHub repo contains the example’s source code. It showcases how to streaming render an HTML string from templates in a JavaScript app running in WasmEdge.

The component/LazyHome.jsx file is the main page template in React. It “lazy” loads the inner page template after a 2s delay once the outer HTML is rendered and returned to the user.

  1. import React, { Suspense } from 'react';
  2. import * as LazyPage from './LazyPage.jsx';
  3. async function sleep(ms) {
  4. return new Promise((r, _) => {
  5. setTimeout(() => r(), ms)
  6. });
  7. }
  8. async function loadLazyPage() {
  9. await sleep(2000);
  10. return LazyPage
  11. }
  12. class LazyHome extends React.Component {
  13. render() {
  14. let LazyPage1 = React.lazy(() => loadLazyPage());
  15. return (
  16. <html lang="en">
  17. <head>
  18. <meta charSet="utf-8" />
  19. <title>Title</title>
  20. </head>
  21. <body>
  22. <div>
  23. <div> This is LazyHome </div>
  24. <Suspense fallback={<div> loading... </div>}>
  25. <LazyPage1 />
  26. </Suspense>
  27. </div>
  28. </body>
  29. </html>
  30. );
  31. }
  32. }
  33. export default LazyHome;

The LazyPage.jsx is the inner page template. It is rendered 2s after the outer page is already returned to the user.

  1. import React from 'react';
  2. class LazyPage extends React.Component {
  3. render() {
  4. return (
  5. <div>
  6. <div>
  7. This is lazy page
  8. </div>
  9. </div>
  10. );
  11. }
  12. }
  13. export default LazyPage;

The main.mjs file starts a non-blocking HTTP server, and then renders the HTML page in multiple chuncks to the response. When a HTTP request comes in, the handle_client() function is called to render the HTML and to send back the results through the stream.

  1. import * as React from 'react';
  2. import { renderToPipeableStream } from 'react-dom/server';
  3. import * as http from 'wasi_http';
  4. import * as net from 'wasi_net';
  5. import LazyHome from './component/LazyHome.jsx';
  6. async function handle_client(s) {
  7. let resp = new http.WasiResponse();
  8. resp.headers = {
  9. "Content-Type": "text/html; charset=utf-8"
  10. }
  11. renderToPipeableStream(<LazyHome />).pipe(resp.chunk(s));
  12. }
  13. async function server_start() {
  14. print('listen 8001...');
  15. let s = new net.WasiTcpServer(8001);
  16. for (var i = 0; i < 100; i++) {
  17. let cs = await s.accept();
  18. handle_client(cs);
  19. }
  20. }
  21. server_start();

The rollup.config.js and package.json files are to build the React SSR dependencies and components into a bundled JavaScript file for WasmEdge. You should use the npm command to build it. The output is in the dist/main.mjs file.

  1. npm install
  2. npm run build

To run the example, do the following on the CLI to start the server.

  1. cd example_js/react_ssr_stream
  2. nohup wasmedge --dir .:. ../../target/wasm32-wasi/release/wasmedge_quickjs.wasm dist/main.mjs &

Send the server a HTTP request via curl or the browser.

  1. curl http://localhost:8001

The results are as follows. The service first returns an HTML page with an empty inner section (i.e., the loading section), and then 2s later, the HTML content for the inner section and the JavaScript to display it.

  1. % Total % Received % Xferd Average Speed Time Time Time Current
  2. Dload Upload Total Spent Left Speed
  3. 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
  4. 100 211 0 211 0 0 1029 0 --:--:-- --:--:-- --:--:-- 1024
  5. 100 275 0 275 0 0 221 0 --:--:-- 0:00:01 --:--:-- 220
  6. 100 547 0 547 0 0 245 0 --:--:-- 0:00:02 --:--:-- 245
  7. 100 1020 0 1020 0 0 413 0 --:--:-- 0:00:02 --:--:-- 413
  8. <!DOCTYPE html><html lang="en"><head><meta charSet="utf-8"/><title>Title</title></head><body><div><div> This is LazyHome </div><!--$?--><template id="B:0"></template><div> loading... </div><!--/$--></div></body></html><div hidden id="S:0"><template id="P:1"></template></div><div hidden id="S:1"><div><div>This is lazy page</div></div></div><script>function $RS(a,b){a=document.getElementById(a);b=document.getElementById(b);for(a.parentNode.removeChild(a);a.firstChild;)b.parentNode.insertBefore(a.firstChild,b);b.parentNode.removeChild(b)};$RS("S:1","P:1")</script><script>function $RC(a,b){a=document.getElementById(a);b=document.getElementById(b);b.parentNode.removeChild(b);if(a){a=a.previousSibling;var f=a.parentNode,c=a.nextSibling,e=0;do{if(c&&8===c.nodeType){var d=c.data;if("/$"===d)if(0===e)break;else e--;else"$"!==d&&"$?"!==d&&"$!"!==d||e++}d=c.nextSibling;f.removeChild(c);c=d}while(c);for(;b.firstChild;)f.insertBefore(b.firstChild,c);a.data="$";a._reactRetry&&a._reactRetry()}};$RC("B:0","S:0")</script>

React 18 SSR

In this section, we will demonstrate a complete React 18 SSR application. It renders the web UI through streaming SSR. The example_js/react18_ssr folder in the GitHub repo contains the example’s source code. The component folder contains the entire React 18 application’s source code, and the public folder contains the public resources (CSS and images) for the web application.

The main.mjs file starts a non-blocking HTTP server, maps the main.css and main.js files in the public folder to web URLs, and then renders the HTML page for each request in renderToPipeableStream().

  1. import * as React from 'react';
  2. import { renderToPipeableStream } from 'react-dom/server';
  3. import * as http from 'wasi_http';
  4. import * as net from 'wasi_net';
  5. import * as std from 'std';
  6. import App from './component/App.js';
  7. import { DataProvider } from './component/data.js'
  8. let assets = {
  9. 'main.js': '/main.js',
  10. 'main.css': '/main.css',
  11. };
  12. const css = std.loadFile('./public/main.css')
  13. function createServerData() {
  14. let done = false;
  15. let promise = null;
  16. return {
  17. read() {
  18. if (done) {
  19. return;
  20. }
  21. if (promise) {
  22. throw promise;
  23. }
  24. promise = new Promise(resolve => {
  25. setTimeout(() => {
  26. done = true;
  27. promise = null;
  28. resolve();
  29. }, 2000);
  30. });
  31. throw promise;
  32. },
  33. };
  34. }
  35. async function handle_client(cs) {
  36. print('open:', cs.peer());
  37. let buffer = new http.Buffer();
  38. while (true) {
  39. try {
  40. let d = await cs.read();
  41. if (d == undefined || d.byteLength <= 0) {
  42. return;
  43. }
  44. buffer.append(d);
  45. let req = buffer.parseRequest();
  46. if (req instanceof http.WasiRequest) {
  47. handle_req(cs, req);
  48. break;
  49. }
  50. } catch (e) {
  51. print(e);
  52. }
  53. }
  54. print('end:', cs.peer());
  55. }
  56. async function handle_req(s, req) {
  57. print('uri:', req.uri)
  58. let resp = new http.WasiResponse();
  59. if (req.uri == '/main.css') {
  60. resp.headers = {
  61. "Content-Type": "text/css; charset=utf-8"
  62. }
  63. let r = resp.encode(css);
  64. s.write(r);
  65. } else {
  66. resp.headers = {
  67. "Content-Type": "text/html; charset=utf-8"
  68. }
  69. let data = createServerData()
  70. renderToPipeableStream(
  71. <DataProvider data={data}>
  72. <App assets={assets} />
  73. </DataProvider>
  74. ).pipe(resp.chunk(s));
  75. }
  76. }
  77. async function server_start() {
  78. print('listen 8002...');
  79. try {
  80. let s = new net.WasiTcpServer(8002);
  81. for (var i = 0; ; i++) {
  82. let cs = await s.accept();
  83. handle_client(cs);
  84. }
  85. } catch (e) {
  86. print(e)
  87. }
  88. }
  89. server_start();

The rollup.config.js and package.json files are to build the React 18 SSR dependencies and components into a bundled JavaScript file for WasmEdge. You should use the npm command to build it. The output is in the dist/main.mjs file.

  1. npm install
  2. npm run build

To run the example, do the following on the CLI to start the server.

  1. cd example_js/react_ssr_stream
  2. nohup wasmedge --dir .:. ../../target/wasm32-wasi/release/wasmedge_quickjs.wasm dist/main.mjs &

Send the server a HTTP request via curl or the browser.

  1. curl http://localhost:8002

The results are as follows. The service first returns an HTML page with an empty inner section (i.e., the loading section), and then 2s later, the HTML content for the inner section and the JavaScript to display it.

  1. % Total % Received % Xferd Average Speed Time Time Time Current
  2. Dload Upload Total Spent Left Speed
  3. 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
  4. 100 439 0 439 0 0 1202 0 --:--:-- --:--:-- --:--:-- 1199
  5. 100 2556 0 2556 0 0 1150 0 --:--:-- 0:00:02 --:--:-- 1150
  6. 100 2556 0 2556 0 0 926 0 --:--:-- 0:00:02 --:--:-- 926
  7. 100 2806 0 2806 0 0 984 0 --:--:-- 0:00:02 --:--:-- 984
  8. <!DOCTYPE html><html lang="en"><head><meta charSet="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="/main.css"/><title>Hello</title></head><body><noscript><b>Enable JavaScript to run this app.</b></noscript><!--$--><main><nav><a href="/">Home</a></nav><aside class="sidebar"><!--$?--><template id="B:0"></template><div class="spinner spinner--active" role="progressbar" aria-busy="true"></div><!--/$--></aside><article class="post"><!--$?--><template id="B:1"></template><div class="spinner spinner--active" role="progressbar" aria-busy="true"></div><!--/$--><section class="comments"><h2>Comments</h2><!--$?--><template id="B:2"></template><div class="spinner spinner--active" role="progressbar" aria-busy="true"></div><!--/$--></section><h2>Thanks for reading!</h2></article></main><!--/$--><script>assetManifest = {"main.js":"/main.js","main.css":"/main.css"};</script></body></html><div hidden id="S:0"><template id="P:3"></template></div><div hidden id="S:1"><template id="P:4"></template></div><div hidden id="S:2"><template id="P:5"></template></div><div hidden id="S:3"><h1>Archive</h1><ul><li>May 2021</li><li>April 2021</li><li>March 2021</li><li>February 2021</li><li>January 2021</li><li>December 2020</li><li>November 2020</li><li>October 2020</li><li>September 2020</li></ul></div><script>function $RS(a,b){a=document.getElementById(a);b=document.getElementById(b);for(a.parentNode.removeChild(a);a.firstChild;)b.parentNode.insertBefore(a.firstChild,b);b.parentNode.removeChild(b)};$RS("S:3","P:3")</script><script>function $RC(a,b){a=document.getElementById(a);b=document.getElementById(b);b.parentNode.removeChild(b);if(a){a=a.previousSibling;var f=a.parentNode,c=a.nextSibling,e=0;do{if(c&&8===c.nodeType){var d=c.data;if("/$"===d)if(0===e)break;else e--;else"$"!==d&&"$?"!==d&&"$!"!==d||e++}d=c.nextSibling;f.removeChild(c);c=d}while(c);for(;b.firstChild;)f.insertBefore(b.firstChild,c);a.data="$";a._reactRetry&&a._reactRetry()}};$RC("B:0","S:0")</script><div hidden id="S:4"><h1>Hello world</h1><p>This demo is <!-- --><b>artificially slowed down</b>. Open<!-- --> <!-- --><code>server/delays.js</code> to adjust how much different things are slowed down.<!-- --></p><p>Notice how HTML for comments &quot;streams in&quot; before the JS (or React) has loaded on the page.</p><p>Also notice that the JS for comments and sidebar has been code-split, but HTML for it is still included in the server output.</p></div><script>$RS("S:4","P:4")</script><script>$RC("B:1","S:1")</script><div hidden id="S:5"><p class="comment">Wait, it doesn&#x27;t wait for React to load?</p><p class="comment">How does this even work?</p><p class="comment">I like marshmallows</p></div><script>$RS("S:5","P:5")</script><script>$RC("B:2","S:2")</script>

The streaming SSR examples make use of WasmEdge’s unique asynchronous networking capabilities and ES6 module support (i.e., the rollup bundled JS file contains ES6 modules). You can learn more about async networking and ES6 in this book.