Skip to content

Commit 5128af2

Browse files
committed
warn if <style> tag href has a space on server
1 parent 0008afb commit 5128af2

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,15 @@ function pushStyle(
15811581
return pushStyleImpl(target, props);
15821582
}
15831583

1584+
if (__DEV__) {
1585+
if (href.includes(' ')) {
1586+
console.error(
1587+
'React expected the `href` prop for a <style> tag opting into hoisting semantics using the `precedence` prop to not have any spaces but ecountered spaces instead. using spaces in this prop will cause hydration of this style to fail on the client. The href for the <style> where this ocurred is "%s".',
1588+
href,
1589+
);
1590+
}
1591+
}
1592+
15841593
const key = getResourceKey('style', href);
15851594
let resource = resources.stylesMap.get(key);
15861595
if (!resource) {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3492,6 +3492,7 @@ body {
34923492
<div>1</div>,
34933493
]);
34943494
});
3495+
34953496
// @gate enableFloat
34963497
it('escapes hrefs when selecting matching elements in the document when rendering Resources', async () => {
34973498
function App() {
@@ -4424,6 +4425,24 @@ background-color: green;
44244425
</html>,
44254426
);
44264427
});
4428+
4429+
it('warns if you render a <style> with an href with a space on the server', async () => {
4430+
await expect(async () => {
4431+
await actIntoEmptyDocument(() => {
4432+
renderToPipeableStream(
4433+
<html>
4434+
<body>
4435+
<style href="foo bar" precedence="default">
4436+
style
4437+
</style>
4438+
</body>
4439+
</html>,
4440+
).pipe(writable);
4441+
});
4442+
}).toErrorDev(
4443+
'React expected the `href` prop for a <style> tag opting into hoisting semantics using the `precedence` prop to not have any spaces but ecountered spaces instead. using spaces in this prop will cause hydration of this style to fail on the client. The href for the <style> where this ocurred is "foo bar".',
4444+
);
4445+
});
44274446
});
44284447

44294448
describe('Script Resources', () => {

0 commit comments

Comments
 (0)