-
Notifications
You must be signed in to change notification settings - Fork 47.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change SSR Fixture to use Partial Hydration
This requires the enableSuspenseServerRenderer flag to be manually enabled for the build to work.
- Loading branch information
1 parent
e144a5e
commit eb3ea2d
Showing
9 changed files
with
153 additions
and
23 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,17 +1,32 @@ | ||
import React, {Component} from 'react'; | ||
import React, {useContext, useState, Suspense} from 'react'; | ||
|
||
import Chrome from './Chrome'; | ||
import Page from './Page'; | ||
import Page2 from './Page2'; | ||
import Theme from './Theme'; | ||
|
||
export default class App extends Component { | ||
render() { | ||
return ( | ||
<Chrome title="Hello World" assets={this.props.assets}> | ||
<div> | ||
<h1>Hello World</h1> | ||
<Page /> | ||
</div> | ||
</Chrome> | ||
); | ||
} | ||
function LoadingIndicator() { | ||
let theme = useContext(Theme); | ||
return <div className={theme + '-loading'}>Loading...</div>; | ||
} | ||
|
||
export default function App({assets}) { | ||
let [CurrentPage, switchPage] = useState(() => Page); | ||
return ( | ||
<Chrome title="Hello World" assets={assets}> | ||
<div> | ||
<h1>Hello World</h1> | ||
<a className="link" onClick={() => switchPage(() => Page)}> | ||
Page 1 | ||
</a> | ||
{' | '} | ||
<a className="link" onClick={() => switchPage(() => Page2)}> | ||
Page 2 | ||
</a> | ||
<Suspense fallback={<LoadingIndicator />}> | ||
<CurrentPage /> | ||
</Suspense> | ||
</div> | ||
</Chrome> | ||
); | ||
} |
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,3 +1,16 @@ | ||
.bold { | ||
.link { | ||
font-weight: bold; | ||
cursor: pointer; | ||
} | ||
.light-box { | ||
margin: 10px 0; | ||
padding: 10px; | ||
background-color: #CCCCCC; | ||
color: #333333; | ||
} | ||
.dark-box { | ||
margin: 10px 0; | ||
padding: 10px; | ||
background-color: #333333; | ||
color: #CCCCCC; | ||
} |
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,15 @@ | ||
import React, {useContext} from 'react'; | ||
|
||
import Theme from './Theme'; | ||
import Suspend from './Suspend'; | ||
|
||
import './Page.css'; | ||
|
||
export default function Page2() { | ||
let theme = useContext(Theme); | ||
return ( | ||
<div className={theme + '-box'}> | ||
<Suspend>Content of a different page</Suspend> | ||
</div> | ||
); | ||
} |
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,21 @@ | ||
let promise = null; | ||
let isResolved = false; | ||
|
||
export default function Suspend({children}) { | ||
// This will suspend the content from rendering but only on the client. | ||
// This is used to demo a slow loading app. | ||
if (typeof window === 'object') { | ||
if (!isResolved) { | ||
if (promise === null) { | ||
promise = new Promise(resolve => { | ||
setTimeout(() => { | ||
isResolved = true; | ||
resolve(); | ||
}, 6000); | ||
}); | ||
} | ||
throw promise; | ||
} | ||
} | ||
return children; | ||
} |
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,25 @@ | ||
import React, {createContext, useContext, useState} from 'react'; | ||
|
||
const Theme = createContext('light'); | ||
|
||
export default Theme; | ||
|
||
export function ThemeToggleButton({onChange}) { | ||
let theme = useContext(Theme); | ||
let [targetTheme, setTargetTheme] = useState(theme); | ||
function toggleTheme() { | ||
let newTheme = theme === 'light' ? 'dark' : 'light'; | ||
// High pri, responsive update. | ||
setTargetTheme(newTheme); | ||
// Perform the actual theme change in a separate update. | ||
setTimeout(() => onChange(newTheme), 0); | ||
} | ||
if (targetTheme !== theme) { | ||
return 'Switching to ' + targetTheme + '...'; | ||
} | ||
return ( | ||
<a className="link" onClick={toggleTheme}> | ||
Switch to {theme === 'light' ? 'Dark' : 'Light'} theme | ||
</a> | ||
); | ||
} |
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,6 +1,7 @@ | ||
import React from 'react'; | ||
import {hydrate} from 'react-dom'; | ||
import {unstable_createRoot} from 'react-dom'; | ||
|
||
import App from './components/App'; | ||
|
||
hydrate(<App assets={window.assetManifest} />, document); | ||
let root = unstable_createRoot(document, {hydrate: true}); | ||
root.render(<App assets={window.assetManifest} />); |