Search code examples
versioningbookmarkletetag

javascript versioning without ability to change js url. Would etag work?


As part of a bookmarklet, I want to load in a script www.example.com/a.js This file may change in the future so I want to do some versioning on it. However, since I must hardcode the url in the bookmarklet, I can't use proper url-versioning .

What would be a good practice to do versioning instead? I imagine using etag would probably work. I.e.: update the eTag on each version update and have the client request the file with the If-None-Match header.

Would this work?


Solution

  • have the client request the file with the If-None-Match header.

    That I know of, there is no way to control or set the headers that a browser will use when loading a page resource such as images and script.

    ETag is something that should work "out of the box". If the client sends an ETag header with the file then the browser should automatically send the If-None-Match header for the next request.

    One frequently seen method to ensure a fresh file, although less efficient, is to add a time stamp or random number to the script like http://example.com/script.js?1234567. Of course that will force the client to download the script every time. You could instead use different date strings to force a download only once per day or week or month, etc.

    And then there is just the good old standard caching headers expires and cache-control that you can use as well to force a fresh download every X period of time.