@@ -57,6 +57,7 @@ const { translators } = require(
57
57
const { getOptionValue } = require ( 'internal/options' ) ;
58
58
const {
59
59
fetchModule,
60
+ inFetchCache,
60
61
} = require ( 'internal/modules/esm/fetch_module' ) ;
61
62
62
63
@@ -338,23 +339,35 @@ class ESMLoader {
338
339
* would have a cache key of https://example.com/foo and baseURL
339
340
* of https://example.com/bar
340
341
*
341
- * MUST BE SYNCHRONOUS for import.meta initialization
342
- * MUST BE CALLED AFTER receiving the url body due to I/O
343
- * @param {string } url
344
- * @returns {string }
342
+ * ! MUST BE SYNCHRONOUS for import.meta initialization
343
+ * ! MUST BE CALLED AFTER receiving the url body due to I/O
344
+ * @param {URL['href'] } url
345
+ * @returns {string|Promise<URL['href']> }
345
346
*/
346
347
getBaseURL ( url ) {
347
- if (
348
+ if ( getOptionValue ( '--experimental-network-imports' ) && (
348
349
StringPrototypeStartsWith ( url , 'http:' ) ||
349
350
StringPrototypeStartsWith ( url , 'https:' )
350
- ) {
351
- // The request & response have already settled, so they are in
352
- // fetchModule's cache, in which case, fetchModule returns
351
+ ) ) {
352
+ // When using network-imports, the request & response have already settled
353
+ // so they are in fetchModule's cache, in which case, fetchModule returns
353
354
// immediately and synchronously
354
- url = fetchModule ( new URL ( url ) , { parentURL : url } ) . resolvedHREF ;
355
- // This should only occur if the module hasn't been fetched yet
356
- if ( typeof url !== 'string' ) { // [2]
357
- throw new ERR_INTERNAL_ASSERTION ( `Base url for module ${ url } not loaded.` ) ;
355
+ // Unless a custom loader bypassed the fetch cache, in which case we just
356
+ // use the original url
357
+ if ( inFetchCache ( url ) ) {
358
+ const module = fetchModule ( new URL ( url ) , { parentURL : url } ) ;
359
+ if ( typeof module ?. resolvedHREF === 'string' ) {
360
+ return module . resolvedHREF ;
361
+ }
362
+ // Internal error
363
+ throw new ERR_INTERNAL_ASSERTION (
364
+ `Base url for module ${ url } not loaded.`
365
+ ) ;
366
+ } else {
367
+ // A custom loader was used instead of network-imports.
368
+ // Adding support for a response URL resolve return in custom loaders is
369
+ // pending.
370
+ return url ;
358
371
}
359
372
}
360
373
return url ;
0 commit comments