-
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.
[Flight] Progressively Enhanced Server Actions (#26774)
This automatically exposes `$$FORM_ACTIONS` on Server References coming from Flight. So that when they're used in a form action, we can encode the ID for the server reference as a hidden field or as part of the name of a button. If the Server Action is a bound function it can have complex data associated with it. In this case this additional data is encoded as additional form fields. To process a POST on the server there's now a `decodeAction` helper that can take one of these progressive posts from FormData and give you a function that is prebound with the correct closure and FormData so that you can just invoke it. I updated the fixture which now has a "Server State" that gets automatically refreshed. This also lets us visualize form fields. There's no "Action State" here for showing error messages that are not thrown, that's still up to user space.
- Loading branch information
1 parent
c10010a
commit aef7ce5
Showing
18 changed files
with
589 additions
and
96 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} />); |
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
Oops, something went wrong.