1

I have searched it a lot on google but didn't get any answer for the same. In order to avoid caching of js and css files we can append a version number after the string like i have done below.

<script type="text/javascript"
    src="<c:url value='/resources/js/Invoice.js?version=1.0'/>">
</script>

But I wish to use some random number instead of 1.0 to avoid caching something like :

<script type="text/javascript"
        src="<c:url value='/resources/js/Invoice.js?version=<%=Math.random()%>' />">
    </script>

But its not working at all. Please help me out

6
  • are you using any properties file in your project like for localized messages? Commented Oct 20, 2016 at 8:43
  • yes I am using one Commented Oct 20, 2016 at 8:44
  • And what technology are you using at server side? Commented Oct 20, 2016 at 8:45
  • java Spring MVC Commented Oct 20, 2016 at 8:47
  • Why not use <%= java.lang.Math.round(java.lang.Math.random() * 100) %> this it self? Commented Oct 20, 2016 at 9:01

2 Answers 2

1

For storing build numbers I think properties files is the best place. And for your problem, you can get this build number using Spring tag library which can be included like this,

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

Updated

You can then get the build number from properties file which is configured in your ApplicationServlet.xml and finally can use build number as follows,

<spring:message code="buildNumber" var="buildNumber" />
<link rel="stylesheet" href="<c:url value="/resources/css/custom/select2.css?${buildNumber}"/>" type="text/css" />

Your applicationServlet-servlet.xml file must include something like this,

<beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <beans:property name="basenames">
        <beans:list>
            <beans:value>buildNumber</beans:value>
            <beans:value>gui</beans:value>
            <beans:value>message</beans:value>              
        </beans:list>
    </beans:property>
</beans:bean>

Here you can store your build number in separate properties file named, buildNumber.properties and having entry like this, buildNumber=601.

You can change the build number anytime you want to deploy your latest code to the server.

With this you can ensure that your client won't need to clear his own browsers cache whenever a new version of your application has deployed.

Sign up to request clarification or add additional context in comments.

4 Comments

How is this going to return a random number each time page is loaded?
This is not going to return a random number, but it will help your client to get the latest changes after each deployment. If you just want to worry about your own browser cache, then you can use some browser plugins like clear cache. But above solution is for application level purpose.
Yeah I got it... But where do i need to put the following code <spring:message code="buildNumber" var="buildNumber" />
@Ashwin Sharma just before you include the js or css files, check the updated answer.
0

Please use this key value in your property file, but I am not sure about your scenario, this has worked for me. browser.cache.disabled=true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.