Ecommerce software home
Shopping Cart Software Forum for Ecommerce Templates
 
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

Find us on Facebook Follow us on Twitter View our YouTube channel
Search our site
 All Forums
 Technical
 Advanced Tips and Tricks - NO QUESTIONS PLEASE
 CSS and JS auto versioning Stop the Cache madness
Author « Topic »  

dbdave
ECT Moderator

USA
10245 Posts

Posted - 09/15/2022 :  19:07:40  
Hello, more and more, on the forum, there are posts where folks cannot see changes after updating, or buttons do not work.
If everything is in place, and it usually is, then it's almost always an issue where your browser has cached the css and js (javascript) files and refuses to pull in the new changes.

One way to overcome this is to add a revision number to your links to these files.
This works for all of today's browsers and is a good way to force the new changes, as long as you don't mind incrementing the version number when you make changes.

<link rel="stylesheet" href="css/ectcart.css?ver=1">
<script src="js/ectcart.js?ver=1"></script>

***************************************

So I came across this post by Peter over at Servelink (regarding .php) and thought it's quite brilliant.
https://www.ecommercetemplates.com/support/topic.asp?TOPIC_ID=114770&whichpage=2
quote:
On a Unix/Linux server, you can also automate that with something like this:
<link rel="stylesheet" href="css/ectcart.css?ver=<?php echo filemtime('css/ectcart.css'); ?>">
<script src="js/ectcart.js?ver=<?php echo filemtime('js/ectcart.js'); ?>"></script>

which will print the Unix timestamp of when the file was last modified. Update the file and the timestamp will change, forcing the browser to fetch a fresh copy.

I thought about this for .asp and there is no built in function for .asp to handle this, but it is possible to create one.

Keep in mind this is for .asp versions running on a windows server.


In the beginning of the <head> of your pages, just insert the code below and append your css and js links with a small entry similar to the .php example Peter posted.
This code will lookup the timestamp on your files and use that for the revision number. When you upload new versions of these files, users will always get the new version and those frustrating cache issues will be gone forever.

Here is the code you will place in the <head> section of your pages.

<%
'begin css and js file versioning
' instructions and support here - https://www.ecommercetemplates.com/support/topic.asp?TOPIC_ID=114969
'************DO NOT EDIT ANY CODE BELOW************************
dim verf,verfelcs,ververf
Function dgttwo(value, digits)
if digits > len(value) then
dgttwo = String(digits-len(value),"0") & value
else
dgttwo = value
end if
End Function
Function versnno(verfelcs)
set ververf=Server.CreateObject("Scripting.FileSystemObject")
if ververf.FileExists(""& Server.MapPath("\") &"\"&verfelcs&"") then
set verf=Server.CreateObject("Scripting.FileSystemObject").GetFile(""& Server.MapPath("\") &"\"&verfelcs&"")
versnno = Year(verf.DateLastModified)&dgttwo(Month(verf.DateLastModified),2)&dgttwo(Day(verf.DateLastModified),2)
else
versnno = "0000"
end if
set verf=nothing
set ververf=nothing
End Function
'end css and js file versioning
%>


Then you just adjust your css and js links like so

<link rel="stylesheet" href="css/ectcart.css?ver=<%=versnno("css/ectcart.css")%>"/>
<link rel="stylesheet" href="css/style.css?ver=<%=versnno("css/style.css")%>"/>
<script src="js/ectcart.js?ver=<%=versnno("js/ectcart.js")%>"></script>

Just load it up and view source on the page and you should see the date of the file used as the version number.

This code is free to use and not officially supported by ect. However I use it on my very busy website and it's working great and I am here for support if you have questions or issues. It will not be overwritten by updates.

Thanks and enjoy.
David

Edited by - dbdave on 12/17/2022 12:33:31
  « Topic »  
Jump To:
Shopping Cart Software Forum for Ecommerce Templates © 2002-2022 ecommercetemplates.com
This page was generated in 0.03 seconds. Snitz Forums 2000