@@ -1788,7 +1788,7 @@ type StyleTagResource = TResource<'style', null>;
1788
1788
type StyleResource = StyleTagResource | StylesheetResource ;
1789
1789
type ScriptResource = TResource < 'script' , null > ;
1790
1790
type VoidResource = TResource < 'void' , null > ;
1791
- type Resource = StyleResource | ScriptResource | VoidResource ;
1791
+ export type Resource = StyleResource | ScriptResource | VoidResource ;
1792
1792
1793
1793
type LoadingState = number ;
1794
1794
const NotLoaded = /* */ 0b000 ;
@@ -2170,6 +2170,7 @@ function preinit(href: string, options: PreinitOptions) {
2170
2170
state . loading |= Errored ;
2171
2171
} ) ;
2172
2172
2173
+ state . loading |= Inserted ;
2173
2174
insertStylesheet ( instance , precedence , resourceRoot ) ;
2174
2175
}
2175
2176
@@ -2518,6 +2519,11 @@ export function acquireResource(
2518
2519
2519
2520
markNodeAsHoistable ( instance ) ;
2520
2521
setInitialProperties ( instance , 'style' , styleProps ) ;
2522
+
2523
+ // TODO: `style` does not have loading state for tracking insertions. I
2524
+ // guess because these aren't suspensey? Not sure whether this is a
2525
+ // factoring smell.
2526
+ // resource.state.loading |= Inserted;
2521
2527
insertStylesheet ( instance , qualifiedProps . precedence , hoistableRoot ) ;
2522
2528
resource . instance = instance ;
2523
2529
@@ -2556,6 +2562,7 @@ export function acquireResource(
2556
2562
linkInstance . onerror = reject ;
2557
2563
} ) ;
2558
2564
setInitialProperties ( instance , 'link' , stylesheetProps ) ;
2565
+ resource . state . loading |= Inserted ;
2559
2566
insertStylesheet ( instance , qualifiedProps . precedence , hoistableRoot ) ;
2560
2567
resource . instance = instance ;
2561
2568
@@ -2604,6 +2611,28 @@ export function acquireResource(
2604
2611
);
2605
2612
}
2606
2613
}
2614
+ } else {
2615
+ // In the case of stylesheets, they might have already been assigned an
2616
+ // instance during ` suspendResource `. But that doesn't mean they were
2617
+ // inserted, because the commit might have been interrupted. So we need to
2618
+ // check now.
2619
+ //
2620
+ // The other resource types are unaffected because they are not
2621
+ // yet suspensey.
2622
+ //
2623
+ // TODO: This is a bit of a code smell. Consider refactoring how
2624
+ // ` suspendResource ` and ` acquireResource ` work together. The idea is that
2625
+ // ` suspendResource ` does all the same stuff as ` acquireResource ` except
2626
+ // for the insertion.
2627
+ if (
2628
+ resource.type === 'stylesheet' &&
2629
+ (resource.state.loading & Inserted) === NotLoaded
2630
+ ) {
2631
+ const qualifiedProps: StylesheetQualifyingProps = props;
2632
+ const instance: Instance = resource.instance;
2633
+ resource.state.loading |= Inserted;
2634
+ insertStylesheet(instance, qualifiedProps.precedence, hoistableRoot);
2635
+ }
2607
2636
}
2608
2637
return resource.instance;
2609
2638
}
@@ -2613,7 +2642,7 @@ export function releaseResource(resource: Resource): void {
2613
2642
}
2614
2643
2615
2644
function insertStylesheet(
2616
- instance: HTMLElement ,
2645
+ instance: Element ,
2617
2646
precedence: string,
2618
2647
root: HoistableRoot,
2619
2648
): void {
0 commit comments