main
js 57 lines 1.2 KB
Raw
1 import * as React from 'react';
2 import {use, Suspense, useState, startTransition} from 'react';
3 import ReactDOM from 'react-dom/client';
4 import {createFromFetch, encodeReply} from 'react-server-dom-esm/client';
5
6 const moduleBaseURL = '/src/';
7
8 function findSourceMapURL(fileName) {
9 return (
10 document.location.origin +
11 '/source-maps?name=' +
12 encodeURIComponent(fileName)
13 );
14 }
15
16 let updateRoot;
17 async function callServer(id, args) {
18 const response = fetch('/', {
19 method: 'POST',
20 headers: {
21 Accept: 'text/x-component',
22 'rsc-action': id,
23 },
24 body: await encodeReply(args),
25 });
26 const {returnValue, root} = await createFromFetch(response, {
27 callServer,
28 moduleBaseURL,
29 findSourceMapURL,
30 });
31 // Refresh the tree with the new RSC payload.
32 startTransition(() => {
33 updateRoot(root);
34 });
35 return returnValue;
36 }
37
38 let data = createFromFetch(
39 fetch('/', {
40 headers: {
41 Accept: 'text/x-component',
42 },
43 }),
44 {
45 callServer,
46 moduleBaseURL,
47 findSourceMapURL,
48 }
49 );
50
51 function Shell({data}) {
52 const [root, setRoot] = useState(use(data));
53 updateRoot = setRoot;
54 return root;
55 }
56
57 ReactDOM.hydrateRoot(document, React.createElement(Shell, {data}));