1

I've a visualforce page which should allow me to change a logo(basically, an image which is stored as an attachment), and display the modified logo.

In this context, when i replace the existing logo with new one through apex DML, it is updating the new logo in database. But, on page it still shows the old logo, but with new logo's name.

If i refresh the same page after 10 Seconds, it is displaying the modified logo.

this is happening mostly with IE, but momentarily with firefox too

I tried:

  1. Keeping 'Cache=false' attribute to apex:page.
  2. using setRedirect(true) call in apex method during pageredirection to the same page.

But, neither solved my problem. Could someone please help me in knowing what is the issue and remedy for it.

Thanks in advance!

1

This occurs because the browser is caching the image data for a short period of time to be more efficient and minimize the amount of data which is requested.

On the apex:page tag, setting the cache attribute to false and the expires attribute to a value of 0 should cause the browser to immediately expire any data that it receives from the page and your image shouldn't be stored in the local cache and then reused on a subsequent load.

If this behavior isn't observed to be working properly and you still see images retrieved from the local cache, there is another option.

The most common way to negate this browser caching behavior is to use a parameter in the URL which acts as a cache-buster. Salesforce's most visible implementation of a cache buster parameter can be seen in the URL that is generated for a Static Resource, which includes what appears to be a random number. In fact this number isn't random, it is the number of milliseconds elapsed since Jan 1, 1970. It is only in the URL in order to provide uniqueness and ensure that the resource isn't ever retrieved from the web browser's cache.

An example URL generated for a Static Resource holding the jQuery library:

/resource/1371662549000/jQuery/jQuery183/jquery-1.8.3.min.js

You could implement this same cache-busting behavior yourself by adding a unique URL parameter value to your VF page's URL when the redirect takes place. The random parameter value will cause the browser to believe that all of the previously requested data for the page is no longer valid and everything will be retrieved from Salesforce again rather than loading from the browser's cache.

When your controller performs a redirect after the user replaces the image, you can add a random parameter to the URL similar to this:

public PageReference saveImage() {

    // perform the DML to store the user's image as an attachment

    // get a reference to this page
    PageReference pRef = Page.YourPageName;

    // add the URL parameters necessary for the page to operate
    pRef.getParameters().put('id', yourRecordID);

    // add a parameter for the number of milliseconds since 1/1/1970
    pRef.getParameters().put('random', DateTime.now().getTime());
    pRef.setRedirect(true);

    // redirect the user
    return pRef;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.