Auto-Versioning JavaScript and CSS Files in PHP

Auto-Versioning JavaScript and CSS Files in PHP

Two methods to automatically version JavaScript and CSS files in plain PHP How often have your clients complained to you about not being able to see the new changes you have made to their website? This happens to me a lot, and it seems to be quite a common occurrence for other web developers too.

How often have your clients complained to you about not being able to see the new changes you have made to their website? This happens to me a lot, and it seems to be quite a common occurrence for other web developers too. For example, perhaps I’ve just updated the JavaScript and/or CSS files, but my client’s browser is still using their cached version (preventing them from seeing any of the new changes) and they’re confused as to why they’re looking at an older site.

The most common way to tackle this issue is to version JavaScript and CSS files. Unfortunately for me, however, manually versioning all the files every time that I have to perform a deploy is boring and error-prone. Happily, there is a way around this that this article will dive into, so without further ado let’s explore two ways to automatically version JavaScript and CSS Files in PHP.

Please note: While we are going to look at how to auto-version JavaScript and CSS files, the two following methods could easily be adapted to images (or any other files) as well.

Method 1

This method aims at harnessing the modification time of a JavaScript or CSS file to implement auto-versioning. The idea is to automatically append a query string to each of our files. This query string represents their own version, whose number is determined from their modification time. In so doing, we are tricking the browser into thinking that new files are being specified when, in fact, browsers usually simply look at the full name of each file (meaning that they will be cached accordingly).

The downside of this method is that some browsers may ignore the appended query strings and use the previously cached copy, instead of the new version with the same files. This is why we are going to run through a second method.

Method 2

This method is more complex and aims to automatically append the version of each JavaScript and CSS file directly to their name. Again, the version number is generated using the file modification time. Firstly, we need to add some additional lines to our .htaccess file, since some mod_rewrite rules to allow version numbers in our file names are required:

php javascript version-control tutorial css

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

PHP json_decode Example | Decode JSON Data in PHP Tutorial

PHP json_decode is an inbuilt function that takes the JSON encoded string and converts it into a PHP variable. JSON stands for JavaScript Object Notation.

HTML, CSS, JavaScript Carousel Tutorial Step by Step - JavaScript Tutorials for Beginners

I'm going to be showing you how to create a JavaScript slider step by step. This took me less than 30 minutes to create and all I used to create this JavaScript carousel was HTML, CSS and JavaScript. If you wish to add auto-play to this carousel, look at setInterval where every X amount of time, the carousel will switch slide. So there it is, a carousel created using just JavaScript! I aim this video at beginners! I'll be creating more tutorials based around JavaScript beginners and going back to doing a lot more pure JavaScript videos—not just React!

JavaScript Shopping Cart - Javascript Project for Beginners

JavaScript Shopping Cart - javascript shopping cart tutorial for beginnersBuy me a coffee 🍺 https://www.paypal.com/paypalme/ziddahSource Code: https://bit....

Review Of CodeLobster IDE - Free PHP, HTML, CSS, JavaScript Editor

In this article, we suggest you get acquainted with the free editor of web languages – CodeLobster IDE. It has been present in the software market for a long time already, and it wins a lot of fans. CodeLobster IDE allows you to edit PHP, HTML, CSS and JavaScript files, it highlights the syntax and…

How to create a calculator using javascript - Pure JS tutorials |Web Tutorials

### In this video I will tell you How to create a calculator using javascript very easily. <iframe width="560" height="315" src="https://www.youtube.com/embed/Q8JXnIR80kU" frameborder="0" allow="accelerometer; autoplay; clipboard-write;...