Home > Mobile >  What happens when a script source is loaded multiple times?
What happens when a script source is loaded multiple times?

Time:01-03

Let's say I load a library multiple times:

<script src="https://unpkg.com/[email protected]"></script>
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://unpkg.com/[email protected]"></script>

I check the Network tab in the devtool and see that it's only loaded once. This makes sense, but I wonder how exactly the engine handles this?

  • It detects duplication and only executes the first line?
  • It executes all the lines, but detects that the same file URL has been visited, so it doesn't download again?
  • It executes all the lines, but detects that the same file name has been downloaded, so it doesn't download again?
  • It executes all the lines, but detects that the same file content has been downloaded, so it doesn't download again?

I read this article, Preventing JavaScript Files from Loading Multiple Times – Michael Kennedy on Technology, and it seems that it will loaded multiple times by default. Am I correct?

CodePudding user response:

All of the major browsers like Firefox, Safari, IE and Opera will cache a Javascript file the first time it is used, and then on subsequent script tags the browser will use the cached copy if it's available and if it hasn't expired. Having said that, caching behavior can usually be altered through browser configuration, so you cannot rely on any kind of caching behavior.

<script src="https://unpkg.com/[email protected]"></script>
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://unpkg.com/[email protected]"></script>

Regarding the execution of the files, even if the files are indeed cached, you may have problems since the same code will be executed five times. At the very least, this will cause the browser to take more time than necessary and it may produce errors, since most JavaScript code isn't written to be executed multiple times. For example, it may attach the same event handlers multiple times.

CodePudding user response:

For your shown code the script https://unpkg.com/[email protected] will be executed 5 times.

Whether it will be downloaded multiple times depends on what is sent as a response for the https://unpkg.com/[email protected] request and on the browser settings.

The server can send ETag or Modification Date headers in combination with caching policy information headers that tell the browser what to be done if the URL is requested another time.

The headers can say that a resource will be valid for a certain time so the browser does not need to do any further requests for that period of time. It however could also say that the browser has to validate the freshness each time using the last received ETag or Modification Date.

  • Related