bigopon
bigopon

Reputation: 1964

Why does dynamic import preservers the origin?

Say I have a script from a cdn with something like this:

// bundle.js at https://cdn.com/bundle.js
function loadModule(name) {
  return import(name);
}

I would expect that when including it in my application, I can use it to load my application modules without an absolute URL? because if I don't, it resolves to the origin where the script comes from:

<script src='https://cdn.com/bundle.js'></script>
<script>
  // points to https://cdn.com/app.js instead of myhost.com/app.js
  loadModule('/app.js');
</script>

My question is: Is this a bug or a spec'd behavior? Would be nice if there could be further explanation around this behavior. I'm using Brave browser.

Upvotes: 0

Views: 68

Answers (1)

Tobias Buschor
Tobias Buschor

Reputation: 3305

This

// bundle.js at https://cdn.com/bundle.js
function loadModule(name) {
    return import(name);
}

desugars to

// bundle.js at https://cdn.com/bundle.js
const scriptBaseURL = 'https://cdn.com/';
function loadModule(name) {
    return import(scriptBaseURL+name);
}

Upvotes: 1

Related Questions