1

In classic ASP, I am telling the browser to cache a certain page for an hour using:

response.expires = 60

This is great, as subsequent page loads for the user within the hour are instantaneous, and without queries to backend databases.

The page allows the user to make changes to the page using Javascript. These changes are infrequent, but when they happen, I'd like that cached page to expire so that a new copy of the page is loaded next time the page is accessed, rather than the cached version that does not have the user's changes.

Is there a way in Javascript to tell the browser the current page has expired, and to clear it from the cache? Alternatively, is there a way in ASP to tell the browser to refresh a cached page even if it has not yet expired?

dangowans
  • 2,263
  • 3
  • 25
  • 40
  • You can call the page with a dummy query-string, e.g `mypage.asp?new=123` that will force browser to download a fresh copy – Yuriy Galanter Mar 21 '14 at 15:07
  • @YuriyGalanter The problem is that all of the pages in the application reference the cached page without the parameter, so I'd cache a new copy with the parameter, but the main page would not be refreshed. – dangowans Mar 21 '14 at 15:11

1 Answers1

1

Here's the best workaround I've found so far.

After doing the changes to the page, I sent a POST request using AJAX to the cached page. In the POST, I put a cache parameter with the current timestamp. At the top of the cached page, I check for the cache parameter, and quit immediately to avoid loading the entire page.

By POSTing to the page, the cache for that page clears. It is an additional request to the server however, so there may be a better answer. It also does not work in Internet Explorer 11, so I disabled caching for IE browsers.

if (request.form("cache") <> "") then
    response.end
end if

' check for IE '

Dim Regex, match
Set Regex = New RegExp
With Regex
    .Pattern = "(MSIE|Trident)"
    .IgnoreCase = True
    .Global = True
End With
match = Regex.test(Request.ServerVariables("HTTP_USER_AGENT"))
If match then
    ' no cache, :( '
else
    response.expires = 60
end if

In my final implementation, rather than passing a dumb timestamp in the POST request, I'm now sending the setting change information. By saving the setting changes as part of the cache-clearing POST request, I was able to avoid doing two separate requests (one to save the changes, and one to clear the cache). I can still see cases for the cache parameter approach, but in my case, all one request works very well for me.

dangowans
  • 2,263
  • 3
  • 25
  • 40