Does importing a JavaScript module generate a 'get' request to server?

Viewed 239

I'm trying to understand ES6 modules in JavaScript. My understanding is that this generates a get-request to the server to fetch SomeClass.js:

<script src="path/SomeClass.js"></script>

If SomeClass is a module, I can do:

import {SomeClass} from "path/SomeClass.js"

Does this also generate the same request to the server? If I import the same module from two separate files, are two redundant requests being generated? If this is the case, are there good ways to do this more efficiently (e.g. importing in a parent class and then passing it to any children that need it)?

Please correct my understanding if I am way off.

1 Answers

Browsers don't make multiple HTTP requests for the same module even when that module is imported multiple times into a single JS program.

As JS files generally change infrequently, you can use standard cache control HTTP response headers to avoid reloading the modules when the page is revisited too.

Related