-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixture with progressive enhancement in forms
This uses an in-memory Server State that gets mutated and then the RSC payload is refreshed after a mutation takes place.
- Loading branch information
1 parent
5a1b033
commit c99b29a
Showing
8 changed files
with
106 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
let serverState = 'Hello World'; | ||
|
||
export function setServerState(message) { | ||
serverState = message; | ||
} | ||
|
||
export function getServerState() { | ||
return serverState; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,44 @@ | ||
import * as React from 'react'; | ||
import {use, Suspense} from 'react'; | ||
import {use, Suspense, useState, startTransition} from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
import {createFromFetch, encodeReply} from 'react-server-dom-webpack/client'; | ||
|
||
// TODO: This should be a dependency of the App but we haven't implemented CSS in Node yet. | ||
import './style.css'; | ||
|
||
let updateRoot; | ||
async function callServer(id, args) { | ||
const response = fetch('/', { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'text/x-component', | ||
'rsc-action': id, | ||
}, | ||
body: await encodeReply(args), | ||
}); | ||
const {returnValue, root} = await createFromFetch(response, {callServer}); | ||
// Refresh the tree with the new RSC payload. | ||
startTransition(() => { | ||
updateRoot(root); | ||
}); | ||
return returnValue; | ||
} | ||
|
||
let data = createFromFetch( | ||
fetch('/', { | ||
headers: { | ||
Accept: 'text/x-component', | ||
}, | ||
}), | ||
{ | ||
async callServer(id, args) { | ||
const response = fetch('/', { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'text/x-component', | ||
'rsc-action': id, | ||
}, | ||
body: await encodeReply(args), | ||
}); | ||
return createFromFetch(response); | ||
}, | ||
callServer, | ||
} | ||
); | ||
|
||
function Shell({data}) { | ||
return use(data); | ||
const [root, setRoot] = useState(use(data)); | ||
updateRoot = setRoot; | ||
return root; | ||
} | ||
|
||
ReactDOM.hydrateRoot(document, <Shell data={data} />); |