1

So I have a third party javascript library that my customers inject into their site, and I want to make sure that their visitors always get the latest version of my script.

My customers shouldnt have to change their implementation, so forcing them to attach a version as a query parameter (example.com/mylibrary.js?v=3) is not acceptable.

Not caching is also not acceptable

https://example.com/MyLibrary.js

This library interacts heavily with my api on https://example.com, so sometimes after a deploy with changes to both the api and js library, the old script version isn't compatible anymore. So users with cached versions will not get the latest version of the javascript immediately, which can cause the application to break.

My customers have heavily visited sites, so I'm using cloudflare to hard cache https://example.com/MyLibrary.js and I want to minimize load on my servers while having a fully automated process of versioning.

The best plan I have so far is to make my webpack build process create a https://example.com/version.txt that includes the latest version of the script, and to rename the library output file to MyLibrary.{versionnumber}.js.

the MyLibrary.js that my customers inject will then just contain code to check the version.txt and then inject a script tag with MyLibrary.{versionnumber}.js.

Everything will be hardcached and version.txt cache purged on every deploy.

Is this impossible ? Stupid ? Is there a better way ? How does google do it with all their scripts ?

user3621898
  • 560
  • 3
  • 21
  • 1
    Possible duplicate of [How can I force clients to refresh JavaScript files?](http://stackoverflow.com/questions/32414/how-can-i-force-clients-to-refresh-javascript-files) – Chad May 11 '17 at 20:47
  • My customers shouldnt have to change their implementation, so forcing them to attach a version as a query parameter (example.com/mylibrary.js?v=3) is not acceptable. – user3621898 May 11 '17 at 20:50
  • Not caching it is also out of the question since my customers have heavily visited sites that would crash mine if I didnt cache. – user3621898 May 11 '17 at 20:54
  • Cloudflare supports ETag by default: https://support.cloudflare.com/hc/en-us/articles/218505467-Does-Cloudflare-support-ETag-headers- – Justin Heath May 11 '17 at 20:54
  • @JustinHeath hmm, We dont have enterprise though, but I will do some testing on the "weak e-tag" feature and check how big changes are needed for it to return the new version. As it doesnt seem to trigger on tiny changes I'm not sure its enough. Thanks – user3621898 May 11 '17 at 21:01
  • Make bigger changes ;-) – Justin Heath May 11 '17 at 21:07

1 Answers1

0

I'd recommend using semantic versioning. Your API should have a version number as part of its url, or requests made to it should specify which major version of the API they want to use. When you make a breaking change to the API, you create a new major version that runs alongside the previous one.

When it comes to a JS file that calls your API, perhaps you should have the major version as part of the URL to access it. Then, the server has to send appropriate headers that tell the client it needs to fetch the new minor version and ignore cache when there's been an update. How you do that depends on your backend.

In other words, clients won't have to change their implementations as long as the changes don't break previous code, but if there's a breaking change, the old file will still work with the old API. You can chose to stop supporting older API versions when you see nobody uses them anymore.

Domino
  • 4,988
  • 27
  • 51