In the head tag, you can add the code below:
<script type="text/javascript"> var xmlhttp = new XMLHttpRequest(); var url = "http://localhost:4000/getLatestVersion"; //api path to get the latest version xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { var tags = JSON.parse(xmlhttp.responseText); for (var i = 0; i < tags.length; i++) { var tag = document.createElement(tags[i].tag); if (tags[i].tag === 'link') { tag.rel = tags[i].rel; tag.href = tags[i].url; } else { tag.src = tags[i].url; } document.head.appendChild(tag); } } }; xmlhttp.open("POST", url, false); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.send(); </script>
Your api path should allow "CORS" from your site, which processes the above code. And your api should return json data as below:
var latestVersion = '1892471298'; //this can be stored in the database var jsonData = [ { tag: 'link', rel: 'stylesheet', url: 'http://myserver.co/static/main.css?ver=' + latestVersion }, { tag: 'script', rel: '', url: 'http://myserver.co/static/script.js?ver=' + latestVersion } ]; //return jsonData to the client here
source share