From f3a14951ab9bccfd59ca977493b72321b24e50a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Markb=C3=A5ge?= Date: Mon, 11 Feb 2019 21:25:44 -0800 Subject: [PATCH] Partial Hydration (#14717) * Basic partial hydration test * Render comments around Suspense components We need this to be able to identify how far to skip ahead if we're not going to hydrate this subtree yet. * Add DehydratedSuspenseComponent type of work Will be used for Suspense boundaries that are left with their server rendered content intact. * Add comment node as hydratable instance type as placeholder for suspense * Skip past nodes within the Suspense boundary This lets us continue hydrating sibling nodes. * A dehydrated suspense boundary comment should be considered a sibling * Retry hydrating at offscreen pri or after ping if suspended * Enter hydration state when retrying dehydrated suspense boundary * Delete all children within a dehydrated suspense boundary when it's deleted * Delete server rendered content when props change before hydration completes * Make test internal * Wrap in act * Change SSR Fixture to use Partial Hydration This requires the enableSuspenseServerRenderer flag to be manually enabled for the build to work. * Changes to any parent Context forces clearing dehydrated content We mark dehydrated boundaries as having child work, since they might have components that read from the changed context. We check this in beginWork and if it does we treat it as if the input has changed (same as if props changes). * Wrap in feature flag * Treat Suspense boundaries without fallbacks as if not-boundaries These don't come into play for purposes of hydration. * Fix clearing of nested suspense boundaries * ping -> retry Co-Authored-By: sebmarkbage * Typo Co-Authored-By: sebmarkbage * Use didReceiveUpdate instead of manually comparing props * Leave comment for why it's ok to ignore the timeout --- fixtures/ssr/src/components/App.js | 39 +- fixtures/ssr/src/components/Chrome.css | 24 + fixtures/ssr/src/components/Chrome.js | 12 +- fixtures/ssr/src/components/Page.css | 15 +- fixtures/ssr/src/components/Page.js | 20 +- fixtures/ssr/src/components/Page2.js | 15 + fixtures/ssr/src/components/Suspend.js | 21 + fixtures/ssr/src/components/Theme.js | 25 + fixtures/ssr/src/index.js | 5 +- ...DOMServerPartialHydration-test.internal.js | 637 ++++++++++++++++++ .../ReactDOMServerSuspense-test.internal.js | 43 +- .../src/client/ReactDOMHostConfig.js | 142 +++- .../src/server/ReactPartialRenderer.js | 28 +- .../src/ReactDebugFiberPerf.js | 4 +- .../src/ReactFiberBeginWork.js | 122 +++- .../src/ReactFiberCommitWork.js | 37 +- .../src/ReactFiberCompleteWork.js | 26 + .../src/ReactFiberHydrationContext.js | 74 +- .../src/ReactFiberNewContext.js | 88 ++- .../src/ReactFiberScheduler.js | 24 +- .../src/ReactFiberUnwindWork.js | 126 +++- .../src/forks/ReactFiberHostConfig.custom.js | 12 + packages/shared/HostConfigWithNoHydration.js | 7 + packages/shared/ReactWorkTags.js | 1 + 24 files changed, 1417 insertions(+), 130 deletions(-) create mode 100644 fixtures/ssr/src/components/Page2.js create mode 100644 fixtures/ssr/src/components/Suspend.js create mode 100644 fixtures/ssr/src/components/Theme.js create mode 100644 packages/react-dom/src/__tests__/ReactDOMServerPartialHydration-test.internal.js diff --git a/fixtures/ssr/src/components/App.js b/fixtures/ssr/src/components/App.js index da63dd4fd9..ebfafc9d15 100644 --- a/fixtures/ssr/src/components/App.js +++ b/fixtures/ssr/src/components/App.js @@ -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 ( - -
-

Hello World

- -
-
- ); - } +function LoadingIndicator() { + let theme = useContext(Theme); + return
Loading...
; +} + +export default function App({assets}) { + let [CurrentPage, switchPage] = useState(() => Page); + return ( + +
+

Hello World

+ switchPage(() => Page)}> + Page 1 + + {' | '} + switchPage(() => Page2)}> + Page 2 + + }> + + +
+
+ ); } diff --git a/fixtures/ssr/src/components/Chrome.css b/fixtures/ssr/src/components/Chrome.css index b019b57b1d..96932a6938 100644 --- a/fixtures/ssr/src/components/Chrome.css +++ b/fixtures/ssr/src/components/Chrome.css @@ -3,3 +3,27 @@ body { padding: 0; font-family: sans-serif; } + +body.light { + background-color: #FFFFFF; + color: #333333; +} + +body.dark { + background-color: #000000; + color: #CCCCCC; +} + +.light-loading { + margin: 10px 0; + padding: 10px; + background-color: #CCCCCC; + color: #666666; +} + +.dark-loading { + margin: 10px 0; + padding: 10px; + background-color: #333333; + color: #999999; +} diff --git a/fixtures/ssr/src/components/Chrome.js b/fixtures/ssr/src/components/Chrome.js index 541d96901c..b52c8d0ee7 100644 --- a/fixtures/ssr/src/components/Chrome.js +++ b/fixtures/ssr/src/components/Chrome.js @@ -1,8 +1,11 @@ import React, {Component} from 'react'; +import Theme, {ThemeToggleButton} from './Theme'; + import './Chrome.css'; export default class Chrome extends Component { + state = {theme: 'light'}; render() { const assets = this.props.assets; return ( @@ -14,13 +17,18 @@ export default class Chrome extends Component { {this.props.title} - +