1

So, I wanted to avoid the cache to see images I'm uploading to this website I'm working on, which I access through localhost.

I hit a handy solution pretty quick:

Attaching to the end of image's path ?random= and a random number generated by Math.floor(). All this with the help of a script.

The HTML showed in the browser:

<img class="gallery_img" src="./uploads/4.jpg?random=172">

Awesome, it works beautifully.

Now, the thing that has been hard to find is an explanation of why and how this actually works.

I mean, 1) how the browser still finds the image in the server with this new path?

Or asked the other way around, 2) how the server handles this ?random=172 attachment and delivers the image asked before that?

If somebody could point me to the right direction I would be grateful.


Solution:

Have a look on what are query parameters.

And if you are dealing with the problem of avoiding the cache, have a look here:

Disable cache for some images

PuikPuiker
  • 13
  • 4
  • 2
    You can attach query parameters to virtually any arbitrary URL, and unless they have a specific meaning to the server, or the server is extremely strict and actually validates query parameters, they will have no impact on the request. This fact is used by Google Analytics and similar trackers to add tracking information to URLs which are used in the browser, but don't have any impact on the server. – deceze Sep 26 '22 at 08:06
  • Now I get it. I wasn't aware of this "query parameter" thing. Now it is clear. Thanks. – PuikPuiker Sep 26 '22 at 08:20

1 Answers1

1

That is not part of the path, that is a query parameter.

And most of the times the server of images does not care about query parameters, at least not a literally random one, maybe it would support a query parameter called width or size and dynamically generate an image of the correct size. But random is most likely simply discarded and the image is served normally.

luk2302
  • 55,258
  • 23
  • 97
  • 137