Why don't duplicate script tags create duplicate requests?

I have a very simple test case (and 3 hours of search and flu)

<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title>  </title> </head> <body> <script type="text/javascript" src="http://example.com/myscript.js"></script> <script type="text/javascript" src="http://example.com/myscript.js"></script> </body> </html> 

And the server returns the following response headers

 Connection: keep-alive Expires: Mon, 04 Dec 1999 21:29:02 GMT Cache-Control: no-store, no-cache, must-revalidate, max-age=0 Pragma: no-cache 

This code, executed in chrome, issues a single request to the server, also in opera, safari escape.

Is this behavior standard?

Is there official documentation for this behavior?

Is this a cache problem because I thought you would still have a request anyway with a 304 response?

Disclaimer: Please do not offer to randomize or avoid this problem. I want to study this question.

+4
source share
2 answers

There are two reasons why this could happen. Firstly, the content provider can set the expires header to prevent the browser from issuing a second request, and the second how browsers handle GET requests in HTTP, such as a receive request, to enable the script.

1. Installation expires Content header

The content provider can set the expires header so that the script is cached by the browser for the first time, hence the second request is missing. This is a good standard web practice for speeding up web pages, and the expires header is set by the host script server. Yahoo Developer has a good article on this on the Add Expiration or Cache-Control Header , which recommends adding the Expires header to scripts and style sheets in addition to the image.

2. script Get Idempotent method

GET requests, such as an inclusion request to enable javascript on a web page, are safe methods according to the HTTP specification. Safe methods are also idempotent, since multiple queries yield the same result as one query, and that the method is used only to retrieve data. Many browsers use this property of the HTTP specification and do not send multiple requests for idempotent methods. George Cummins explained this well, and an article about it is available from the Mozilla Developer Network .

+2
source

Many HTTP methods (GET, HEAD, PUT, DELETE) are defined as idempotent, which means that multiple identical requests have the same effect as a single request. Browsers take this into account and avoid overhead when sending multiple identical requests where possible.

For more information, see Hypertext Transfer Protocol - HTTP / 1.1 RFC Section 9.1 . For a high-level overview, see the Wikipedia article on HTTP .

+3
source

All Articles