0

the problem is that I have a remote js file to include in a page and it has to be included the last version of it. To be more specific: The file is for example: http://example.com/javascript/v03/header.js The problem is that when they generate a new javascript file the url will be changed to http://example.com/javascript/v04/header.js for example.

The URL will be changed to a new version number.

My question is what will be the best/clean way to detect a new version of that file in Php?

Thank you!

  • 1
    If possible, I would recommend doing it the other way around: Get a local copy of the file and only update when you see there is an update and it doesn't break anything on your side. – jeroen Apr 12 '18 at 13:07
  • Why not use version control and have the same name be the most current version? – kchason Apr 12 '18 at 13:08
  • @kchason It seems the external file is not under the OP's control so that is most likely not an option. – jeroen Apr 12 '18 at 13:10
  • @kchason you are right and maybe I can run a cron job daily to check if there is a new version like increment the existing version with 1 and see if the file exists. – Beleiu Iulian Apr 12 '18 at 13:18
  • 4
    When new versions are provided on new URLs it is usually because you **shouldn't** blindly use the latest version of it, but upgrade only after careful consideration and testing. – Quentin Apr 12 '18 at 13:31
  • @Quentin Meh! What could possibly go wrong? `` – Andreas Apr 12 '18 at 13:36
  • @Andreas [This](https://stackoverflow.com/a/1732454/2096401) – TripeHound Apr 12 '18 at 13:37
  • Umm.... That is regex on html, we are talking about running a JavaScript. Also maybe not obvious for everyone, but the joke tag means it was a joke. – Andreas Apr 12 '18 at 13:39
  • Use integrity option as suggested by @bigwolk. Refer the answer [here](https://stackoverflow.com/questions/15479930/how-to-check-the-integrity-of-loaded-javascript-code). – aniket mule Apr 12 '18 at 13:45

1 Answers1

1

Untested but should work.
You file_get_contents the versions and when it returns false you have gone to far.

$link = "http://example.com/javascript/VERSION/header.js";

For($i=1; $i<100; $i++){
    $test= str_replace("VERSION", "v" . str_pad($i, 2, "0", STR_PAD_LEFT), $link);
    If(file_get_contents($test)){
        $latest = $test;
    }else{
        Break;
    }
}

Echo "latest version is " . $latest;

Then optionally you save the $i number somewhere and use that the next time you run it so that you don't need to recheck version 1-4 every time.

EDIT, noticed I had $str instead of $link.

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • Just a note: you should never ever do such thing on page generation. It will slow down page rendering and downtime of `http://example.com` will kill you website too. If you really need this, do it in backround via cron or some queue. – rob006 Apr 13 '18 at 13:21
  • @rob006 I think it can be used if used smart. Such as, you don't need to check it at every page load, once a week may be enough. If it has been checked this week can be saved in a textfile or database. Also I wouldn't want to run the js automatically, maybe make the page email me about the new version. This way it will not use much/any performance and example.com won't notice your pinging. – Andreas Apr 13 '18 at 15:31