How to prevent unwanted web caching

When you access websites through the browser, caching speeds perceived access since it only actually downloads data for pages and images that seem to have been modified since the last time you accessed them. There is actually caching happening all over the internet between you and the site you are accessing, so occasionally something goes wrong - you don't get the latest version of the data you are accessing.

In How to update modules automatically in VBA, data is retrieved using the xmlHTTP object, and sometimes it does not recognize that data has been updated, and returns the cached version. Here's how gistThat gets over this problem, ( a solution that can be applied to any case you suspect as not refreshing web caches correctly).

URL parameters

Although there are http headers you can tweak to indicate that the response should not come from cache, they seem not to work reliably. Instead - here's a way to use a bogus and random parameter in your URL so that it looks like a different URL to the you previously accessed, thereby forcing the response to be freshly downloaded.

Just pass your URL to this function, and use the modifed one returned by this function instead.

Private Function gtPreventCaching(url As String) As String
    ' this will tweak the url with an extra random parameter to prevent any accidental caching
    Dim p As String
    If (InStr(1, url, "?") > 0) Then
        p = "&"
    Else
        p = "?"
    End If
    gtPreventCaching = url & p & "gtPreventCaching=" & CStr(Rnd())
End Function

For more tips like this see Get Started Snippets

In the meantime why not join our forumfollow the blog or follow me on twitter to ensure you get updates when they are available.  

Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, available All formats are available now from O'Reilly,Amazon and all good bookshops. You can also read a preview on O'Reilly

If you prefer Video style learning I also have two courses available. also published by O'Reilly.
Google Apps Script for Developers and Google Apps Script for Beginners.







Comments