@@ -92,57 +92,60 @@ function preload(
9292 deps ?: string [ ] ,
9393 importerUrl ?: string ,
9494) {
95+ let promise : Promise < unknown > = Promise . resolve ( )
9596 // @ts -expect-error __VITE_IS_MODERN__ will be replaced with boolean later
96- if ( ! __VITE_IS_MODERN__ || ! deps || deps . length === 0 ) {
97- return baseModule ( )
98- }
99-
100- const links = document . getElementsByTagName ( 'link' )
101-
102- return Promise . all (
103- deps . map ( ( dep ) => {
104- // @ts -expect-error assetsURL is declared before preload.toString()
105- dep = assetsURL ( dep , importerUrl )
106- if ( dep in seen ) return
107- seen [ dep ] = true
108- const isCss = dep . endsWith ( '.css' )
109- const cssSelector = isCss ? '[rel="stylesheet"]' : ''
110- const isBaseRelative = ! ! importerUrl
111-
112- // check if the file is already preloaded by SSR markup
113- if ( isBaseRelative ) {
114- // When isBaseRelative is true then we have `importerUrl` and `dep` is
115- // already converted to an absolute URL by the `assetsURL` function
116- for ( let i = links . length - 1 ; i >= 0 ; i -- ) {
117- const link = links [ i ]
118- // The `links[i].href` is an absolute URL thanks to browser doing the work
119- // for us. See https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#reflecting-content-attributes-in-idl-attributes:idl-domstring-5
120- if ( link . href === dep && ( ! isCss || link . rel === 'stylesheet' ) ) {
121- return
97+ if ( __VITE_IS_MODERN__ && deps && deps . length > 0 ) {
98+ const links = document . getElementsByTagName ( 'link' )
99+
100+ promise = Promise . all (
101+ deps . map ( ( dep ) => {
102+ // @ts -expect-error assetsURL is declared before preload.toString()
103+ dep = assetsURL ( dep , importerUrl )
104+ if ( dep in seen ) return
105+ seen [ dep ] = true
106+ const isCss = dep . endsWith ( '.css' )
107+ const cssSelector = isCss ? '[rel="stylesheet"]' : ''
108+ const isBaseRelative = ! ! importerUrl
109+
110+ // check if the file is already preloaded by SSR markup
111+ if ( isBaseRelative ) {
112+ // When isBaseRelative is true then we have `importerUrl` and `dep` is
113+ // already converted to an absolute URL by the `assetsURL` function
114+ for ( let i = links . length - 1 ; i >= 0 ; i -- ) {
115+ const link = links [ i ]
116+ // The `links[i].href` is an absolute URL thanks to browser doing the work
117+ // for us. See https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#reflecting-content-attributes-in-idl-attributes:idl-domstring-5
118+ if ( link . href === dep && ( ! isCss || link . rel === 'stylesheet' ) ) {
119+ return
120+ }
122121 }
122+ } else if (
123+ document . querySelector ( `link[href="${ dep } "]${ cssSelector } ` )
124+ ) {
125+ return
123126 }
124- } else if ( document . querySelector ( `link[href="${ dep } "]${ cssSelector } ` ) ) {
125- return
126- }
127127
128- const link = document . createElement ( 'link' )
129- link . rel = isCss ? 'stylesheet' : scriptRel
130- if ( ! isCss ) {
131- link . as = 'script'
132- link . crossOrigin = ''
133- }
134- link . href = dep
135- document . head . appendChild ( link )
136- if ( isCss ) {
137- return new Promise ( ( res , rej ) => {
138- link . addEventListener ( 'load' , res )
139- link . addEventListener ( 'error' , ( ) =>
140- rej ( new Error ( `Unable to preload CSS for ${ dep } ` ) ) ,
141- )
142- } )
143- }
144- } ) ,
145- )
128+ const link = document . createElement ( 'link' )
129+ link . rel = isCss ? 'stylesheet' : scriptRel
130+ if ( ! isCss ) {
131+ link . as = 'script'
132+ link . crossOrigin = ''
133+ }
134+ link . href = dep
135+ document . head . appendChild ( link )
136+ if ( isCss ) {
137+ return new Promise ( ( res , rej ) => {
138+ link . addEventListener ( 'load' , res )
139+ link . addEventListener ( 'error' , ( ) =>
140+ rej ( new Error ( `Unable to preload CSS for ${ dep } ` ) ) ,
141+ )
142+ } )
143+ }
144+ } ) ,
145+ )
146+ }
147+
148+ return promise
146149 . then ( ( ) => baseModule ( ) )
147150 . catch ( ( err ) => {
148151 const e = new Event ( 'vite:preloadError' , { cancelable : true } )
0 commit comments