Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new Image().src="same url" coding twice but the browser just can catch one request

Look at the following code. Chrome logs one request in debug console. If this is because of caching, why does it not log two requests with the last one being 304?

What explains this browser behavior?

<script>
     new Image().src="//stackoverflow.com/"
     new Image().src="//stackoverflow.com/"
</script>
like image 420
sinbar Avatar asked Dec 11 '25 07:12

sinbar


1 Answers

The browser has already downloaded the image, why would it make another request for the same image? If an image is used more than once on a page (which happens regularly), making an individual request for each instance it's used would produce a ton of unnecessary overhead. Two images with the same URL within the same page are assumed to be the same image.

like image 121
deceze Avatar answered Dec 13 '25 21:12

deceze