@@ -47,6 +47,9 @@ describe('ReactFlightDOM', () => {
4747 // condition
4848 jest . resetModules ( ) ;
4949
50+ // Some of the tests pollute the head.
51+ document . head . innerHTML = '' ;
52+
5053 JSDOM = require ( 'jsdom' ) . JSDOM ;
5154
5255 patchSetImmediate ( ) ;
@@ -1998,6 +2001,105 @@ describe('ReactFlightDOM', () => {
19982001 expect ( hintRows . length ) . toEqual ( 6 ) ;
19992002 } ) ;
20002003
2004+ it ( 'preloads resources without needing to render them' , async ( ) => {
2005+ function NoScriptComponent ( ) {
2006+ return (
2007+ < p >
2008+ < img src = "image-do-not-load" />
2009+ < link rel = "stylesheet" href = "css-do-not-load" />
2010+ </ p >
2011+ ) ;
2012+ }
2013+
2014+ function Component ( ) {
2015+ return (
2016+ < div >
2017+ < img src = "image-resource" />
2018+ < img
2019+ src = "image-do-not-load"
2020+ srcSet = "image-preload-src-set"
2021+ sizes = "image-sizes"
2022+ />
2023+ < img src = "image-do-not-load" loading = "lazy" />
2024+ < link
2025+ rel = "preload"
2026+ href = "video-resource"
2027+ as = "video"
2028+ media = "(orientation: landscape)"
2029+ />
2030+ < link rel = "modulepreload" href = "module-resource" />
2031+ < picture >
2032+ < source
2033+ srcSet = "image-not-yet-preloaded"
2034+ media = "(orientation: portrait)"
2035+ />
2036+ < img src = "image-do-not-load" />
2037+ </ picture >
2038+ < noscript >
2039+ < NoScriptComponent />
2040+ </ noscript >
2041+ < link rel = "stylesheet" href = "css-resource" />
2042+ </ div >
2043+ ) ;
2044+ }
2045+
2046+ const { writable, readable} = getTestStream ( ) ;
2047+ const { pipe} = await serverAct ( ( ) =>
2048+ ReactServerDOMServer . renderToPipeableStream ( < Component /> , webpackMap ) ,
2049+ ) ;
2050+ pipe ( writable ) ;
2051+
2052+ let response = null ;
2053+ function getResponse ( ) {
2054+ if ( response === null ) {
2055+ response = ReactServerDOMClient . createFromReadableStream ( readable ) ;
2056+ }
2057+ return response ;
2058+ }
2059+
2060+ function App ( ) {
2061+ // Not rendered but use for its side-effects.
2062+ getResponse ( ) ;
2063+ return (
2064+ < html >
2065+ < body >
2066+ < p > hello world</ p >
2067+ </ body >
2068+ </ html >
2069+ ) ;
2070+ }
2071+
2072+ const root = ReactDOMClient . createRoot ( document ) ;
2073+ await act ( ( ) => {
2074+ root . render ( < App /> ) ;
2075+ } ) ;
2076+
2077+ expect ( getMeaningfulChildren ( document ) ) . toEqual (
2078+ < html >
2079+ < head >
2080+ < link rel = "preload" as = "image" href = "image-resource" />
2081+ < link
2082+ rel = "preload"
2083+ as = "image"
2084+ imagesrcset = "image-preload-src-set"
2085+ imagesizes = "image-sizes"
2086+ />
2087+ < link
2088+ rel = "preload"
2089+ as = "video"
2090+ href = "video-resource"
2091+ media = "(orientation: landscape)"
2092+ />
2093+ < link rel = "modulepreload" href = "module-resource" />
2094+ < link rel = "preload" as = "stylesheet" href = "css-resource" />
2095+ </ head >
2096+ < body >
2097+ < p > hello world</ p >
2098+ </ body >
2099+ </ html > ,
2100+ ) ;
2101+ } ) ;
2102+
20012103 it ( 'should be able to include a client reference in printed errors' , async ( ) => {
20022104 const reportedErrors = [ ] ;
20032105
0 commit comments