0

Right now I'm having to change a js file more often than not. cache problems on half the users. Next solution, to add a

?v=x 

behind. Sure, why not. But of course I almost always forgot to change that number as well.

So I came up with this solution.

<script src="js/main.js?v=<?php echo md5_file('js/main.js'); ?>"></script>

Is md5_file too expensive? Right now it's working like a charm but I don't really know if on bigger websites this would be very bad for the server.

Any thoughts or alternative solutions?

monxas
  • 2,475
  • 2
  • 20
  • 36
  • Instead using md5, why not use a random number? http://stackoverflow.com/a/7413275/2026740 – Daniel Corzo Jan 05 '17 at 17:02
  • @DanielCorzo Th thing is I always forget to change that number after I change it so I start again having cache problems. This way I can forget about it. – monxas Jan 05 '17 at 17:06

1 Answers1

0

Can you use the date the file was last modified? Or if you don't mind it never caching you could use a random number.

Bastiaanus
  • 357
  • 1
  • 10
  • Yeah, I'm trying to keep caching functionality. Can I get last modified date from a file in php? would it be cheaper than using md5_file? – monxas Jan 05 '17 at 17:08
  • @monxas maybe you can use [this](http://php.net/manual/en/function.filemtime.php). My guess is that it'd be cheaper than hashing the whole file, to make sure you could write a simple test script that measures the execution time of both. – Bastiaanus Jan 06 '17 at 14:52