Skip to content

Commit 06f4826

Browse files
committed
We previously preloaded stylesheets that were rendered in Fizz. The idea was we'd get a headstart fetching these resources since we know they are going to be rendered. However to really be effective non-float stylesheets need to rendered in the head and the preload here is not helpful and potentially hurtful to perf in a minor way. This change removes this functionality to make the code smaller and simpler
1 parent 1cea384 commit 06f4826

File tree

3 files changed

+1
-151
lines changed

3 files changed

+1
-151
lines changed

packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,21 +1960,6 @@ function pushLink(
19601960
}
19611961
}
19621962
}
1963-
let resource = resources.preloadsMap.get(key);
1964-
if (!resource) {
1965-
resource = {
1966-
type: 'preload',
1967-
chunks: ([]: Array<Chunk | PrecomputedChunk>),
1968-
state: NoState,
1969-
props: preloadAsStylePropsFromProps(href, props),
1970-
};
1971-
resources.preloadsMap.set(key, resource);
1972-
if (__DEV__) {
1973-
markAsImplicitResourceDEV(resource, props, resource.props);
1974-
}
1975-
}
1976-
pushLinkImpl(resource.chunks, resource.props);
1977-
resources.usedStylesheets.add(resource);
19781963
return pushLinkImpl(target, props);
19791964
} else {
19801965
// This stylesheet refers to a Resource and we create a new one if necessary
@@ -4246,22 +4231,6 @@ export function writePreamble(
42464231
// Flush unblocked stylesheets by precedence
42474232
resources.precedences.forEach(flushAllStylesInPreamble, destination);
42484233

4249-
resources.usedStylesheets.forEach(resource => {
4250-
const key = getResourceKey(resource.props.as, resource.props.href);
4251-
if (resources.stylesMap.has(key)) {
4252-
// The underlying stylesheet is represented both as a used stylesheet
4253-
// (a regular component we will attempt to preload) and as a StylesheetResource.
4254-
// We don't want to emit two preloads for the same href so we defer
4255-
// the preload rules of the StylesheetResource when there is a conflict
4256-
} else {
4257-
const chunks = resource.chunks;
4258-
for (i = 0; i < chunks.length; i++) {
4259-
writeChunk(destination, chunks[i]);
4260-
}
4261-
}
4262-
});
4263-
resources.usedStylesheets.clear();
4264-
42654234
resources.scripts.forEach(flushResourceInPreamble, destination);
42664235
resources.scripts.clear();
42674236

@@ -4342,22 +4311,6 @@ export function writeHoistables(
43424311
// but we want to kick off preloading as soon as possible
43434312
resources.precedences.forEach(preloadLateStyles, destination);
43444313

4345-
resources.usedStylesheets.forEach(resource => {
4346-
const key = getResourceKey(resource.props.as, resource.props.href);
4347-
if (resources.stylesMap.has(key)) {
4348-
// The underlying stylesheet is represented both as a used stylesheet
4349-
// (a regular component we will attempt to preload) and as a StylesheetResource.
4350-
// We don't want to emit two preloads for the same href so we defer
4351-
// the preload rules of the StylesheetResource when there is a conflict
4352-
} else {
4353-
const chunks = resource.chunks;
4354-
for (i = 0; i < chunks.length; i++) {
4355-
writeChunk(destination, chunks[i]);
4356-
}
4357-
}
4358-
});
4359-
resources.usedStylesheets.clear();
4360-
43614314
resources.scripts.forEach(flushResourceLate, destination);
43624315
resources.scripts.clear();
43634316

@@ -4908,7 +4861,6 @@ export type Resources = {
49084861
// usedImagePreloads: Set<PreloadResource>,
49094862
precedences: Map<string, Set<StyleResource>>,
49104863
stylePrecedences: Map<string, StyleTagResource>,
4911-
usedStylesheets: Set<PreloadResource>,
49124864
scripts: Set<ScriptResource>,
49134865
usedScripts: Set<PreloadResource>,
49144866
explicitStylesheetPreloads: Set<PreloadResource>,
@@ -4936,7 +4888,6 @@ export function createResources(): Resources {
49364888
// usedImagePreloads: new Set(),
49374889
precedences: new Map(),
49384890
stylePrecedences: new Map(),
4939-
usedStylesheets: new Set(),
49404891
scripts: new Set(),
49414892
usedScripts: new Set(),
49424893
explicitStylesheetPreloads: new Set(),

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

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -3879,36 +3879,6 @@ body {
38793879
'Warning: ReactDOM.preload(): The options provided conflict with another call to `ReactDOM.preload("foo", { as: "font", ...})`. React will always use the options it first encounters when preloading a resource for a given `href` and `as` type, and any later options will be ignored if different. Try updating all calls to `ReactDOM.preload()` with the same `href` and `as` type to use the same options, or eliminate one of the calls.\n "crossOrigin" missing from options, original option value: "use-credentials"',
38803880
]);
38813881
});
3882-
3883-
// @gate enableFloat
3884-
it('warns if you pass incompatible options to two `ReactDOM.preload(...)` when an implicit preload already exists with the same href', async () => {
3885-
function Component() {
3886-
ReactDOM.preload('foo', {
3887-
as: 'style',
3888-
crossOrigin: 'use-credentials',
3889-
});
3890-
}
3891-
3892-
await expect(async () => {
3893-
await act(() => {
3894-
renderToPipeableStream(
3895-
<html>
3896-
<body>
3897-
<link
3898-
rel="stylesheet"
3899-
href="foo"
3900-
integrity="some hash"
3901-
media="print"
3902-
/>
3903-
<Component />
3904-
</body>
3905-
</html>,
3906-
);
3907-
});
3908-
}).toErrorDev([
3909-
'ReactDOM.preload(): For `href` "foo", The options provided conflict with props on a matching <link rel="stylesheet" ... /> element. When the preload options disagree with the underlying resource it usually means the browser will not be able to use the preload when the resource is fetched, negating any benefit the preload would provide. React will preload the resource using props derived from the resource instead and ignore the options provided to the `ReactDOM.preload()` call. In general, preloading is useful when you expect to render a resource soon but have not yet done so. In this case since the underlying resource was already rendered the preload call may be extraneous. Try removing the call, otherwise try adjusting both the props on the <link rel="stylesheet" ... /> and the options passed to `ReactDOM.preload()` to agree.\n "integrity" missing from options, underlying prop value: "some hash"\n "media" missing from options, underlying prop value: "print"\n "crossOrigin" option value: "use-credentials", missing from underlying props',
3910-
]);
3911-
});
39123882
});
39133883

39143884
describe('ReactDOM.preinit(href, { as: ... })', () => {
@@ -4503,71 +4473,6 @@ body {
45034473
);
45044474
});
45054475

4506-
// @gate enableFloat
4507-
it('preloads stylesheets without a precedence prop when server rendering', async () => {
4508-
await act(() => {
4509-
const {pipe} = renderToPipeableStream(
4510-
<html>
4511-
<head />
4512-
<body>
4513-
<link rel="stylesheet" href="notaresource" />
4514-
<div>hello world</div>
4515-
</body>
4516-
</html>,
4517-
);
4518-
pipe(writable);
4519-
});
4520-
4521-
expect(getMeaningfulChildren(document)).toEqual(
4522-
<html>
4523-
<head>
4524-
<link rel="preload" as="style" href="notaresource" />
4525-
</head>
4526-
<body>
4527-
<link rel="stylesheet" href="notaresource" />
4528-
<div>hello world</div>
4529-
</body>
4530-
</html>,
4531-
);
4532-
});
4533-
4534-
// @gate enableFloat
4535-
it('respects attributes defined on the stylesheet element when preloading stylesheets during server rendering', async () => {
4536-
await act(() => {
4537-
const {pipe} = renderToPipeableStream(
4538-
<html>
4539-
<head>
4540-
<link rel="stylesheet" fetchPriority="high" href="foo" />
4541-
</head>
4542-
<body>
4543-
<link rel="stylesheet" fetchPriority="low" href="notaresource" />
4544-
<div>hello world</div>
4545-
</body>
4546-
</html>,
4547-
);
4548-
pipe(writable);
4549-
});
4550-
4551-
expect(getMeaningfulChildren(document)).toEqual(
4552-
<html>
4553-
<head>
4554-
<link rel="preload" as="style" fetchpriority="high" href="foo" />
4555-
<link
4556-
rel="preload"
4557-
as="style"
4558-
fetchpriority="low"
4559-
href="notaresource"
4560-
/>
4561-
<link rel="stylesheet" fetchpriority="high" href="foo" />
4562-
</head>
4563-
<body>
4564-
<link rel="stylesheet" fetchpriority="low" href="notaresource" />
4565-
<div>hello world</div>
4566-
</body>
4567-
</html>,
4568-
);
4569-
});
4570-
45714476
// @gate enableFloat
45724477
it('hoists stylesheet resources to the correct precedence', async () => {
45734478
await act(() => {

packages/react-dom/src/__tests__/ReactDOMSingletonComponents-test.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,6 @@ describe('ReactDOM HostSingleton', () => {
245245
expect(getVisibleChildren(document)).toEqual(
246246
<html data-foo="foo">
247247
<head data-bar="bar">
248-
<link rel="preload" href="resource" as="style" />
249-
<link rel="preload" href="3rdparty" as="style" />
250-
<link rel="preload" href="3rdparty2" as="style" />
251248
<title>a server title</title>
252249
<link rel="stylesheet" href="resource" />
253250
<link rel="stylesheet" href="3rdparty" />
@@ -842,10 +839,7 @@ describe('ReactDOM HostSingleton', () => {
842839
await waitForAll([]);
843840
expect(getVisibleChildren(document)).toEqual(
844841
<html>
845-
<head>
846-
<link rel="preload" as="style" href="before" />
847-
<link rel="preload" as="style" href="after" />
848-
</head>
842+
<head />
849843
<body>
850844
<link rel="stylesheet" href="before" />
851845
<link rel="stylesheet" href="after" />

0 commit comments

Comments
 (0)