Home > Enterprise >  static resource versioning - for cache busting
static resource versioning - for cache busting

Time:01-07

I've a html file with bunch of css/js links. How can I version these static resources. Versioning can be file name versioning or adding as a query parameter . I'm not using any server side template, I've html file with js/css links. I don't want to append query param manually in links. Any help will be appreciated.

//in html
  <link rel="stylesheet" href="test1.css">
  <link rel="stylesheet" href="test2.css">
  <link rel="stylesheet" href="test3.css">
  <script src="myscripts.js"></script>
  <script src="myscripts1.js"></script>
  <script src="myscripts2.js"></script>

CodePudding user response:

If you are familiar with versioning please skip to "Suggestion" part. Just wanted to make sure we are on the same page and that we agree where we should use versioning, thank you

It (versioning) depends on the business structure and/or purpose of development!





In general, versioning is almost done in the following ways

Here are the approaches to which versioning is actually useful

  1. Let's say you have created something in which, each of the versions have their own usage (e.g pm2, jenkins or even a language like java).
    It has been used to create a tool or automate a task inside some project and now you have released a new version of it that does not support some features of the previous versions at all (no support for deprecated codes anymore).

    Method: In such scenario, one should always create complete versions of their own product and publish them from the very beginning to the latest and beyond, so that anyone uses it makes sure of it's stability and long supports that may be needed.
    Narrowing it down to major.minor.point release model would be the best practice if your project is considered as agile in its own kind (node.js etc).

pros: Agile projects done the right way and most certainly the only solution for them

cons: Not suitable for small projects with low budgets and definitely an overkill



  1. You are working on a scrum based project (e.g agile multi platform projects like Uber) and therefore the business required you to release a new version every once in a while and also, wants all of the previous versions as well for their own documentation purposes.

    Method: As you only need the latest version to be public and the rest is just for documentations and possibly even a suitable fallback, one can use the major.minor.point release model by only marking each release and using automation techs like Jenkins and Docker for getting the latest codes form a version control and deploy it on orchestrated techs like Kubernetes. You should consult with your devops team on the deployment method which suits best for you. Basically you relay on version control platforms like Git and Github ONLY for documentations. One can actually use all these techs with the way above as well. The point is whether the end user care about the versions or not!

pros: Fast deployment for agile apps with monitoring features

cons: Not suitable for smaller projects with low budgets






Suggestion:

Have you ever used bootstrap 5 along with 4th and 3th version of it, all at the same time?
If so, It is considered as an insufficient move.

If one (let's say Justin!) was trying to achieve some sort of static & local versioning, I would simply suggest him not to as it brings limited offer to what it actually costs.

Instead, Justin can write and evolve his codes to be more modular so that he can use it in multiple places.



If I did not get your question or you thought I could be more of a help, please explain your situation again with more details. (what is your goal and why, what you have done so far and where is the problem)

CodePudding user response:

The problem you are encountering is one of the many reasons why build tools exist. If you were using something like webpack, gulp etc, there are relatively straightforward ways to add the hash of your assets to their url in your html file. However if you cannot use any of the above tools, then you could in theory, write a script that fixes your problem.

It would:

  1. parse out static links for your html.
  2. compute their hashes
  3. create a copy of your project directory
  4. rename the cloned directory's copy of the static assets file to append their hashes
  5. update the new names in the cloned directory's copy of your html file.
  6. serve files from the cloned directory.

For html parsing, most languages have a built-in html parser (but you could use regex at your own risk since it is easier to write and you are the one in charge of your html source code).

I have done all these before though and the truth is it is easy but more stress than it deserves. Instead, you should look into upgrading your build tools/setup.

  • Related