Search code examples
javascripthtmlcachingsharepointembed

Always load newest js version from html embed


I have an embed webpart in sharepoint that showcases part of our available content, it's an html file that calls a script.js generated by an excel spreadsheet. The list of content changes often enough that multiple updates are pushed every week, often once a day.

Currently, it reads the .js correctly, but, if the file changes, it only reads the file again on a force reload.

I have tried adding the following code to my HTML: <meta http-equiv="Cache-Control" content="no-cache,no-store, must-revalidate"/> But it didn't change my situation. Is there any way I can make it update without the user force reloading the page? Ideally, only the HTML file would be edited, because the .js is generated automatically.


Solution

  • Add a random number to the URL if you can.

    Depending on how you generate the url in the first place the syntax may vary but something along the lines of

    let script = document.createElement('script');
    script.src = 'my.js?' + Math.random();
    document.body.append(script);
    

    So make a new script that you always load with this code, referencing to your real script (my.js) and load this script instead from your html file and you should be fine.