-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This enables forms to be submitted even before hydration. While handling an MPA-style POST request, we can parse the hidden form fields that contain meta data about the form action, using `decodeAction` and `decodeFormState`, to call the action, and send the result as form state, along with the rendered root, in the RSC response. The form state is used during server-side rendering (passed into `renderToReadableStream`) to render the result as part of the initial HTML, as well as in the client during hydration (passed into `hydrateRoot`) to avoid hydration mismatches. BREAKING CHANGE: The Webpack RSC server plugin now emits the server manifest in the structure that React expects.
- Loading branch information
1 parent
46b3ba8
commit 4f4cf84
Showing
24 changed files
with
307 additions
and
175 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,5 @@ | ||
import {Router} from '@mfng/core/client/browser'; | ||
import * as React from 'react'; | ||
import ReactDOMClient from 'react-dom/client'; | ||
import {hydrateApp} from '@mfng/core/client'; | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import 'tailwindcss/tailwind.css'; | ||
|
||
React.startTransition(() => { | ||
ReactDOMClient.hydrateRoot( | ||
document, | ||
<React.StrictMode> | ||
<Router /> | ||
</React.StrictMode>, | ||
); | ||
}); | ||
hydrateApp().catch(console.error); |
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 |
---|---|---|
@@ -1,19 +1,5 @@ | ||
import {Router} from '@mfng/core/client/browser'; | ||
import {Analytics} from '@vercel/analytics/react'; | ||
import * as React from 'react'; | ||
import ReactDOMClient from 'react-dom/client'; | ||
import {hydrateApp} from '@mfng/core/client'; | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import 'tailwindcss/tailwind.css'; | ||
import {reportWebVitals} from './vitals.js'; | ||
|
||
React.startTransition(() => { | ||
ReactDOMClient.hydrateRoot( | ||
document, | ||
<React.StrictMode> | ||
<Router /> | ||
<Analytics /> | ||
</React.StrictMode>, | ||
); | ||
|
||
reportWebVitals(); | ||
}); | ||
hydrateApp().catch(console.error); |
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 was deleted.
Oops, something went wrong.
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,47 @@ | ||
import * as React from 'react'; | ||
import type {ReactFormState} from 'react-dom/client'; | ||
import ReactDOMClient from 'react-dom/client'; | ||
import ReactServerDOMClient from 'react-server-dom-webpack/client.browser'; | ||
import {callServer} from './call-server.js'; | ||
import {createUrlPath} from './router-location-utils.js'; | ||
import {Router} from './router.js'; | ||
|
||
export interface RscAppResult { | ||
readonly root: React.ReactElement; | ||
readonly formState?: ReactFormState; | ||
} | ||
|
||
export async function hydrateApp(): Promise<void> { | ||
const {root: initialRoot, formState} = | ||
await ReactServerDOMClient.createFromReadableStream<RscAppResult>( | ||
self.initialRscResponseStream, | ||
{callServer}, | ||
); | ||
|
||
const initialUrlPath = createUrlPath(document.location); | ||
|
||
const fetchRoot = React.cache(async function fetchRoot( | ||
urlPath: string, | ||
): Promise<React.ReactElement> { | ||
if (urlPath === initialUrlPath) { | ||
return initialRoot; | ||
} | ||
|
||
const {root} = await ReactServerDOMClient.createFromFetch<RscAppResult>( | ||
fetch(urlPath, {headers: {accept: `text/x-component`}}), | ||
{callServer}, | ||
); | ||
|
||
return root; | ||
}); | ||
|
||
React.startTransition(() => { | ||
ReactDOMClient.hydrateRoot( | ||
document, | ||
<React.StrictMode> | ||
<Router fetchRoot={fetchRoot} /> | ||
</React.StrictMode>, | ||
{formState}, | ||
); | ||
}); | ||
} |
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 +1,2 @@ | ||
export * from './hydrate-app.js'; | ||
export * from './use-router.js'; |
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,15 @@ | ||
import type {RouterLocation} from '../use-router-location.js'; | ||
|
||
export function createUrlPath(location: RouterLocation): string { | ||
const {pathname, search} = location; | ||
|
||
return `${pathname}${normalizeSearch(search)}`; | ||
} | ||
|
||
export function createUrl(location: RouterLocation): URL { | ||
return new URL(createUrlPath(location), document.location.origin); | ||
} | ||
|
||
function normalizeSearch(search: string): string { | ||
return `${search.replace(/(^[^?].*)/, `?$1`)}`; | ||
} |
Oops, something went wrong.