From a9985529f1aa55477f0feafe2398d36707cf6108 Mon Sep 17 00:00:00 2001 From: Josh Story Date: Wed, 25 Oct 2023 11:51:01 -0700 Subject: [PATCH] [Fizz] Do not reinsert stylesheets after initial insert (#27586) The loading state tracking for suspensey CSS is too complicated. Prior to this change it had a state it could enter into where a stylesheet was already in the DOM but the loading state did not know it was inserted causing a later transition to try to insert it again. This fix is to add proper tracking of insertions on the codepaths that were missing it. It also modifies the logic of when to suspend based on whether the stylesheet has already been inserted or not. This is not 100% correct semantics however because a prior commit could have inserted a stylesheet and a later transition should ideally be able to wait on that load before committing. I haven't attempted to fix this yet however because the loading state tracking is too complicated as it is and requires a more thorough refactor. Additionally it's not particularly valuable to delay a transition on a loading stylesheet when a previous commit also relied on that stylesheet but didn't wait for it b/c it was sync. I will follow up with an improvement PR later fixes: https://github.com/facebook/react/issues/27585 --- .../src/client/ReactFiberConfigDOM.js | 123 +++++++++--------- .../src/__tests__/ReactDOMFloat-test.js | 78 +++++++++++ 2 files changed, 141 insertions(+), 60 deletions(-) diff --git a/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js b/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js index 519484f27222e..8d19040973443 100644 --- a/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js +++ b/packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js @@ -2337,7 +2337,7 @@ function preinitStyle( getStylesheetSelectorFromKey(key), ); if (instance) { - state.loading = Loaded; + state.loading = Loaded & Inserted; } else { // Construct a new instance and insert it const stylesheetProps = Object.assign( @@ -2769,6 +2769,7 @@ export function acquireResource( getStylesheetSelectorFromKey(key), ); if (instance) { + resource.state.loading |= Inserted; resource.instance = instance; markNodeAsHoistable(instance); return instance; @@ -3360,71 +3361,73 @@ export function suspendResource( return; } } - if (resource.instance === null) { - const qualifiedProps: StylesheetQualifyingProps = props; - const key = getStyleKey(qualifiedProps.href); + if ((resource.state.loading & Inserted) === NotLoaded) { + if (resource.instance === null) { + const qualifiedProps: StylesheetQualifyingProps = props; + const key = getStyleKey(qualifiedProps.href); - // Attempt to hydrate instance from DOM - let instance: null | Instance = hoistableRoot.querySelector( - getStylesheetSelectorFromKey(key), - ); - if (instance) { - // If this instance has a loading state it came from the Fizz runtime. - // If there is not loading state it is assumed to have been server rendered - // as part of the preamble and therefore synchronously loaded. It could have - // errored however which we still do not yet have a means to detect. For now - // we assume it is loaded. - const maybeLoadingState: ?Promise = (instance: any)._p; - if ( - maybeLoadingState !== null && - typeof maybeLoadingState === 'object' && - // $FlowFixMe[method-unbinding] - typeof maybeLoadingState.then === 'function' - ) { - const loadingState = maybeLoadingState; - state.count++; - const ping = onUnsuspend.bind(state); - loadingState.then(ping, ping); + // Attempt to hydrate instance from DOM + let instance: null | Instance = hoistableRoot.querySelector( + getStylesheetSelectorFromKey(key), + ); + if (instance) { + // If this instance has a loading state it came from the Fizz runtime. + // If there is not loading state it is assumed to have been server rendered + // as part of the preamble and therefore synchronously loaded. It could have + // errored however which we still do not yet have a means to detect. For now + // we assume it is loaded. + const maybeLoadingState: ?Promise = (instance: any)._p; + if ( + maybeLoadingState !== null && + typeof maybeLoadingState === 'object' && + // $FlowFixMe[method-unbinding] + typeof maybeLoadingState.then === 'function' + ) { + const loadingState = maybeLoadingState; + state.count++; + const ping = onUnsuspend.bind(state); + loadingState.then(ping, ping); + } + resource.state.loading |= Inserted; + resource.instance = instance; + markNodeAsHoistable(instance); + return; } - resource.state.loading |= Inserted; - resource.instance = instance; - markNodeAsHoistable(instance); - return; - } - const ownerDocument = getDocumentFromRoot(hoistableRoot); - - const stylesheetProps = stylesheetPropsFromRawProps(props); - const preloadProps = preloadPropsMap.get(key); - if (preloadProps) { - adoptPreloadPropsForStylesheet(stylesheetProps, preloadProps); - } + const ownerDocument = getDocumentFromRoot(hoistableRoot); - // Construct and insert a new instance - instance = ownerDocument.createElement('link'); - markNodeAsHoistable(instance); - const linkInstance: HTMLLinkElement = (instance: any); - // This Promise is a loading state used by the Fizz runtime. We need this incase there is a race - // between this resource being rendered on the client and being rendered with a late completed boundary. - (linkInstance: any)._p = new Promise((resolve, reject) => { - linkInstance.onload = resolve; - linkInstance.onerror = reject; - }); - setInitialProperties(instance, 'link', stylesheetProps); - resource.instance = instance; - } + const stylesheetProps = stylesheetPropsFromRawProps(props); + const preloadProps = preloadPropsMap.get(key); + if (preloadProps) { + adoptPreloadPropsForStylesheet(stylesheetProps, preloadProps); + } - if (state.stylesheets === null) { - state.stylesheets = new Map(); - } - state.stylesheets.set(resource, hoistableRoot); + // Construct and insert a new instance + instance = ownerDocument.createElement('link'); + markNodeAsHoistable(instance); + const linkInstance: HTMLLinkElement = (instance: any); + // This Promise is a loading state used by the Fizz runtime. We need this incase there is a race + // between this resource being rendered on the client and being rendered with a late completed boundary. + (linkInstance: any)._p = new Promise((resolve, reject) => { + linkInstance.onload = resolve; + linkInstance.onerror = reject; + }); + setInitialProperties(instance, 'link', stylesheetProps); + resource.instance = instance; + } - const preloadEl = resource.state.preload; - if (preloadEl && (resource.state.loading & Settled) === NotLoaded) { - state.count++; - const ping = onUnsuspend.bind(state); - preloadEl.addEventListener('load', ping); - preloadEl.addEventListener('error', ping); + if (state.stylesheets === null) { + state.stylesheets = new Map(); + } + state.stylesheets.set(resource, hoistableRoot); + + const preloadEl = resource.state.preload; + if (preloadEl && (resource.state.loading & Settled) === NotLoaded) { + state.count++; + const ping = onUnsuspend.bind(state); + preloadEl.addEventListener('load', ping); + preloadEl.addEventListener('error', ping); + } } } } diff --git a/packages/react-dom/src/__tests__/ReactDOMFloat-test.js b/packages/react-dom/src/__tests__/ReactDOMFloat-test.js index 8fc6ef45ab2e2..622722a947d7d 100644 --- a/packages/react-dom/src/__tests__/ReactDOMFloat-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMFloat-test.js @@ -2852,6 +2852,84 @@ body { ]); }); + // https://github.com/facebook/react/issues/27585 + it('does not reinsert already inserted stylesheets during a delayed commit', async () => { + await act(() => { + renderToPipeableStream( + + + + + server + + , + ).pipe(writable); + }); + + expect(getMeaningfulChildren(document)).toEqual( + + + + + + server + , + ); + + const root = ReactDOMClient.createRoot(document.body); + expect(getMeaningfulChildren(container)).toBe(undefined); + root.render( + <> + + +
client
+ , + ); + await waitForAll([]); + expect(getMeaningfulChildren(document)).toEqual( + + + + + + + + +
client
+ + , + ); + + // In a transition we add another reference to an already loaded resource + // https://github.com/facebook/react/issues/27585 + React.startTransition(() => { + root.render( + <> + + +
client
+ + , + ); + }); + await waitForAll([]); + // In https://github.com/facebook/react/issues/27585 the order updated + // to second, third, first + expect(getMeaningfulChildren(document)).toEqual( + + + + + + + + +
client
+ + , + ); + }); + xit('can delay commit until css resources error', async () => { // TODO: This test fails and crashes jest. need to figure out why before unskipping. const root = ReactDOMClient.createRoot(container);