You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
add warnings for non-resources rendered outside body or head (#25532)
Adds some clarifying warnings when you render a component that is almost
a resource but isn't and the element was rendered outside the main
document tree (outside of `<body>` or `<head>`
it('warns if you render resource-like elements above <head> or <body>',async()=>{
286
+
constroot=ReactDOMClient.createRoot(document);
287
+
288
+
renderSafelyAndExpect(
289
+
root,
290
+
<>
291
+
<noscript>foo</noscript>
292
+
<html>
293
+
<body>foo</body>
294
+
</html>
295
+
</>,
296
+
).toErrorDev(
297
+
[
298
+
'Cannot render <noscript> outside the main document. Try moving it into the root <head> tag.',
299
+
'Warning: validateDOMNesting(...): <noscript> cannot appear as a child of <#document>.',
300
+
],
301
+
{withoutStack: 1},
302
+
);
303
+
304
+
renderSafelyAndExpect(
305
+
root,
306
+
<html>
307
+
<template>foo</template>
308
+
<body>foo</body>
309
+
</html>,
310
+
).toErrorDev([
311
+
'Cannot render <template> outside the main document. Try moving it into the root <head> tag.',
312
+
'Warning: validateDOMNesting(...): <template> cannot appear as a child of <html>.',
313
+
]);
314
+
315
+
renderSafelyAndExpect(
316
+
root,
317
+
<html>
318
+
<body>foo</body>
319
+
<style>foo</style>
320
+
</html>,
321
+
).toErrorDev([
322
+
'Cannot render <style> outside the main document. Try moving it into the root <head> tag.',
323
+
'Warning: validateDOMNesting(...): <style> cannot appear as a child of <html>.',
324
+
]);
325
+
326
+
renderSafelyAndExpect(
327
+
root,
328
+
<>
329
+
<html>
330
+
<body>foo</body>
331
+
</html>
332
+
<linkrel="stylesheet"href="foo"/>
333
+
</>,
334
+
).toErrorDev(
335
+
[
336
+
'Cannot render a <link rel="stylesheet" /> outside the main document without knowing its precedence. Consider adding precedence="default" or moving it into the root <head> tag.',
337
+
'Warning: validateDOMNesting(...): <link> cannot appear as a child of <#document>.',
338
+
],
339
+
{withoutStack: 1},
340
+
);
341
+
342
+
renderSafelyAndExpect(
343
+
root,
344
+
<>
345
+
<html>
346
+
<body>foo</body>
347
+
<scripthref="foo"/>
348
+
</html>
349
+
</>,
350
+
).toErrorDev([
351
+
'Cannot render a sync or defer <script> outside the main document without knowing its order. Try adding async="" or moving it into the root <head> tag.',
352
+
'Warning: validateDOMNesting(...): <script> cannot appear as a child of <html>.',
353
+
]);
354
+
355
+
renderSafelyAndExpect(
356
+
root,
357
+
<>
358
+
<html>
359
+
<scriptasync={true}onLoad={()=>{}}href="bar"/>
360
+
<body>foo</body>
361
+
</html>
362
+
</>,
363
+
).toErrorDev([
364
+
'Cannot render a <script> with onLoad or onError listeners outside the main document. Try removing onLoad={...} and onError={...} or moving it into the root <head> tag or somewhere in the <body>.',
365
+
]);
366
+
367
+
renderSafelyAndExpect(
368
+
root,
369
+
<>
370
+
<linkrel="foo"onLoad={()=>{}}href="bar"/>
371
+
<html>
372
+
<body>foo</body>
373
+
</html>
374
+
</>,
375
+
).toErrorDev(
376
+
[
377
+
'Cannot render a <link> with onLoad or onError listeners outside the main document. Try removing onLoad={...} and onError={...} or moving it into the root <head> tag or somewhere in the <body>.',
378
+
],
379
+
{withoutStack: 1},
380
+
);
381
+
});
382
+
271
383
// @gate enableFloat
272
384
it('can acquire a resource after releasing it in the same commit',async()=>{
0 commit comments