@@ -2337,7 +2337,7 @@ function preinitStyle(
23372337 getStylesheetSelectorFromKey ( key ) ,
23382338 ) ;
23392339 if ( instance ) {
2340- state . loading = Loaded ;
2340+ state . loading = Loaded & Inserted ;
23412341 } else {
23422342 // Construct a new instance and insert it
23432343 const stylesheetProps = Object . assign (
@@ -2769,6 +2769,7 @@ export function acquireResource(
27692769 getStylesheetSelectorFromKey ( key ) ,
27702770 ) ;
27712771 if ( instance ) {
2772+ resource . state . loading |= Inserted ;
27722773 resource . instance = instance ;
27732774 markNodeAsHoistable ( instance ) ;
27742775 return instance ;
@@ -3360,71 +3361,73 @@ export function suspendResource(
33603361 return ;
33613362 }
33623363 }
3363- if ( resource . instance === null ) {
3364- const qualifiedProps : StylesheetQualifyingProps = props ;
3365- const key = getStyleKey ( qualifiedProps . href ) ;
3364+ if ( ( resource . state . loading & Inserted ) === NotLoaded ) {
3365+ if ( resource . instance === null ) {
3366+ const qualifiedProps : StylesheetQualifyingProps = props ;
3367+ const key = getStyleKey ( qualifiedProps . href ) ;
33663368
3367- // Attempt to hydrate instance from DOM
3368- let instance : null | Instance = hoistableRoot . querySelector (
3369- getStylesheetSelectorFromKey ( key ) ,
3370- ) ;
3371- if ( instance ) {
3372- // If this instance has a loading state it came from the Fizz runtime.
3373- // If there is not loading state it is assumed to have been server rendered
3374- // as part of the preamble and therefore synchronously loaded. It could have
3375- // errored however which we still do not yet have a means to detect. For now
3376- // we assume it is loaded.
3377- const maybeLoadingState : ?Promise < mixed > = ( instance : any ) . _p ;
3378- if (
3379- maybeLoadingState !== null &&
3380- typeof maybeLoadingState === 'object ' &&
3381- // $FlowFixMe[method-unbinding]
3382- typeof maybeLoadingState . then === 'function '
3383- ) {
3384- const loadingState = maybeLoadingState ;
3385- state . count ++ ;
3386- const ping = onUnsuspend . bind ( state ) ;
3387- loadingState . then ( ping , ping ) ;
3369+ // Attempt to hydrate instance from DOM
3370+ let instance : null | Instance = hoistableRoot . querySelector (
3371+ getStylesheetSelectorFromKey ( key ) ,
3372+ ) ;
3373+ if ( instance ) {
3374+ // If this instance has a loading state it came from the Fizz runtime.
3375+ // If there is not loading state it is assumed to have been server rendered
3376+ // as part of the preamble and therefore synchronously loaded. It could have
3377+ // errored however which we still do not yet have a means to detect. For now
3378+ // we assume it is loaded.
3379+ const maybeLoadingState : ?Promise < mixed > = ( instance : any ) . _p ;
3380+ if (
3381+ maybeLoadingState !== null &&
3382+ typeof maybeLoadingState === 'object ' &&
3383+ // $FlowFixMe[method-unbinding]
3384+ typeof maybeLoadingState . then === 'function '
3385+ ) {
3386+ const loadingState = maybeLoadingState ;
3387+ state . count ++ ;
3388+ const ping = onUnsuspend . bind ( state ) ;
3389+ loadingState . then ( ping , ping ) ;
3390+ }
3391+ resource . state . loading |= Inserted ;
3392+ resource . instance = instance ;
3393+ markNodeAsHoistable ( instance ) ;
3394+ return ;
33883395 }
3389- resource . state . loading |= Inserted ;
3390- resource . instance = instance ;
3391- markNodeAsHoistable ( instance ) ;
3392- return ;
3393- }
33943396
3395- const ownerDocument = getDocumentFromRoot ( hoistableRoot ) ;
3396-
3397- const stylesheetProps = stylesheetPropsFromRawProps ( props ) ;
3398- const preloadProps = preloadPropsMap . get ( key ) ;
3399- if ( preloadProps ) {
3400- adoptPreloadPropsForStylesheet ( stylesheetProps , preloadProps ) ;
3401- }
3397+ const ownerDocument = getDocumentFromRoot ( hoistableRoot ) ;
34023398
3403- // Construct and insert a new instance
3404- instance = ownerDocument . createElement ( 'link ') ;
3405- markNodeAsHoistable ( instance ) ;
3406- const linkInstance : HTMLLinkElement = ( instance : any ) ;
3407- // This Promise is a loading state used by the Fizz runtime. We need this incase there is a race
3408- // between this resource being rendered on the client and being rendered with a late completed boundary.
3409- ( linkInstance : any ) . _p = new Promise ( ( resolve , reject ) => {
3410- linkInstance . onload = resolve ;
3411- linkInstance . onerror = reject ;
3412- } ) ;
3413- setInitialProperties ( instance , 'link ', stylesheetProps ) ;
3414- resource . instance = instance ;
3415- }
3399+ const stylesheetProps = stylesheetPropsFromRawProps ( props ) ;
3400+ const preloadProps = preloadPropsMap . get ( key ) ;
3401+ if ( preloadProps ) {
3402+ adoptPreloadPropsForStylesheet ( stylesheetProps , preloadProps ) ;
3403+ }
34163404
3417- if ( state . stylesheets === null ) {
3418- state . stylesheets = new Map ( ) ;
3419- }
3420- state . stylesheets . set ( resource , hoistableRoot ) ;
3405+ // Construct and insert a new instance
3406+ instance = ownerDocument . createElement ( 'link ') ;
3407+ markNodeAsHoistable ( instance ) ;
3408+ const linkInstance : HTMLLinkElement = ( instance : any ) ;
3409+ // This Promise is a loading state used by the Fizz runtime. We need this incase there is a race
3410+ // between this resource being rendered on the client and being rendered with a late completed boundary.
3411+ ( linkInstance : any ) . _p = new Promise ( ( resolve , reject ) => {
3412+ linkInstance . onload = resolve ;
3413+ linkInstance . onerror = reject ;
3414+ } ) ;
3415+ setInitialProperties ( instance , 'link ', stylesheetProps ) ;
3416+ resource . instance = instance ;
3417+ }
34213418
3422- const preloadEl = resource . state . preload ;
3423- if ( preloadEl && ( resource . state . loading & Settled ) === NotLoaded ) {
3424- state . count ++ ;
3425- const ping = onUnsuspend . bind ( state ) ;
3426- preloadEl . addEventListener ( 'load ', ping ) ;
3427- preloadEl . addEventListener ( 'error ', ping ) ;
3419+ if ( state . stylesheets === null ) {
3420+ state . stylesheets = new Map ( ) ;
3421+ }
3422+ state . stylesheets . set ( resource , hoistableRoot ) ;
3423+
3424+ const preloadEl = resource . state . preload ;
3425+ if ( preloadEl && ( resource . state . loading & Settled ) === NotLoaded ) {
3426+ state . count ++ ;
3427+ const ping = onUnsuspend . bind ( state ) ;
3428+ preloadEl . addEventListener ( 'load ', ping ) ;
3429+ preloadEl . addEventListener ( 'error ', ping ) ;
3430+ }
34283431 }
34293432 }
34303433}
0 commit comments