Commit d5736f0

Browse files
authored
[Fiber] track stylesheet preloads when explicitly preloaded (#36386)
Previously stylesheet resources would omit connecting with preloads inserted via `preload` which caused unecessary suspension of commits since the stylsheet resource would attempt to load the stylesheet again and delay the initial commit or commit a fallback (depending on whether the current screen should remain). This missing piece is that if you preload a stylesheet you must be able to use that sheets loading state when determining if the stylesheet is already loaded or not. This adds a pending indicator on client inserted prelaod links. We still assume SSR'd preloads are already loaded.
1 parent dd45307 commit d5736f0

3 files changed

Lines changed: 157 additions & 22 deletions

File tree

‎packages/react-dom-bindings/src/client/ReactDOMComponentTree.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const internalEventHandlesSetKey = '__reactHandles$' + randomKey;
5050
constinternalRootNodeResourcesKey='__reactResources$'+randomKey;
5151
constinternalHoistableMarker='__reactMarker$'+randomKey;
5252
constinternalScrollTimer='__reactScroll$'+randomKey;
53+
constinternalLoadPendingKey='__reactLoad$'+randomKey;
5354

5455
typeInstanceUnion=
5556
|Instance
@@ -386,6 +387,18 @@ export function clearScrollEndTimer(node: EventTarget): void {
386387
(node: any)[internalScrollTimer]=undefined;
387388
}
388389

390+
export function markNodeAsPendingLoad(node: Node): void {
391+
(node: any)[internalLoadPendingKey]=true;
392+
}
393+
394+
export function clearPendingLoadOnNode(node: Node): void {
395+
(node: any)[internalLoadPendingKey]=undefined;
396+
}
397+
398+
export function isNodePendingLoad(node: Node): boolean {
399+
return(node: any)[internalLoadPendingKey]===true;
400+
}
401+
389402
export function isOwnedInstance(node: Node): boolean {
390403
if(enableInternalInstanceMap){
391404
return!!(

‎packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js‎

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ import {
5656
getResourcesFromRoot,
5757
isMarkedHoistable,
5858
markNodeAsHoistable,
59+
markNodeAsPendingLoad,
60+
clearPendingLoadOnNode,
61+
isNodePendingLoad,
5962
isOwnedInstance,
6063
}from'./ReactDOMComponentTree';
6164
import{
@@ -5005,11 +5008,18 @@ function preload(href: string, as: string, options?: ?PreloadImplOptions) {
50055008
as==='script'&&
50065009
ownerDocument.querySelector(getScriptSelectorFromKey(key))
50075010
){
5008-
// We already have a stylesheet for this key. We don't need to preload it.
5011+
// We already have a script for this key. We don't need to preload it.
50095012
return;
50105013
}
50115014
constinstance=ownerDocument.createElement('link');
50125015
setInitialProperties(instance,'link',preloadProps);
5016+
if(as==='style'){
5017+
// Stash a loading state on the preload link. it will clean itself up once settled
5018+
markNodeAsPendingLoad(instance);
5019+
instance.onload=instance.onerror=()=>{
5020+
clearPendingLoadOnNode(instance);
5021+
};
5022+
}
50135023
markNodeAsHoistable(instance);
50145024
(ownerDocument.head: any).appendChild(instance);
50155025
}
@@ -5357,19 +5367,16 @@ export function getResource(
53575367
resource.instance=instance;
53585368
resource.state.loading=Loaded|Inserted;
53595369
}
5360-
}
5361-
5362-
if(!preloadPropsMap.has(key)){
5363-
constpreloadProps=preloadPropsFromStylesheet(qualifiedProps);
5364-
preloadPropsMap.set(key,preloadProps);
5365-
if(!instance){
5366-
preloadStylesheet(
5367-
ownerDocument,
5368-
key,
5369-
preloadProps,
5370-
resource.state,
5371-
);
5370+
}else{
5371+
// We don't have an instance we need to preload it instead
5372+
// $FlowFixMe[incompatible-type] -- the key we use here can only match non module preloads
5373+
letpreloadProps: void|PreloadProps=preloadPropsMap.get(key);
5374+
if(!preloadProps){
5375+
preloadProps=preloadPropsFromStylesheet(qualifiedProps);
5376+
preloadPropsMap.set(key,preloadProps);
53725377
}
5378+
5379+
preloadStylesheet(ownerDocument,key,preloadProps,resource.state);
53735380
}
53745381
}
53755382
if(currentProps&&currentResource===null){
@@ -5540,22 +5547,33 @@ function preloadStylesheet(
55405547
preloadProps: PreloadProps,
55415548
state: StylesheetState,
55425549
){
5543-
constpreloadEl=ownerDocument.querySelector(
5550+
letinstance=ownerDocument.querySelector(
55445551
getPreloadStylesheetSelectorFromKey(key),
55455552
);
5546-
if(preloadEl){
5547-
// If we find a preload already it was SSR'd and we won't have an actual
5548-
// loading state to track. For now we will just assume it is loaded
5549-
state.loading=Loaded;
5553+
if(instance){
5554+
if(!isNodePendingLoad(instance)){
5555+
// If we find a preload already it was SSR'd and we won't have an actual
5556+
// loading state to track. For now we will just assume it is loaded
5557+
state.loading=Loaded;
5558+
return;
5559+
}else{
5560+
// fall through and attach loading listeners
5561+
}
55505562
}else{
5551-
constinstance=ownerDocument.createElement('link');
5552-
state.preload=instance;
5553-
instance.addEventListener('load',()=>(state.loading|=Loaded));
5554-
instance.addEventListener('error',()=>(state.loading|=Errored));
5563+
instance=ownerDocument.createElement('link');
5564+
markNodeAsPendingLoad(instance);
5565+
instance.onload=instance.onerror=clearPendingLoadOnNode.bind(
5566+
null,
5567+
instance,
5568+
);
55555569
setInitialProperties(instance,'link',preloadProps);
55565570
markNodeAsHoistable(instance);
55575571
(ownerDocument.head: any).appendChild(instance);
55585572
}
5573+
// $FlowFixMe: [incompatible-type] -- if instance is an Element it will also be an HTMLLinkElement
5574+
state.preload=instance;
5575+
instance.addEventListener('load',()=>(state.loading|=Loaded));
5576+
instance.addEventListener('error',()=>(state.loading|=Errored));
55595577
}
55605578

55615579
functionpreloadPropsFromStylesheet(

‎packages/react-dom/src/__tests__/ReactDOMFloat-test.js‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3674,6 +3674,110 @@ body {
36743674
);
36753675
});
36763676

3677+
it('does not suspend a transition on a stylesheet whose preload has already loaded',async()=>{
3678+
constroot=ReactDOMClient.createRoot(document);
3679+
root.render(
3680+
<html>
3681+
<body>
3682+
<Suspensefallback="loading...">initial</Suspense>
3683+
</body>
3684+
</html>,
3685+
);
3686+
awaitwaitForAll([]);
3687+
3688+
ReactDOM.preload('route.css',{as: 'style'});
3689+
expect(getMeaningfulChildren(document.head)).toEqual(
3690+
<linkrel="preload"href="route.css"as="style"/>,
3691+
);
3692+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3693+
3694+
loadPreloads(['route.css']);
3695+
assertLog(['load preload: route.css']);
3696+
3697+
React.startTransition(()=>{
3698+
root.render(
3699+
<html>
3700+
<body>
3701+
<Suspensefallback="loading...">
3702+
<linkrel="stylesheet"href="route.css"precedence="default"/>
3703+
next
3704+
</Suspense>
3705+
</body>
3706+
</html>,
3707+
);
3708+
});
3709+
awaitwaitForAll([]);
3710+
3711+
expect(getMeaningfulChildren(document.head)).toEqual([
3712+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3713+
<linkrel="preload"href="route.css"as="style"/>,
3714+
]);
3715+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3716+
3717+
loadStylesheets(['route.css']);
3718+
assertLog(['load stylesheet: route.css']);
3719+
expect(getMeaningfulChildren(document.head)).toEqual([
3720+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3721+
<linkrel="preload"href="route.css"as="style"/>,
3722+
]);
3723+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3724+
});
3725+
3726+
it('suspends a transition on a stylesheet whose preload has not loaded yet',async()=>{
3727+
constroot=ReactDOMClient.createRoot(document);
3728+
root.render(
3729+
<html>
3730+
<body>
3731+
<Suspensefallback="loading...">initial</Suspense>
3732+
</body>
3733+
</html>,
3734+
);
3735+
awaitwaitForAll([]);
3736+
3737+
ReactDOM.preload('route.css',{as: 'style'});
3738+
expect(getMeaningfulChildren(document.head)).toEqual(
3739+
<linkrel="preload"href="route.css"as="style"/>,
3740+
);
3741+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3742+
3743+
React.startTransition(()=>{
3744+
root.render(
3745+
<html>
3746+
<body>
3747+
<Suspensefallback="loading...">
3748+
<linkrel="stylesheet"href="route.css"precedence="default"/>
3749+
next
3750+
</Suspense>
3751+
</body>
3752+
</html>,
3753+
);
3754+
});
3755+
awaitwaitForAll([]);
3756+
3757+
expect(getMeaningfulChildren(document.head)).toEqual(
3758+
<linkrel="preload"href="route.css"as="style"/>,
3759+
);
3760+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3761+
3762+
loadPreloads(['route.css']);
3763+
assertLog(['load preload: route.css']);
3764+
awaitwaitForAll([]);
3765+
expect(getMeaningfulChildren(document.head)).toEqual([
3766+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3767+
<linkrel="preload"href="route.css"as="style"/>,
3768+
]);
3769+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3770+
3771+
loadStylesheets(['route.css']);
3772+
assertLog(['load stylesheet: route.css']);
3773+
awaitwaitForAll([]);
3774+
expect(getMeaningfulChildren(document.head)).toEqual([
3775+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3776+
<linkrel="preload"href="route.css"as="style"/>,
3777+
]);
3778+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3779+
});
3780+
36773781
it('can suspend commits on more than one root for the same resource at the same time',async()=>{
36783782
document.body.innerHTML='';
36793783
constcontainer1=document.createElement('div');

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit d5736f0

Browse files
authored
[Fiber] track stylesheet preloads when explicitly preloaded (#36386)
Previously stylesheet resources would omit connecting with preloads inserted via `preload` which caused unecessary suspension of commits since the stylsheet resource would attempt to load the stylesheet again and delay the initial commit or commit a fallback (depending on whether the current screen should remain). This missing piece is that if you preload a stylesheet you must be able to use that sheets loading state when determining if the stylesheet is already loaded or not. This adds a pending indicator on client inserted prelaod links. We still assume SSR'd preloads are already loaded.
1 parent dd45307 commit d5736f0

3 files changed

Lines changed: 157 additions & 22 deletions

File tree

‎packages/react-dom-bindings/src/client/ReactDOMComponentTree.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const internalEventHandlesSetKey = '__reactHandles$' + randomKey;
5050
constinternalRootNodeResourcesKey='__reactResources$'+randomKey;
5151
constinternalHoistableMarker='__reactMarker$'+randomKey;
5252
constinternalScrollTimer='__reactScroll$'+randomKey;
53+
constinternalLoadPendingKey='__reactLoad$'+randomKey;
5354

5455
typeInstanceUnion=
5556
|Instance
@@ -386,6 +387,18 @@ export function clearScrollEndTimer(node: EventTarget): void {
386387
(node: any)[internalScrollTimer]=undefined;
387388
}
388389

390+
export function markNodeAsPendingLoad(node: Node): void {
391+
(node: any)[internalLoadPendingKey]=true;
392+
}
393+
394+
export function clearPendingLoadOnNode(node: Node): void {
395+
(node: any)[internalLoadPendingKey]=undefined;
396+
}
397+
398+
export function isNodePendingLoad(node: Node): boolean {
399+
return(node: any)[internalLoadPendingKey]===true;
400+
}
401+
389402
export function isOwnedInstance(node: Node): boolean {
390403
if(enableInternalInstanceMap){
391404
return!!(

‎packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js‎

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ import {
5656
getResourcesFromRoot,
5757
isMarkedHoistable,
5858
markNodeAsHoistable,
59+
markNodeAsPendingLoad,
60+
clearPendingLoadOnNode,
61+
isNodePendingLoad,
5962
isOwnedInstance,
6063
}from'./ReactDOMComponentTree';
6164
import{
@@ -5005,11 +5008,18 @@ function preload(href: string, as: string, options?: ?PreloadImplOptions) {
50055008
as==='script'&&
50065009
ownerDocument.querySelector(getScriptSelectorFromKey(key))
50075010
){
5008-
// We already have a stylesheet for this key. We don't need to preload it.
5011+
// We already have a script for this key. We don't need to preload it.
50095012
return;
50105013
}
50115014
constinstance=ownerDocument.createElement('link');
50125015
setInitialProperties(instance,'link',preloadProps);
5016+
if(as==='style'){
5017+
// Stash a loading state on the preload link. it will clean itself up once settled
5018+
markNodeAsPendingLoad(instance);
5019+
instance.onload=instance.onerror=()=>{
5020+
clearPendingLoadOnNode(instance);
5021+
};
5022+
}
50135023
markNodeAsHoistable(instance);
50145024
(ownerDocument.head: any).appendChild(instance);
50155025
}
@@ -5357,19 +5367,16 @@ export function getResource(
53575367
resource.instance=instance;
53585368
resource.state.loading=Loaded|Inserted;
53595369
}
5360-
}
5361-
5362-
if(!preloadPropsMap.has(key)){
5363-
constpreloadProps=preloadPropsFromStylesheet(qualifiedProps);
5364-
preloadPropsMap.set(key,preloadProps);
5365-
if(!instance){
5366-
preloadStylesheet(
5367-
ownerDocument,
5368-
key,
5369-
preloadProps,
5370-
resource.state,
5371-
);
5370+
}else{
5371+
// We don't have an instance we need to preload it instead
5372+
// $FlowFixMe[incompatible-type] -- the key we use here can only match non module preloads
5373+
letpreloadProps: void|PreloadProps=preloadPropsMap.get(key);
5374+
if(!preloadProps){
5375+
preloadProps=preloadPropsFromStylesheet(qualifiedProps);
5376+
preloadPropsMap.set(key,preloadProps);
53725377
}
5378+
5379+
preloadStylesheet(ownerDocument,key,preloadProps,resource.state);
53735380
}
53745381
}
53755382
if(currentProps&&currentResource===null){
@@ -5540,22 +5547,33 @@ function preloadStylesheet(
55405547
preloadProps: PreloadProps,
55415548
state: StylesheetState,
55425549
){
5543-
constpreloadEl=ownerDocument.querySelector(
5550+
letinstance=ownerDocument.querySelector(
55445551
getPreloadStylesheetSelectorFromKey(key),
55455552
);
5546-
if(preloadEl){
5547-
// If we find a preload already it was SSR'd and we won't have an actual
5548-
// loading state to track. For now we will just assume it is loaded
5549-
state.loading=Loaded;
5553+
if(instance){
5554+
if(!isNodePendingLoad(instance)){
5555+
// If we find a preload already it was SSR'd and we won't have an actual
5556+
// loading state to track. For now we will just assume it is loaded
5557+
state.loading=Loaded;
5558+
return;
5559+
}else{
5560+
// fall through and attach loading listeners
5561+
}
55505562
}else{
5551-
constinstance=ownerDocument.createElement('link');
5552-
state.preload=instance;
5553-
instance.addEventListener('load',()=>(state.loading|=Loaded));
5554-
instance.addEventListener('error',()=>(state.loading|=Errored));
5563+
instance=ownerDocument.createElement('link');
5564+
markNodeAsPendingLoad(instance);
5565+
instance.onload=instance.onerror=clearPendingLoadOnNode.bind(
5566+
null,
5567+
instance,
5568+
);
55555569
setInitialProperties(instance,'link',preloadProps);
55565570
markNodeAsHoistable(instance);
55575571
(ownerDocument.head: any).appendChild(instance);
55585572
}
5573+
// $FlowFixMe: [incompatible-type] -- if instance is an Element it will also be an HTMLLinkElement
5574+
state.preload=instance;
5575+
instance.addEventListener('load',()=>(state.loading|=Loaded));
5576+
instance.addEventListener('error',()=>(state.loading|=Errored));
55595577
}
55605578

55615579
functionpreloadPropsFromStylesheet(

‎packages/react-dom/src/__tests__/ReactDOMFloat-test.js‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3674,6 +3674,110 @@ body {
36743674
);
36753675
});
36763676

3677+
it('does not suspend a transition on a stylesheet whose preload has already loaded',async()=>{
3678+
constroot=ReactDOMClient.createRoot(document);
3679+
root.render(
3680+
<html>
3681+
<body>
3682+
<Suspensefallback="loading...">initial</Suspense>
3683+
</body>
3684+
</html>,
3685+
);
3686+
awaitwaitForAll([]);
3687+
3688+
ReactDOM.preload('route.css',{as: 'style'});
3689+
expect(getMeaningfulChildren(document.head)).toEqual(
3690+
<linkrel="preload"href="route.css"as="style"/>,
3691+
);
3692+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3693+
3694+
loadPreloads(['route.css']);
3695+
assertLog(['load preload: route.css']);
3696+
3697+
React.startTransition(()=>{
3698+
root.render(
3699+
<html>
3700+
<body>
3701+
<Suspensefallback="loading...">
3702+
<linkrel="stylesheet"href="route.css"precedence="default"/>
3703+
next
3704+
</Suspense>
3705+
</body>
3706+
</html>,
3707+
);
3708+
});
3709+
awaitwaitForAll([]);
3710+
3711+
expect(getMeaningfulChildren(document.head)).toEqual([
3712+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3713+
<linkrel="preload"href="route.css"as="style"/>,
3714+
]);
3715+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3716+
3717+
loadStylesheets(['route.css']);
3718+
assertLog(['load stylesheet: route.css']);
3719+
expect(getMeaningfulChildren(document.head)).toEqual([
3720+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3721+
<linkrel="preload"href="route.css"as="style"/>,
3722+
]);
3723+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3724+
});
3725+
3726+
it('suspends a transition on a stylesheet whose preload has not loaded yet',async()=>{
3727+
constroot=ReactDOMClient.createRoot(document);
3728+
root.render(
3729+
<html>
3730+
<body>
3731+
<Suspensefallback="loading...">initial</Suspense>
3732+
</body>
3733+
</html>,
3734+
);
3735+
awaitwaitForAll([]);
3736+
3737+
ReactDOM.preload('route.css',{as: 'style'});
3738+
expect(getMeaningfulChildren(document.head)).toEqual(
3739+
<linkrel="preload"href="route.css"as="style"/>,
3740+
);
3741+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3742+
3743+
React.startTransition(()=>{
3744+
root.render(
3745+
<html>
3746+
<body>
3747+
<Suspensefallback="loading...">
3748+
<linkrel="stylesheet"href="route.css"precedence="default"/>
3749+
next
3750+
</Suspense>
3751+
</body>
3752+
</html>,
3753+
);
3754+
});
3755+
awaitwaitForAll([]);
3756+
3757+
expect(getMeaningfulChildren(document.head)).toEqual(
3758+
<linkrel="preload"href="route.css"as="style"/>,
3759+
);
3760+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3761+
3762+
loadPreloads(['route.css']);
3763+
assertLog(['load preload: route.css']);
3764+
awaitwaitForAll([]);
3765+
expect(getMeaningfulChildren(document.head)).toEqual([
3766+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3767+
<linkrel="preload"href="route.css"as="style"/>,
3768+
]);
3769+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3770+
3771+
loadStylesheets(['route.css']);
3772+
assertLog(['load stylesheet: route.css']);
3773+
awaitwaitForAll([]);
3774+
expect(getMeaningfulChildren(document.head)).toEqual([
3775+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3776+
<linkrel="preload"href="route.css"as="style"/>,
3777+
]);
3778+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3779+
});
3780+
36773781
it('can suspend commits on more than one root for the same resource at the same time',async()=>{
36783782
document.body.innerHTML='';
36793783
constcontainer1=document.createElement('div');

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit d5736f0

Browse files
authored
[Fiber] track stylesheet preloads when explicitly preloaded (#36386)
Previously stylesheet resources would omit connecting with preloads inserted via `preload` which caused unecessary suspension of commits since the stylsheet resource would attempt to load the stylesheet again and delay the initial commit or commit a fallback (depending on whether the current screen should remain). This missing piece is that if you preload a stylesheet you must be able to use that sheets loading state when determining if the stylesheet is already loaded or not. This adds a pending indicator on client inserted prelaod links. We still assume SSR'd preloads are already loaded.
1 parent dd45307 commit d5736f0

3 files changed

Lines changed: 157 additions & 22 deletions

File tree

‎packages/react-dom-bindings/src/client/ReactDOMComponentTree.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const internalEventHandlesSetKey = '__reactHandles$' + randomKey;
5050
constinternalRootNodeResourcesKey='__reactResources$'+randomKey;
5151
constinternalHoistableMarker='__reactMarker$'+randomKey;
5252
constinternalScrollTimer='__reactScroll$'+randomKey;
53+
constinternalLoadPendingKey='__reactLoad$'+randomKey;
5354

5455
typeInstanceUnion=
5556
|Instance
@@ -386,6 +387,18 @@ export function clearScrollEndTimer(node: EventTarget): void {
386387
(node: any)[internalScrollTimer]=undefined;
387388
}
388389

390+
export function markNodeAsPendingLoad(node: Node): void {
391+
(node: any)[internalLoadPendingKey]=true;
392+
}
393+
394+
export function clearPendingLoadOnNode(node: Node): void {
395+
(node: any)[internalLoadPendingKey]=undefined;
396+
}
397+
398+
export function isNodePendingLoad(node: Node): boolean {
399+
return(node: any)[internalLoadPendingKey]===true;
400+
}
401+
389402
export function isOwnedInstance(node: Node): boolean {
390403
if(enableInternalInstanceMap){
391404
return!!(

‎packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js‎

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ import {
5656
getResourcesFromRoot,
5757
isMarkedHoistable,
5858
markNodeAsHoistable,
59+
markNodeAsPendingLoad,
60+
clearPendingLoadOnNode,
61+
isNodePendingLoad,
5962
isOwnedInstance,
6063
}from'./ReactDOMComponentTree';
6164
import{
@@ -5005,11 +5008,18 @@ function preload(href: string, as: string, options?: ?PreloadImplOptions) {
50055008
as==='script'&&
50065009
ownerDocument.querySelector(getScriptSelectorFromKey(key))
50075010
){
5008-
// We already have a stylesheet for this key. We don't need to preload it.
5011+
// We already have a script for this key. We don't need to preload it.
50095012
return;
50105013
}
50115014
constinstance=ownerDocument.createElement('link');
50125015
setInitialProperties(instance,'link',preloadProps);
5016+
if(as==='style'){
5017+
// Stash a loading state on the preload link. it will clean itself up once settled
5018+
markNodeAsPendingLoad(instance);
5019+
instance.onload=instance.onerror=()=>{
5020+
clearPendingLoadOnNode(instance);
5021+
};
5022+
}
50135023
markNodeAsHoistable(instance);
50145024
(ownerDocument.head: any).appendChild(instance);
50155025
}
@@ -5357,19 +5367,16 @@ export function getResource(
53575367
resource.instance=instance;
53585368
resource.state.loading=Loaded|Inserted;
53595369
}
5360-
}
5361-
5362-
if(!preloadPropsMap.has(key)){
5363-
constpreloadProps=preloadPropsFromStylesheet(qualifiedProps);
5364-
preloadPropsMap.set(key,preloadProps);
5365-
if(!instance){
5366-
preloadStylesheet(
5367-
ownerDocument,
5368-
key,
5369-
preloadProps,
5370-
resource.state,
5371-
);
5370+
}else{
5371+
// We don't have an instance we need to preload it instead
5372+
// $FlowFixMe[incompatible-type] -- the key we use here can only match non module preloads
5373+
letpreloadProps: void|PreloadProps=preloadPropsMap.get(key);
5374+
if(!preloadProps){
5375+
preloadProps=preloadPropsFromStylesheet(qualifiedProps);
5376+
preloadPropsMap.set(key,preloadProps);
53725377
}
5378+
5379+
preloadStylesheet(ownerDocument,key,preloadProps,resource.state);
53735380
}
53745381
}
53755382
if(currentProps&&currentResource===null){
@@ -5540,22 +5547,33 @@ function preloadStylesheet(
55405547
preloadProps: PreloadProps,
55415548
state: StylesheetState,
55425549
){
5543-
constpreloadEl=ownerDocument.querySelector(
5550+
letinstance=ownerDocument.querySelector(
55445551
getPreloadStylesheetSelectorFromKey(key),
55455552
);
5546-
if(preloadEl){
5547-
// If we find a preload already it was SSR'd and we won't have an actual
5548-
// loading state to track. For now we will just assume it is loaded
5549-
state.loading=Loaded;
5553+
if(instance){
5554+
if(!isNodePendingLoad(instance)){
5555+
// If we find a preload already it was SSR'd and we won't have an actual
5556+
// loading state to track. For now we will just assume it is loaded
5557+
state.loading=Loaded;
5558+
return;
5559+
}else{
5560+
// fall through and attach loading listeners
5561+
}
55505562
}else{
5551-
constinstance=ownerDocument.createElement('link');
5552-
state.preload=instance;
5553-
instance.addEventListener('load',()=>(state.loading|=Loaded));
5554-
instance.addEventListener('error',()=>(state.loading|=Errored));
5563+
instance=ownerDocument.createElement('link');
5564+
markNodeAsPendingLoad(instance);
5565+
instance.onload=instance.onerror=clearPendingLoadOnNode.bind(
5566+
null,
5567+
instance,
5568+
);
55555569
setInitialProperties(instance,'link',preloadProps);
55565570
markNodeAsHoistable(instance);
55575571
(ownerDocument.head: any).appendChild(instance);
55585572
}
5573+
// $FlowFixMe: [incompatible-type] -- if instance is an Element it will also be an HTMLLinkElement
5574+
state.preload=instance;
5575+
instance.addEventListener('load',()=>(state.loading|=Loaded));
5576+
instance.addEventListener('error',()=>(state.loading|=Errored));
55595577
}
55605578

55615579
functionpreloadPropsFromStylesheet(

‎packages/react-dom/src/__tests__/ReactDOMFloat-test.js‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3674,6 +3674,110 @@ body {
36743674
);
36753675
});
36763676

3677+
it('does not suspend a transition on a stylesheet whose preload has already loaded',async()=>{
3678+
constroot=ReactDOMClient.createRoot(document);
3679+
root.render(
3680+
<html>
3681+
<body>
3682+
<Suspensefallback="loading...">initial</Suspense>
3683+
</body>
3684+
</html>,
3685+
);
3686+
awaitwaitForAll([]);
3687+
3688+
ReactDOM.preload('route.css',{as: 'style'});
3689+
expect(getMeaningfulChildren(document.head)).toEqual(
3690+
<linkrel="preload"href="route.css"as="style"/>,
3691+
);
3692+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3693+
3694+
loadPreloads(['route.css']);
3695+
assertLog(['load preload: route.css']);
3696+
3697+
React.startTransition(()=>{
3698+
root.render(
3699+
<html>
3700+
<body>
3701+
<Suspensefallback="loading...">
3702+
<linkrel="stylesheet"href="route.css"precedence="default"/>
3703+
next
3704+
</Suspense>
3705+
</body>
3706+
</html>,
3707+
);
3708+
});
3709+
awaitwaitForAll([]);
3710+
3711+
expect(getMeaningfulChildren(document.head)).toEqual([
3712+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3713+
<linkrel="preload"href="route.css"as="style"/>,
3714+
]);
3715+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3716+
3717+
loadStylesheets(['route.css']);
3718+
assertLog(['load stylesheet: route.css']);
3719+
expect(getMeaningfulChildren(document.head)).toEqual([
3720+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3721+
<linkrel="preload"href="route.css"as="style"/>,
3722+
]);
3723+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3724+
});
3725+
3726+
it('suspends a transition on a stylesheet whose preload has not loaded yet',async()=>{
3727+
constroot=ReactDOMClient.createRoot(document);
3728+
root.render(
3729+
<html>
3730+
<body>
3731+
<Suspensefallback="loading...">initial</Suspense>
3732+
</body>
3733+
</html>,
3734+
);
3735+
awaitwaitForAll([]);
3736+
3737+
ReactDOM.preload('route.css',{as: 'style'});
3738+
expect(getMeaningfulChildren(document.head)).toEqual(
3739+
<linkrel="preload"href="route.css"as="style"/>,
3740+
);
3741+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3742+
3743+
React.startTransition(()=>{
3744+
root.render(
3745+
<html>
3746+
<body>
3747+
<Suspensefallback="loading...">
3748+
<linkrel="stylesheet"href="route.css"precedence="default"/>
3749+
next
3750+
</Suspense>
3751+
</body>
3752+
</html>,
3753+
);
3754+
});
3755+
awaitwaitForAll([]);
3756+
3757+
expect(getMeaningfulChildren(document.head)).toEqual(
3758+
<linkrel="preload"href="route.css"as="style"/>,
3759+
);
3760+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3761+
3762+
loadPreloads(['route.css']);
3763+
assertLog(['load preload: route.css']);
3764+
awaitwaitForAll([]);
3765+
expect(getMeaningfulChildren(document.head)).toEqual([
3766+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3767+
<linkrel="preload"href="route.css"as="style"/>,
3768+
]);
3769+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3770+
3771+
loadStylesheets(['route.css']);
3772+
assertLog(['load stylesheet: route.css']);
3773+
awaitwaitForAll([]);
3774+
expect(getMeaningfulChildren(document.head)).toEqual([
3775+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3776+
<linkrel="preload"href="route.css"as="style"/>,
3777+
]);
3778+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3779+
});
3780+
36773781
it('can suspend commits on more than one root for the same resource at the same time',async()=>{
36783782
document.body.innerHTML='';
36793783
constcontainer1=document.createElement('div');

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit d5736f0

Browse files
authored
[Fiber] track stylesheet preloads when explicitly preloaded (#36386)
Previously stylesheet resources would omit connecting with preloads inserted via `preload` which caused unecessary suspension of commits since the stylsheet resource would attempt to load the stylesheet again and delay the initial commit or commit a fallback (depending on whether the current screen should remain). This missing piece is that if you preload a stylesheet you must be able to use that sheets loading state when determining if the stylesheet is already loaded or not. This adds a pending indicator on client inserted prelaod links. We still assume SSR'd preloads are already loaded.
1 parent dd45307 commit d5736f0

3 files changed

Lines changed: 157 additions & 22 deletions

File tree

‎packages/react-dom-bindings/src/client/ReactDOMComponentTree.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const internalEventHandlesSetKey = '__reactHandles$' + randomKey;
5050
constinternalRootNodeResourcesKey='__reactResources$'+randomKey;
5151
constinternalHoistableMarker='__reactMarker$'+randomKey;
5252
constinternalScrollTimer='__reactScroll$'+randomKey;
53+
constinternalLoadPendingKey='__reactLoad$'+randomKey;
5354

5455
typeInstanceUnion=
5556
|Instance
@@ -386,6 +387,18 @@ export function clearScrollEndTimer(node: EventTarget): void {
386387
(node: any)[internalScrollTimer]=undefined;
387388
}
388389

390+
export function markNodeAsPendingLoad(node: Node): void {
391+
(node: any)[internalLoadPendingKey]=true;
392+
}
393+
394+
export function clearPendingLoadOnNode(node: Node): void {
395+
(node: any)[internalLoadPendingKey]=undefined;
396+
}
397+
398+
export function isNodePendingLoad(node: Node): boolean {
399+
return(node: any)[internalLoadPendingKey]===true;
400+
}
401+
389402
export function isOwnedInstance(node: Node): boolean {
390403
if(enableInternalInstanceMap){
391404
return!!(

‎packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js‎

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ import {
5656
getResourcesFromRoot,
5757
isMarkedHoistable,
5858
markNodeAsHoistable,
59+
markNodeAsPendingLoad,
60+
clearPendingLoadOnNode,
61+
isNodePendingLoad,
5962
isOwnedInstance,
6063
}from'./ReactDOMComponentTree';
6164
import{
@@ -5005,11 +5008,18 @@ function preload(href: string, as: string, options?: ?PreloadImplOptions) {
50055008
as==='script'&&
50065009
ownerDocument.querySelector(getScriptSelectorFromKey(key))
50075010
){
5008-
// We already have a stylesheet for this key. We don't need to preload it.
5011+
// We already have a script for this key. We don't need to preload it.
50095012
return;
50105013
}
50115014
constinstance=ownerDocument.createElement('link');
50125015
setInitialProperties(instance,'link',preloadProps);
5016+
if(as==='style'){
5017+
// Stash a loading state on the preload link. it will clean itself up once settled
5018+
markNodeAsPendingLoad(instance);
5019+
instance.onload=instance.onerror=()=>{
5020+
clearPendingLoadOnNode(instance);
5021+
};
5022+
}
50135023
markNodeAsHoistable(instance);
50145024
(ownerDocument.head: any).appendChild(instance);
50155025
}
@@ -5357,19 +5367,16 @@ export function getResource(
53575367
resource.instance=instance;
53585368
resource.state.loading=Loaded|Inserted;
53595369
}
5360-
}
5361-
5362-
if(!preloadPropsMap.has(key)){
5363-
constpreloadProps=preloadPropsFromStylesheet(qualifiedProps);
5364-
preloadPropsMap.set(key,preloadProps);
5365-
if(!instance){
5366-
preloadStylesheet(
5367-
ownerDocument,
5368-
key,
5369-
preloadProps,
5370-
resource.state,
5371-
);
5370+
}else{
5371+
// We don't have an instance we need to preload it instead
5372+
// $FlowFixMe[incompatible-type] -- the key we use here can only match non module preloads
5373+
letpreloadProps: void|PreloadProps=preloadPropsMap.get(key);
5374+
if(!preloadProps){
5375+
preloadProps=preloadPropsFromStylesheet(qualifiedProps);
5376+
preloadPropsMap.set(key,preloadProps);
53725377
}
5378+
5379+
preloadStylesheet(ownerDocument,key,preloadProps,resource.state);
53735380
}
53745381
}
53755382
if(currentProps&&currentResource===null){
@@ -5540,22 +5547,33 @@ function preloadStylesheet(
55405547
preloadProps: PreloadProps,
55415548
state: StylesheetState,
55425549
){
5543-
constpreloadEl=ownerDocument.querySelector(
5550+
letinstance=ownerDocument.querySelector(
55445551
getPreloadStylesheetSelectorFromKey(key),
55455552
);
5546-
if(preloadEl){
5547-
// If we find a preload already it was SSR'd and we won't have an actual
5548-
// loading state to track. For now we will just assume it is loaded
5549-
state.loading=Loaded;
5553+
if(instance){
5554+
if(!isNodePendingLoad(instance)){
5555+
// If we find a preload already it was SSR'd and we won't have an actual
5556+
// loading state to track. For now we will just assume it is loaded
5557+
state.loading=Loaded;
5558+
return;
5559+
}else{
5560+
// fall through and attach loading listeners
5561+
}
55505562
}else{
5551-
constinstance=ownerDocument.createElement('link');
5552-
state.preload=instance;
5553-
instance.addEventListener('load',()=>(state.loading|=Loaded));
5554-
instance.addEventListener('error',()=>(state.loading|=Errored));
5563+
instance=ownerDocument.createElement('link');
5564+
markNodeAsPendingLoad(instance);
5565+
instance.onload=instance.onerror=clearPendingLoadOnNode.bind(
5566+
null,
5567+
instance,
5568+
);
55555569
setInitialProperties(instance,'link',preloadProps);
55565570
markNodeAsHoistable(instance);
55575571
(ownerDocument.head: any).appendChild(instance);
55585572
}
5573+
// $FlowFixMe: [incompatible-type] -- if instance is an Element it will also be an HTMLLinkElement
5574+
state.preload=instance;
5575+
instance.addEventListener('load',()=>(state.loading|=Loaded));
5576+
instance.addEventListener('error',()=>(state.loading|=Errored));
55595577
}
55605578

55615579
functionpreloadPropsFromStylesheet(

‎packages/react-dom/src/__tests__/ReactDOMFloat-test.js‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3674,6 +3674,110 @@ body {
36743674
);
36753675
});
36763676

3677+
it('does not suspend a transition on a stylesheet whose preload has already loaded',async()=>{
3678+
constroot=ReactDOMClient.createRoot(document);
3679+
root.render(
3680+
<html>
3681+
<body>
3682+
<Suspensefallback="loading...">initial</Suspense>
3683+
</body>
3684+
</html>,
3685+
);
3686+
awaitwaitForAll([]);
3687+
3688+
ReactDOM.preload('route.css',{as: 'style'});
3689+
expect(getMeaningfulChildren(document.head)).toEqual(
3690+
<linkrel="preload"href="route.css"as="style"/>,
3691+
);
3692+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3693+
3694+
loadPreloads(['route.css']);
3695+
assertLog(['load preload: route.css']);
3696+
3697+
React.startTransition(()=>{
3698+
root.render(
3699+
<html>
3700+
<body>
3701+
<Suspensefallback="loading...">
3702+
<linkrel="stylesheet"href="route.css"precedence="default"/>
3703+
next
3704+
</Suspense>
3705+
</body>
3706+
</html>,
3707+
);
3708+
});
3709+
awaitwaitForAll([]);
3710+
3711+
expect(getMeaningfulChildren(document.head)).toEqual([
3712+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3713+
<linkrel="preload"href="route.css"as="style"/>,
3714+
]);
3715+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3716+
3717+
loadStylesheets(['route.css']);
3718+
assertLog(['load stylesheet: route.css']);
3719+
expect(getMeaningfulChildren(document.head)).toEqual([
3720+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3721+
<linkrel="preload"href="route.css"as="style"/>,
3722+
]);
3723+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3724+
});
3725+
3726+
it('suspends a transition on a stylesheet whose preload has not loaded yet',async()=>{
3727+
constroot=ReactDOMClient.createRoot(document);
3728+
root.render(
3729+
<html>
3730+
<body>
3731+
<Suspensefallback="loading...">initial</Suspense>
3732+
</body>
3733+
</html>,
3734+
);
3735+
awaitwaitForAll([]);
3736+
3737+
ReactDOM.preload('route.css',{as: 'style'});
3738+
expect(getMeaningfulChildren(document.head)).toEqual(
3739+
<linkrel="preload"href="route.css"as="style"/>,
3740+
);
3741+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3742+
3743+
React.startTransition(()=>{
3744+
root.render(
3745+
<html>
3746+
<body>
3747+
<Suspensefallback="loading...">
3748+
<linkrel="stylesheet"href="route.css"precedence="default"/>
3749+
next
3750+
</Suspense>
3751+
</body>
3752+
</html>,
3753+
);
3754+
});
3755+
awaitwaitForAll([]);
3756+
3757+
expect(getMeaningfulChildren(document.head)).toEqual(
3758+
<linkrel="preload"href="route.css"as="style"/>,
3759+
);
3760+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3761+
3762+
loadPreloads(['route.css']);
3763+
assertLog(['load preload: route.css']);
3764+
awaitwaitForAll([]);
3765+
expect(getMeaningfulChildren(document.head)).toEqual([
3766+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3767+
<linkrel="preload"href="route.css"as="style"/>,
3768+
]);
3769+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3770+
3771+
loadStylesheets(['route.css']);
3772+
assertLog(['load stylesheet: route.css']);
3773+
awaitwaitForAll([]);
3774+
expect(getMeaningfulChildren(document.head)).toEqual([
3775+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3776+
<linkrel="preload"href="route.css"as="style"/>,
3777+
]);
3778+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3779+
});
3780+
36773781
it('can suspend commits on more than one root for the same resource at the same time',async()=>{
36783782
document.body.innerHTML='';
36793783
constcontainer1=document.createElement('div');

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit d5736f0

Browse files
authored
[Fiber] track stylesheet preloads when explicitly preloaded (#36386)
Previously stylesheet resources would omit connecting with preloads inserted via `preload` which caused unecessary suspension of commits since the stylsheet resource would attempt to load the stylesheet again and delay the initial commit or commit a fallback (depending on whether the current screen should remain). This missing piece is that if you preload a stylesheet you must be able to use that sheets loading state when determining if the stylesheet is already loaded or not. This adds a pending indicator on client inserted prelaod links. We still assume SSR'd preloads are already loaded.
1 parent dd45307 commit d5736f0

3 files changed

Lines changed: 157 additions & 22 deletions

File tree

‎packages/react-dom-bindings/src/client/ReactDOMComponentTree.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const internalEventHandlesSetKey = '__reactHandles$' + randomKey;
5050
constinternalRootNodeResourcesKey='__reactResources$'+randomKey;
5151
constinternalHoistableMarker='__reactMarker$'+randomKey;
5252
constinternalScrollTimer='__reactScroll$'+randomKey;
53+
constinternalLoadPendingKey='__reactLoad$'+randomKey;
5354

5455
typeInstanceUnion=
5556
|Instance
@@ -386,6 +387,18 @@ export function clearScrollEndTimer(node: EventTarget): void {
386387
(node: any)[internalScrollTimer]=undefined;
387388
}
388389

390+
export function markNodeAsPendingLoad(node: Node): void {
391+
(node: any)[internalLoadPendingKey]=true;
392+
}
393+
394+
export function clearPendingLoadOnNode(node: Node): void {
395+
(node: any)[internalLoadPendingKey]=undefined;
396+
}
397+
398+
export function isNodePendingLoad(node: Node): boolean {
399+
return(node: any)[internalLoadPendingKey]===true;
400+
}
401+
389402
export function isOwnedInstance(node: Node): boolean {
390403
if(enableInternalInstanceMap){
391404
return!!(

‎packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js‎

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ import {
5656
getResourcesFromRoot,
5757
isMarkedHoistable,
5858
markNodeAsHoistable,
59+
markNodeAsPendingLoad,
60+
clearPendingLoadOnNode,
61+
isNodePendingLoad,
5962
isOwnedInstance,
6063
}from'./ReactDOMComponentTree';
6164
import{
@@ -5005,11 +5008,18 @@ function preload(href: string, as: string, options?: ?PreloadImplOptions) {
50055008
as==='script'&&
50065009
ownerDocument.querySelector(getScriptSelectorFromKey(key))
50075010
){
5008-
// We already have a stylesheet for this key. We don't need to preload it.
5011+
// We already have a script for this key. We don't need to preload it.
50095012
return;
50105013
}
50115014
constinstance=ownerDocument.createElement('link');
50125015
setInitialProperties(instance,'link',preloadProps);
5016+
if(as==='style'){
5017+
// Stash a loading state on the preload link. it will clean itself up once settled
5018+
markNodeAsPendingLoad(instance);
5019+
instance.onload=instance.onerror=()=>{
5020+
clearPendingLoadOnNode(instance);
5021+
};
5022+
}
50135023
markNodeAsHoistable(instance);
50145024
(ownerDocument.head: any).appendChild(instance);
50155025
}
@@ -5357,19 +5367,16 @@ export function getResource(
53575367
resource.instance=instance;
53585368
resource.state.loading=Loaded|Inserted;
53595369
}
5360-
}
5361-
5362-
if(!preloadPropsMap.has(key)){
5363-
constpreloadProps=preloadPropsFromStylesheet(qualifiedProps);
5364-
preloadPropsMap.set(key,preloadProps);
5365-
if(!instance){
5366-
preloadStylesheet(
5367-
ownerDocument,
5368-
key,
5369-
preloadProps,
5370-
resource.state,
5371-
);
5370+
}else{
5371+
// We don't have an instance we need to preload it instead
5372+
// $FlowFixMe[incompatible-type] -- the key we use here can only match non module preloads
5373+
letpreloadProps: void|PreloadProps=preloadPropsMap.get(key);
5374+
if(!preloadProps){
5375+
preloadProps=preloadPropsFromStylesheet(qualifiedProps);
5376+
preloadPropsMap.set(key,preloadProps);
53725377
}
5378+
5379+
preloadStylesheet(ownerDocument,key,preloadProps,resource.state);
53735380
}
53745381
}
53755382
if(currentProps&&currentResource===null){
@@ -5540,22 +5547,33 @@ function preloadStylesheet(
55405547
preloadProps: PreloadProps,
55415548
state: StylesheetState,
55425549
){
5543-
constpreloadEl=ownerDocument.querySelector(
5550+
letinstance=ownerDocument.querySelector(
55445551
getPreloadStylesheetSelectorFromKey(key),
55455552
);
5546-
if(preloadEl){
5547-
// If we find a preload already it was SSR'd and we won't have an actual
5548-
// loading state to track. For now we will just assume it is loaded
5549-
state.loading=Loaded;
5553+
if(instance){
5554+
if(!isNodePendingLoad(instance)){
5555+
// If we find a preload already it was SSR'd and we won't have an actual
5556+
// loading state to track. For now we will just assume it is loaded
5557+
state.loading=Loaded;
5558+
return;
5559+
}else{
5560+
// fall through and attach loading listeners
5561+
}
55505562
}else{
5551-
constinstance=ownerDocument.createElement('link');
5552-
state.preload=instance;
5553-
instance.addEventListener('load',()=>(state.loading|=Loaded));
5554-
instance.addEventListener('error',()=>(state.loading|=Errored));
5563+
instance=ownerDocument.createElement('link');
5564+
markNodeAsPendingLoad(instance);
5565+
instance.onload=instance.onerror=clearPendingLoadOnNode.bind(
5566+
null,
5567+
instance,
5568+
);
55555569
setInitialProperties(instance,'link',preloadProps);
55565570
markNodeAsHoistable(instance);
55575571
(ownerDocument.head: any).appendChild(instance);
55585572
}
5573+
// $FlowFixMe: [incompatible-type] -- if instance is an Element it will also be an HTMLLinkElement
5574+
state.preload=instance;
5575+
instance.addEventListener('load',()=>(state.loading|=Loaded));
5576+
instance.addEventListener('error',()=>(state.loading|=Errored));
55595577
}
55605578

55615579
functionpreloadPropsFromStylesheet(

‎packages/react-dom/src/__tests__/ReactDOMFloat-test.js‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3674,6 +3674,110 @@ body {
36743674
);
36753675
});
36763676

3677+
it('does not suspend a transition on a stylesheet whose preload has already loaded',async()=>{
3678+
constroot=ReactDOMClient.createRoot(document);
3679+
root.render(
3680+
<html>
3681+
<body>
3682+
<Suspensefallback="loading...">initial</Suspense>
3683+
</body>
3684+
</html>,
3685+
);
3686+
awaitwaitForAll([]);
3687+
3688+
ReactDOM.preload('route.css',{as: 'style'});
3689+
expect(getMeaningfulChildren(document.head)).toEqual(
3690+
<linkrel="preload"href="route.css"as="style"/>,
3691+
);
3692+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3693+
3694+
loadPreloads(['route.css']);
3695+
assertLog(['load preload: route.css']);
3696+
3697+
React.startTransition(()=>{
3698+
root.render(
3699+
<html>
3700+
<body>
3701+
<Suspensefallback="loading...">
3702+
<linkrel="stylesheet"href="route.css"precedence="default"/>
3703+
next
3704+
</Suspense>
3705+
</body>
3706+
</html>,
3707+
);
3708+
});
3709+
awaitwaitForAll([]);
3710+
3711+
expect(getMeaningfulChildren(document.head)).toEqual([
3712+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3713+
<linkrel="preload"href="route.css"as="style"/>,
3714+
]);
3715+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3716+
3717+
loadStylesheets(['route.css']);
3718+
assertLog(['load stylesheet: route.css']);
3719+
expect(getMeaningfulChildren(document.head)).toEqual([
3720+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3721+
<linkrel="preload"href="route.css"as="style"/>,
3722+
]);
3723+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3724+
});
3725+
3726+
it('suspends a transition on a stylesheet whose preload has not loaded yet',async()=>{
3727+
constroot=ReactDOMClient.createRoot(document);
3728+
root.render(
3729+
<html>
3730+
<body>
3731+
<Suspensefallback="loading...">initial</Suspense>
3732+
</body>
3733+
</html>,
3734+
);
3735+
awaitwaitForAll([]);
3736+
3737+
ReactDOM.preload('route.css',{as: 'style'});
3738+
expect(getMeaningfulChildren(document.head)).toEqual(
3739+
<linkrel="preload"href="route.css"as="style"/>,
3740+
);
3741+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3742+
3743+
React.startTransition(()=>{
3744+
root.render(
3745+
<html>
3746+
<body>
3747+
<Suspensefallback="loading...">
3748+
<linkrel="stylesheet"href="route.css"precedence="default"/>
3749+
next
3750+
</Suspense>
3751+
</body>
3752+
</html>,
3753+
);
3754+
});
3755+
awaitwaitForAll([]);
3756+
3757+
expect(getMeaningfulChildren(document.head)).toEqual(
3758+
<linkrel="preload"href="route.css"as="style"/>,
3759+
);
3760+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3761+
3762+
loadPreloads(['route.css']);
3763+
assertLog(['load preload: route.css']);
3764+
awaitwaitForAll([]);
3765+
expect(getMeaningfulChildren(document.head)).toEqual([
3766+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3767+
<linkrel="preload"href="route.css"as="style"/>,
3768+
]);
3769+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3770+
3771+
loadStylesheets(['route.css']);
3772+
assertLog(['load stylesheet: route.css']);
3773+
awaitwaitForAll([]);
3774+
expect(getMeaningfulChildren(document.head)).toEqual([
3775+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3776+
<linkrel="preload"href="route.css"as="style"/>,
3777+
]);
3778+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3779+
});
3780+
36773781
it('can suspend commits on more than one root for the same resource at the same time',async()=>{
36783782
document.body.innerHTML='';
36793783
constcontainer1=document.createElement('div');

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit d5736f0

Browse files
authored
[Fiber] track stylesheet preloads when explicitly preloaded (#36386)
Previously stylesheet resources would omit connecting with preloads inserted via `preload` which caused unecessary suspension of commits since the stylsheet resource would attempt to load the stylesheet again and delay the initial commit or commit a fallback (depending on whether the current screen should remain). This missing piece is that if you preload a stylesheet you must be able to use that sheets loading state when determining if the stylesheet is already loaded or not. This adds a pending indicator on client inserted prelaod links. We still assume SSR'd preloads are already loaded.
1 parent dd45307 commit d5736f0

3 files changed

Lines changed: 157 additions & 22 deletions

File tree

‎packages/react-dom-bindings/src/client/ReactDOMComponentTree.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const internalEventHandlesSetKey = '__reactHandles$' + randomKey;
5050
constinternalRootNodeResourcesKey='__reactResources$'+randomKey;
5151
constinternalHoistableMarker='__reactMarker$'+randomKey;
5252
constinternalScrollTimer='__reactScroll$'+randomKey;
53+
constinternalLoadPendingKey='__reactLoad$'+randomKey;
5354

5455
typeInstanceUnion=
5556
|Instance
@@ -386,6 +387,18 @@ export function clearScrollEndTimer(node: EventTarget): void {
386387
(node: any)[internalScrollTimer]=undefined;
387388
}
388389

390+
export function markNodeAsPendingLoad(node: Node): void {
391+
(node: any)[internalLoadPendingKey]=true;
392+
}
393+
394+
export function clearPendingLoadOnNode(node: Node): void {
395+
(node: any)[internalLoadPendingKey]=undefined;
396+
}
397+
398+
export function isNodePendingLoad(node: Node): boolean {
399+
return(node: any)[internalLoadPendingKey]===true;
400+
}
401+
389402
export function isOwnedInstance(node: Node): boolean {
390403
if(enableInternalInstanceMap){
391404
return!!(

‎packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js‎

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ import {
5656
getResourcesFromRoot,
5757
isMarkedHoistable,
5858
markNodeAsHoistable,
59+
markNodeAsPendingLoad,
60+
clearPendingLoadOnNode,
61+
isNodePendingLoad,
5962
isOwnedInstance,
6063
}from'./ReactDOMComponentTree';
6164
import{
@@ -5005,11 +5008,18 @@ function preload(href: string, as: string, options?: ?PreloadImplOptions) {
50055008
as==='script'&&
50065009
ownerDocument.querySelector(getScriptSelectorFromKey(key))
50075010
){
5008-
// We already have a stylesheet for this key. We don't need to preload it.
5011+
// We already have a script for this key. We don't need to preload it.
50095012
return;
50105013
}
50115014
constinstance=ownerDocument.createElement('link');
50125015
setInitialProperties(instance,'link',preloadProps);
5016+
if(as==='style'){
5017+
// Stash a loading state on the preload link. it will clean itself up once settled
5018+
markNodeAsPendingLoad(instance);
5019+
instance.onload=instance.onerror=()=>{
5020+
clearPendingLoadOnNode(instance);
5021+
};
5022+
}
50135023
markNodeAsHoistable(instance);
50145024
(ownerDocument.head: any).appendChild(instance);
50155025
}
@@ -5357,19 +5367,16 @@ export function getResource(
53575367
resource.instance=instance;
53585368
resource.state.loading=Loaded|Inserted;
53595369
}
5360-
}
5361-
5362-
if(!preloadPropsMap.has(key)){
5363-
constpreloadProps=preloadPropsFromStylesheet(qualifiedProps);
5364-
preloadPropsMap.set(key,preloadProps);
5365-
if(!instance){
5366-
preloadStylesheet(
5367-
ownerDocument,
5368-
key,
5369-
preloadProps,
5370-
resource.state,
5371-
);
5370+
}else{
5371+
// We don't have an instance we need to preload it instead
5372+
// $FlowFixMe[incompatible-type] -- the key we use here can only match non module preloads
5373+
letpreloadProps: void|PreloadProps=preloadPropsMap.get(key);
5374+
if(!preloadProps){
5375+
preloadProps=preloadPropsFromStylesheet(qualifiedProps);
5376+
preloadPropsMap.set(key,preloadProps);
53725377
}
5378+
5379+
preloadStylesheet(ownerDocument,key,preloadProps,resource.state);
53735380
}
53745381
}
53755382
if(currentProps&&currentResource===null){
@@ -5540,22 +5547,33 @@ function preloadStylesheet(
55405547
preloadProps: PreloadProps,
55415548
state: StylesheetState,
55425549
){
5543-
constpreloadEl=ownerDocument.querySelector(
5550+
letinstance=ownerDocument.querySelector(
55445551
getPreloadStylesheetSelectorFromKey(key),
55455552
);
5546-
if(preloadEl){
5547-
// If we find a preload already it was SSR'd and we won't have an actual
5548-
// loading state to track. For now we will just assume it is loaded
5549-
state.loading=Loaded;
5553+
if(instance){
5554+
if(!isNodePendingLoad(instance)){
5555+
// If we find a preload already it was SSR'd and we won't have an actual
5556+
// loading state to track. For now we will just assume it is loaded
5557+
state.loading=Loaded;
5558+
return;
5559+
}else{
5560+
// fall through and attach loading listeners
5561+
}
55505562
}else{
5551-
constinstance=ownerDocument.createElement('link');
5552-
state.preload=instance;
5553-
instance.addEventListener('load',()=>(state.loading|=Loaded));
5554-
instance.addEventListener('error',()=>(state.loading|=Errored));
5563+
instance=ownerDocument.createElement('link');
5564+
markNodeAsPendingLoad(instance);
5565+
instance.onload=instance.onerror=clearPendingLoadOnNode.bind(
5566+
null,
5567+
instance,
5568+
);
55555569
setInitialProperties(instance,'link',preloadProps);
55565570
markNodeAsHoistable(instance);
55575571
(ownerDocument.head: any).appendChild(instance);
55585572
}
5573+
// $FlowFixMe: [incompatible-type] -- if instance is an Element it will also be an HTMLLinkElement
5574+
state.preload=instance;
5575+
instance.addEventListener('load',()=>(state.loading|=Loaded));
5576+
instance.addEventListener('error',()=>(state.loading|=Errored));
55595577
}
55605578

55615579
functionpreloadPropsFromStylesheet(

‎packages/react-dom/src/__tests__/ReactDOMFloat-test.js‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3674,6 +3674,110 @@ body {
36743674
);
36753675
});
36763676

3677+
it('does not suspend a transition on a stylesheet whose preload has already loaded',async()=>{
3678+
constroot=ReactDOMClient.createRoot(document);
3679+
root.render(
3680+
<html>
3681+
<body>
3682+
<Suspensefallback="loading...">initial</Suspense>
3683+
</body>
3684+
</html>,
3685+
);
3686+
awaitwaitForAll([]);
3687+
3688+
ReactDOM.preload('route.css',{as: 'style'});
3689+
expect(getMeaningfulChildren(document.head)).toEqual(
3690+
<linkrel="preload"href="route.css"as="style"/>,
3691+
);
3692+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3693+
3694+
loadPreloads(['route.css']);
3695+
assertLog(['load preload: route.css']);
3696+
3697+
React.startTransition(()=>{
3698+
root.render(
3699+
<html>
3700+
<body>
3701+
<Suspensefallback="loading...">
3702+
<linkrel="stylesheet"href="route.css"precedence="default"/>
3703+
next
3704+
</Suspense>
3705+
</body>
3706+
</html>,
3707+
);
3708+
});
3709+
awaitwaitForAll([]);
3710+
3711+
expect(getMeaningfulChildren(document.head)).toEqual([
3712+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3713+
<linkrel="preload"href="route.css"as="style"/>,
3714+
]);
3715+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3716+
3717+
loadStylesheets(['route.css']);
3718+
assertLog(['load stylesheet: route.css']);
3719+
expect(getMeaningfulChildren(document.head)).toEqual([
3720+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3721+
<linkrel="preload"href="route.css"as="style"/>,
3722+
]);
3723+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3724+
});
3725+
3726+
it('suspends a transition on a stylesheet whose preload has not loaded yet',async()=>{
3727+
constroot=ReactDOMClient.createRoot(document);
3728+
root.render(
3729+
<html>
3730+
<body>
3731+
<Suspensefallback="loading...">initial</Suspense>
3732+
</body>
3733+
</html>,
3734+
);
3735+
awaitwaitForAll([]);
3736+
3737+
ReactDOM.preload('route.css',{as: 'style'});
3738+
expect(getMeaningfulChildren(document.head)).toEqual(
3739+
<linkrel="preload"href="route.css"as="style"/>,
3740+
);
3741+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3742+
3743+
React.startTransition(()=>{
3744+
root.render(
3745+
<html>
3746+
<body>
3747+
<Suspensefallback="loading...">
3748+
<linkrel="stylesheet"href="route.css"precedence="default"/>
3749+
next
3750+
</Suspense>
3751+
</body>
3752+
</html>,
3753+
);
3754+
});
3755+
awaitwaitForAll([]);
3756+
3757+
expect(getMeaningfulChildren(document.head)).toEqual(
3758+
<linkrel="preload"href="route.css"as="style"/>,
3759+
);
3760+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3761+
3762+
loadPreloads(['route.css']);
3763+
assertLog(['load preload: route.css']);
3764+
awaitwaitForAll([]);
3765+
expect(getMeaningfulChildren(document.head)).toEqual([
3766+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3767+
<linkrel="preload"href="route.css"as="style"/>,
3768+
]);
3769+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3770+
3771+
loadStylesheets(['route.css']);
3772+
assertLog(['load stylesheet: route.css']);
3773+
awaitwaitForAll([]);
3774+
expect(getMeaningfulChildren(document.head)).toEqual([
3775+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3776+
<linkrel="preload"href="route.css"as="style"/>,
3777+
]);
3778+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3779+
});
3780+
36773781
it('can suspend commits on more than one root for the same resource at the same time',async()=>{
36783782
document.body.innerHTML='';
36793783
constcontainer1=document.createElement('div');

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit d5736f0

Browse files
authored
[Fiber] track stylesheet preloads when explicitly preloaded (#36386)
Previously stylesheet resources would omit connecting with preloads inserted via `preload` which caused unecessary suspension of commits since the stylsheet resource would attempt to load the stylesheet again and delay the initial commit or commit a fallback (depending on whether the current screen should remain). This missing piece is that if you preload a stylesheet you must be able to use that sheets loading state when determining if the stylesheet is already loaded or not. This adds a pending indicator on client inserted prelaod links. We still assume SSR'd preloads are already loaded.
1 parent dd45307 commit d5736f0

3 files changed

Lines changed: 157 additions & 22 deletions

File tree

‎packages/react-dom-bindings/src/client/ReactDOMComponentTree.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const internalEventHandlesSetKey = '__reactHandles$' + randomKey;
5050
constinternalRootNodeResourcesKey='__reactResources$'+randomKey;
5151
constinternalHoistableMarker='__reactMarker$'+randomKey;
5252
constinternalScrollTimer='__reactScroll$'+randomKey;
53+
constinternalLoadPendingKey='__reactLoad$'+randomKey;
5354

5455
typeInstanceUnion=
5556
|Instance
@@ -386,6 +387,18 @@ export function clearScrollEndTimer(node: EventTarget): void {
386387
(node: any)[internalScrollTimer]=undefined;
387388
}
388389

390+
export function markNodeAsPendingLoad(node: Node): void {
391+
(node: any)[internalLoadPendingKey]=true;
392+
}
393+
394+
export function clearPendingLoadOnNode(node: Node): void {
395+
(node: any)[internalLoadPendingKey]=undefined;
396+
}
397+
398+
export function isNodePendingLoad(node: Node): boolean {
399+
return(node: any)[internalLoadPendingKey]===true;
400+
}
401+
389402
export function isOwnedInstance(node: Node): boolean {
390403
if(enableInternalInstanceMap){
391404
return!!(

‎packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js‎

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ import {
5656
getResourcesFromRoot,
5757
isMarkedHoistable,
5858
markNodeAsHoistable,
59+
markNodeAsPendingLoad,
60+
clearPendingLoadOnNode,
61+
isNodePendingLoad,
5962
isOwnedInstance,
6063
}from'./ReactDOMComponentTree';
6164
import{
@@ -5005,11 +5008,18 @@ function preload(href: string, as: string, options?: ?PreloadImplOptions) {
50055008
as==='script'&&
50065009
ownerDocument.querySelector(getScriptSelectorFromKey(key))
50075010
){
5008-
// We already have a stylesheet for this key. We don't need to preload it.
5011+
// We already have a script for this key. We don't need to preload it.
50095012
return;
50105013
}
50115014
constinstance=ownerDocument.createElement('link');
50125015
setInitialProperties(instance,'link',preloadProps);
5016+
if(as==='style'){
5017+
// Stash a loading state on the preload link. it will clean itself up once settled
5018+
markNodeAsPendingLoad(instance);
5019+
instance.onload=instance.onerror=()=>{
5020+
clearPendingLoadOnNode(instance);
5021+
};
5022+
}
50135023
markNodeAsHoistable(instance);
50145024
(ownerDocument.head: any).appendChild(instance);
50155025
}
@@ -5357,19 +5367,16 @@ export function getResource(
53575367
resource.instance=instance;
53585368
resource.state.loading=Loaded|Inserted;
53595369
}
5360-
}
5361-
5362-
if(!preloadPropsMap.has(key)){
5363-
constpreloadProps=preloadPropsFromStylesheet(qualifiedProps);
5364-
preloadPropsMap.set(key,preloadProps);
5365-
if(!instance){
5366-
preloadStylesheet(
5367-
ownerDocument,
5368-
key,
5369-
preloadProps,
5370-
resource.state,
5371-
);
5370+
}else{
5371+
// We don't have an instance we need to preload it instead
5372+
// $FlowFixMe[incompatible-type] -- the key we use here can only match non module preloads
5373+
letpreloadProps: void|PreloadProps=preloadPropsMap.get(key);
5374+
if(!preloadProps){
5375+
preloadProps=preloadPropsFromStylesheet(qualifiedProps);
5376+
preloadPropsMap.set(key,preloadProps);
53725377
}
5378+
5379+
preloadStylesheet(ownerDocument,key,preloadProps,resource.state);
53735380
}
53745381
}
53755382
if(currentProps&&currentResource===null){
@@ -5540,22 +5547,33 @@ function preloadStylesheet(
55405547
preloadProps: PreloadProps,
55415548
state: StylesheetState,
55425549
){
5543-
constpreloadEl=ownerDocument.querySelector(
5550+
letinstance=ownerDocument.querySelector(
55445551
getPreloadStylesheetSelectorFromKey(key),
55455552
);
5546-
if(preloadEl){
5547-
// If we find a preload already it was SSR'd and we won't have an actual
5548-
// loading state to track. For now we will just assume it is loaded
5549-
state.loading=Loaded;
5553+
if(instance){
5554+
if(!isNodePendingLoad(instance)){
5555+
// If we find a preload already it was SSR'd and we won't have an actual
5556+
// loading state to track. For now we will just assume it is loaded
5557+
state.loading=Loaded;
5558+
return;
5559+
}else{
5560+
// fall through and attach loading listeners
5561+
}
55505562
}else{
5551-
constinstance=ownerDocument.createElement('link');
5552-
state.preload=instance;
5553-
instance.addEventListener('load',()=>(state.loading|=Loaded));
5554-
instance.addEventListener('error',()=>(state.loading|=Errored));
5563+
instance=ownerDocument.createElement('link');
5564+
markNodeAsPendingLoad(instance);
5565+
instance.onload=instance.onerror=clearPendingLoadOnNode.bind(
5566+
null,
5567+
instance,
5568+
);
55555569
setInitialProperties(instance,'link',preloadProps);
55565570
markNodeAsHoistable(instance);
55575571
(ownerDocument.head: any).appendChild(instance);
55585572
}
5573+
// $FlowFixMe: [incompatible-type] -- if instance is an Element it will also be an HTMLLinkElement
5574+
state.preload=instance;
5575+
instance.addEventListener('load',()=>(state.loading|=Loaded));
5576+
instance.addEventListener('error',()=>(state.loading|=Errored));
55595577
}
55605578

55615579
functionpreloadPropsFromStylesheet(

‎packages/react-dom/src/__tests__/ReactDOMFloat-test.js‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3674,6 +3674,110 @@ body {
36743674
);
36753675
});
36763676

3677+
it('does not suspend a transition on a stylesheet whose preload has already loaded',async()=>{
3678+
constroot=ReactDOMClient.createRoot(document);
3679+
root.render(
3680+
<html>
3681+
<body>
3682+
<Suspensefallback="loading...">initial</Suspense>
3683+
</body>
3684+
</html>,
3685+
);
3686+
awaitwaitForAll([]);
3687+
3688+
ReactDOM.preload('route.css',{as: 'style'});
3689+
expect(getMeaningfulChildren(document.head)).toEqual(
3690+
<linkrel="preload"href="route.css"as="style"/>,
3691+
);
3692+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3693+
3694+
loadPreloads(['route.css']);
3695+
assertLog(['load preload: route.css']);
3696+
3697+
React.startTransition(()=>{
3698+
root.render(
3699+
<html>
3700+
<body>
3701+
<Suspensefallback="loading...">
3702+
<linkrel="stylesheet"href="route.css"precedence="default"/>
3703+
next
3704+
</Suspense>
3705+
</body>
3706+
</html>,
3707+
);
3708+
});
3709+
awaitwaitForAll([]);
3710+
3711+
expect(getMeaningfulChildren(document.head)).toEqual([
3712+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3713+
<linkrel="preload"href="route.css"as="style"/>,
3714+
]);
3715+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3716+
3717+
loadStylesheets(['route.css']);
3718+
assertLog(['load stylesheet: route.css']);
3719+
expect(getMeaningfulChildren(document.head)).toEqual([
3720+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3721+
<linkrel="preload"href="route.css"as="style"/>,
3722+
]);
3723+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3724+
});
3725+
3726+
it('suspends a transition on a stylesheet whose preload has not loaded yet',async()=>{
3727+
constroot=ReactDOMClient.createRoot(document);
3728+
root.render(
3729+
<html>
3730+
<body>
3731+
<Suspensefallback="loading...">initial</Suspense>
3732+
</body>
3733+
</html>,
3734+
);
3735+
awaitwaitForAll([]);
3736+
3737+
ReactDOM.preload('route.css',{as: 'style'});
3738+
expect(getMeaningfulChildren(document.head)).toEqual(
3739+
<linkrel="preload"href="route.css"as="style"/>,
3740+
);
3741+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3742+
3743+
React.startTransition(()=>{
3744+
root.render(
3745+
<html>
3746+
<body>
3747+
<Suspensefallback="loading...">
3748+
<linkrel="stylesheet"href="route.css"precedence="default"/>
3749+
next
3750+
</Suspense>
3751+
</body>
3752+
</html>,
3753+
);
3754+
});
3755+
awaitwaitForAll([]);
3756+
3757+
expect(getMeaningfulChildren(document.head)).toEqual(
3758+
<linkrel="preload"href="route.css"as="style"/>,
3759+
);
3760+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3761+
3762+
loadPreloads(['route.css']);
3763+
assertLog(['load preload: route.css']);
3764+
awaitwaitForAll([]);
3765+
expect(getMeaningfulChildren(document.head)).toEqual([
3766+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3767+
<linkrel="preload"href="route.css"as="style"/>,
3768+
]);
3769+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3770+
3771+
loadStylesheets(['route.css']);
3772+
assertLog(['load stylesheet: route.css']);
3773+
awaitwaitForAll([]);
3774+
expect(getMeaningfulChildren(document.head)).toEqual([
3775+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3776+
<linkrel="preload"href="route.css"as="style"/>,
3777+
]);
3778+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3779+
});
3780+
36773781
it('can suspend commits on more than one root for the same resource at the same time',async()=>{
36783782
document.body.innerHTML='';
36793783
constcontainer1=document.createElement('div');

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit d5736f0

Browse files
authored
[Fiber] track stylesheet preloads when explicitly preloaded (#36386)
Previously stylesheet resources would omit connecting with preloads inserted via `preload` which caused unecessary suspension of commits since the stylsheet resource would attempt to load the stylesheet again and delay the initial commit or commit a fallback (depending on whether the current screen should remain). This missing piece is that if you preload a stylesheet you must be able to use that sheets loading state when determining if the stylesheet is already loaded or not. This adds a pending indicator on client inserted prelaod links. We still assume SSR'd preloads are already loaded.
1 parent dd45307 commit d5736f0

3 files changed

Lines changed: 157 additions & 22 deletions

File tree

‎packages/react-dom-bindings/src/client/ReactDOMComponentTree.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const internalEventHandlesSetKey = '__reactHandles$' + randomKey;
5050
constinternalRootNodeResourcesKey='__reactResources$'+randomKey;
5151
constinternalHoistableMarker='__reactMarker$'+randomKey;
5252
constinternalScrollTimer='__reactScroll$'+randomKey;
53+
constinternalLoadPendingKey='__reactLoad$'+randomKey;
5354

5455
typeInstanceUnion=
5556
|Instance
@@ -386,6 +387,18 @@ export function clearScrollEndTimer(node: EventTarget): void {
386387
(node: any)[internalScrollTimer]=undefined;
387388
}
388389

390+
export function markNodeAsPendingLoad(node: Node): void {
391+
(node: any)[internalLoadPendingKey]=true;
392+
}
393+
394+
export function clearPendingLoadOnNode(node: Node): void {
395+
(node: any)[internalLoadPendingKey]=undefined;
396+
}
397+
398+
export function isNodePendingLoad(node: Node): boolean {
399+
return(node: any)[internalLoadPendingKey]===true;
400+
}
401+
389402
export function isOwnedInstance(node: Node): boolean {
390403
if(enableInternalInstanceMap){
391404
return!!(

‎packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js‎

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ import {
5656
getResourcesFromRoot,
5757
isMarkedHoistable,
5858
markNodeAsHoistable,
59+
markNodeAsPendingLoad,
60+
clearPendingLoadOnNode,
61+
isNodePendingLoad,
5962
isOwnedInstance,
6063
}from'./ReactDOMComponentTree';
6164
import{
@@ -5005,11 +5008,18 @@ function preload(href: string, as: string, options?: ?PreloadImplOptions) {
50055008
as==='script'&&
50065009
ownerDocument.querySelector(getScriptSelectorFromKey(key))
50075010
){
5008-
// We already have a stylesheet for this key. We don't need to preload it.
5011+
// We already have a script for this key. We don't need to preload it.
50095012
return;
50105013
}
50115014
constinstance=ownerDocument.createElement('link');
50125015
setInitialProperties(instance,'link',preloadProps);
5016+
if(as==='style'){
5017+
// Stash a loading state on the preload link. it will clean itself up once settled
5018+
markNodeAsPendingLoad(instance);
5019+
instance.onload=instance.onerror=()=>{
5020+
clearPendingLoadOnNode(instance);
5021+
};
5022+
}
50135023
markNodeAsHoistable(instance);
50145024
(ownerDocument.head: any).appendChild(instance);
50155025
}
@@ -5357,19 +5367,16 @@ export function getResource(
53575367
resource.instance=instance;
53585368
resource.state.loading=Loaded|Inserted;
53595369
}
5360-
}
5361-
5362-
if(!preloadPropsMap.has(key)){
5363-
constpreloadProps=preloadPropsFromStylesheet(qualifiedProps);
5364-
preloadPropsMap.set(key,preloadProps);
5365-
if(!instance){
5366-
preloadStylesheet(
5367-
ownerDocument,
5368-
key,
5369-
preloadProps,
5370-
resource.state,
5371-
);
5370+
}else{
5371+
// We don't have an instance we need to preload it instead
5372+
// $FlowFixMe[incompatible-type] -- the key we use here can only match non module preloads
5373+
letpreloadProps: void|PreloadProps=preloadPropsMap.get(key);
5374+
if(!preloadProps){
5375+
preloadProps=preloadPropsFromStylesheet(qualifiedProps);
5376+
preloadPropsMap.set(key,preloadProps);
53725377
}
5378+
5379+
preloadStylesheet(ownerDocument,key,preloadProps,resource.state);
53735380
}
53745381
}
53755382
if(currentProps&&currentResource===null){
@@ -5540,22 +5547,33 @@ function preloadStylesheet(
55405547
preloadProps: PreloadProps,
55415548
state: StylesheetState,
55425549
){
5543-
constpreloadEl=ownerDocument.querySelector(
5550+
letinstance=ownerDocument.querySelector(
55445551
getPreloadStylesheetSelectorFromKey(key),
55455552
);
5546-
if(preloadEl){
5547-
// If we find a preload already it was SSR'd and we won't have an actual
5548-
// loading state to track. For now we will just assume it is loaded
5549-
state.loading=Loaded;
5553+
if(instance){
5554+
if(!isNodePendingLoad(instance)){
5555+
// If we find a preload already it was SSR'd and we won't have an actual
5556+
// loading state to track. For now we will just assume it is loaded
5557+
state.loading=Loaded;
5558+
return;
5559+
}else{
5560+
// fall through and attach loading listeners
5561+
}
55505562
}else{
5551-
constinstance=ownerDocument.createElement('link');
5552-
state.preload=instance;
5553-
instance.addEventListener('load',()=>(state.loading|=Loaded));
5554-
instance.addEventListener('error',()=>(state.loading|=Errored));
5563+
instance=ownerDocument.createElement('link');
5564+
markNodeAsPendingLoad(instance);
5565+
instance.onload=instance.onerror=clearPendingLoadOnNode.bind(
5566+
null,
5567+
instance,
5568+
);
55555569
setInitialProperties(instance,'link',preloadProps);
55565570
markNodeAsHoistable(instance);
55575571
(ownerDocument.head: any).appendChild(instance);
55585572
}
5573+
// $FlowFixMe: [incompatible-type] -- if instance is an Element it will also be an HTMLLinkElement
5574+
state.preload=instance;
5575+
instance.addEventListener('load',()=>(state.loading|=Loaded));
5576+
instance.addEventListener('error',()=>(state.loading|=Errored));
55595577
}
55605578

55615579
functionpreloadPropsFromStylesheet(

‎packages/react-dom/src/__tests__/ReactDOMFloat-test.js‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3674,6 +3674,110 @@ body {
36743674
);
36753675
});
36763676

3677+
it('does not suspend a transition on a stylesheet whose preload has already loaded',async()=>{
3678+
constroot=ReactDOMClient.createRoot(document);
3679+
root.render(
3680+
<html>
3681+
<body>
3682+
<Suspensefallback="loading...">initial</Suspense>
3683+
</body>
3684+
</html>,
3685+
);
3686+
awaitwaitForAll([]);
3687+
3688+
ReactDOM.preload('route.css',{as: 'style'});
3689+
expect(getMeaningfulChildren(document.head)).toEqual(
3690+
<linkrel="preload"href="route.css"as="style"/>,
3691+
);
3692+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3693+
3694+
loadPreloads(['route.css']);
3695+
assertLog(['load preload: route.css']);
3696+
3697+
React.startTransition(()=>{
3698+
root.render(
3699+
<html>
3700+
<body>
3701+
<Suspensefallback="loading...">
3702+
<linkrel="stylesheet"href="route.css"precedence="default"/>
3703+
next
3704+
</Suspense>
3705+
</body>
3706+
</html>,
3707+
);
3708+
});
3709+
awaitwaitForAll([]);
3710+
3711+
expect(getMeaningfulChildren(document.head)).toEqual([
3712+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3713+
<linkrel="preload"href="route.css"as="style"/>,
3714+
]);
3715+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3716+
3717+
loadStylesheets(['route.css']);
3718+
assertLog(['load stylesheet: route.css']);
3719+
expect(getMeaningfulChildren(document.head)).toEqual([
3720+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3721+
<linkrel="preload"href="route.css"as="style"/>,
3722+
]);
3723+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3724+
});
3725+
3726+
it('suspends a transition on a stylesheet whose preload has not loaded yet',async()=>{
3727+
constroot=ReactDOMClient.createRoot(document);
3728+
root.render(
3729+
<html>
3730+
<body>
3731+
<Suspensefallback="loading...">initial</Suspense>
3732+
</body>
3733+
</html>,
3734+
);
3735+
awaitwaitForAll([]);
3736+
3737+
ReactDOM.preload('route.css',{as: 'style'});
3738+
expect(getMeaningfulChildren(document.head)).toEqual(
3739+
<linkrel="preload"href="route.css"as="style"/>,
3740+
);
3741+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3742+
3743+
React.startTransition(()=>{
3744+
root.render(
3745+
<html>
3746+
<body>
3747+
<Suspensefallback="loading...">
3748+
<linkrel="stylesheet"href="route.css"precedence="default"/>
3749+
next
3750+
</Suspense>
3751+
</body>
3752+
</html>,
3753+
);
3754+
});
3755+
awaitwaitForAll([]);
3756+
3757+
expect(getMeaningfulChildren(document.head)).toEqual(
3758+
<linkrel="preload"href="route.css"as="style"/>,
3759+
);
3760+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3761+
3762+
loadPreloads(['route.css']);
3763+
assertLog(['load preload: route.css']);
3764+
awaitwaitForAll([]);
3765+
expect(getMeaningfulChildren(document.head)).toEqual([
3766+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3767+
<linkrel="preload"href="route.css"as="style"/>,
3768+
]);
3769+
expect(getMeaningfulChildren(document.body)).toEqual('initial');
3770+
3771+
loadStylesheets(['route.css']);
3772+
assertLog(['load stylesheet: route.css']);
3773+
awaitwaitForAll([]);
3774+
expect(getMeaningfulChildren(document.head)).toEqual([
3775+
<linkrel="stylesheet"href="route.css"data-precedence="default"/>,
3776+
<linkrel="preload"href="route.css"as="style"/>,
3777+
]);
3778+
expect(getMeaningfulChildren(document.body)).toEqual('next');
3779+
});
3780+
36773781
it('can suspend commits on more than one root for the same resource at the same time',async()=>{
36783782
document.body.innerHTML='';
36793783
constcontainer1=document.createElement('div');

0 commit comments

Comments
 (0)