How can I use JavaScript to detect if I am on a cached page

chrome check if page is cached
javascript clear cache
html force page to not load from cache
javascript cache
how to check if cache is cleared
how to implement cache in javascript
browser cache javascript
store data in cache javascript

I want to display from cache for a long time and I want a slightly different behavior on page render vs loading the page from cache. Is there an easy way I can determine this with JavaScript?

One way you could do it is to include the time the page was generated in the page and then use some javascript to compare the local time to the time the page was generated. If the time is different by a threshold then the page has come from a cache. The problem with that is if the client machine has its time set incorrectly, although you could get around this by making the client include its current system time in the request to generate the page and then send that value back to the client.

onpageshow Event, When you visit a website in a modern web browser for the first time, Another option that you have is to check the cache directory on the hard  We can do this by setting a cookie on every response from the server, and modifying that cookie using javascript. We can then use this cookie to know whether the page has been loaded from the server or the browser cache. Setting the cookie on every response from the server. We will use a loadedFromBrowserCache cookie to facilitate the cache detection.

I started with the answer "Daniel" gave above but I fear that over a slow connection I could run into some latency issues.

Here is the solution that ultimately worked for me. On the server side I add a cookie refCount and set it's value to 0. On document load in javascript I first check refCount and then increment it. When checking if refCount is greater than 1 I know the page is cached. So for this works like a charm.

Thanks guys for leading me to this solution.

Find out if websites get loaded from cache and how to force reloads , only-if-cached — The browser looks for a matching request in its HTTP cache. Naive stale-while-revalidate client-level implementation. This code will force a page refresh when the javascript detects that this page has been loaded from cache. Although future javascript web page apps should be more elegantly coded to work with page caching, this is a quick fix for pages that I don’t have time to rebuild from scratch.

While this question is already 4 years old. I thought I would add my 2 cents using jQuery and the History plugin.

    $('body').append('<div class="is_cached"></div>');

   if($('.is_cached').length >= 1)
      alert('this page is cached');

When the document is first loaded. A new div.is_cached is appended. There isn't a compatible way to execute javascript when loading a cached page, but you can monitor for history changes. When history changes and the div.is_cached exists, then the user is viewing a cached paged.

Request.cache, The CacheStorage interface represents the storage for Cache objects. On this Page Use CacheStorage.match() to check if a given Request is a key in any of the '/sw-test/app.js', '/sw-test/image-list.js', '/sw-test/star-wars-logo.jpg', always resolves // but in case of success response will have value if  Therefore, cache buster query string can be updated so that the browser doesn't recognize the file in the cache memory and downloads the new file on a refresh of the page. When to use this mechanism Do not do for all .js, .css files in your solution package.

CacheStorage, If you want to use the web server described in this codelab, you need Python Check out the "broken" version of the app that does not have the service worker implemented. Tell the browser to register the JavaScript file as the "service worker". This event is where you want to cache your page assets. To batch add URLs to cache, you can use the addAll method:'test-cache').then(function(cache) { cache.addAll(['/', '/images/logo.png']) .then(function() { // Cached! The addAll method accepts an array of URLs whose URLs and responses should be cached.

Using XmlHttpRequest you can pull up the current page and then examine the http headers of the response.

Best case is to just do a HEAD request and then examine the headers.

For some examples of doing this have a look at

Adding a Service Worker and Offline into your Web App, It's a JavaScript Worker, so it can't access the DOM directly. If all the files are cached successfully, then the service worker While you would use these powers for good, a man-in-the-middle To avoid this, you can only register service workers on pages served Check if we received a valid response Chrome users can load chrome://cache/ instead which lists cached entries directly when loaded in the browser. Last but not least, Windows users can also run a selection of Nirsoft tools to display the browser cache on their system. Forcing reloads. The easiest way to force a reload is to hit the Ctrl-F5 shortcut when that page is open.

Service Workers: an Introduction, While other blog posts and presentations offer more detail on our code caching When V8 compiles a script, the compiled bytecode is stored in a we can give our best tips for improving your website's use of the code caches. most JavaScript engines try to detect them and compile them immediately,  About "Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers. The site was built and is maintained by Alexis Deveria, with occasional updates provided by the web development community.

Code caching for JavaScript developers · V8, In this article, we'll explain the how the browser uses its cache to load pages faster The browser won't have any files cached for the site so it will fetch server but consult its cache for the static assets (JavaScript, CSS, images). We can see the difference cache makes when we refresh the Wikipedia page:. Function Caching, or just Caching means the use of memoization in order to save processing time in our programs. Usually, this means that we have some sort of data structure that saves previous results so we don't compute them again. Since Javascript's functions behave like objects, we can use the functions themselves instead of external resources.

A Web Developer's Guide to Browser Caching, You can find out if you have third party cookies enabled and find instructions to Something went wrong detecting third-party cookies. A "third-party" cookie is set when you are using one website, but that website has embedded javascript to ensure that it doesn't keep using any third party cookies it may have cached. Thanks for your feedback. Server side options are useful too, but that doesn’t mean using a JS method is costly or a mistake. Just look at AngularJS, or Backbone and you’ll see tons of really robust functionality being used as clientside.

  • damn, you beat me to it, thumbs up!
  • I was actually thinking of this solution. I was hoping something easier in the document but maybe it does not exist :)
  • This is not a good answer if you're looking for it to work with any consistency. Between slow load times, server local time vs client local time, or clients with incorrect system dates, you have no way of guaranteeing the veracity of that test.
  • I've used the same approach myself. I wrote up a detailed description on my blog:…
  • Saved my day! I didn't get why increment the cookie, though. Just set it to "firstLoad=yes" and changed to "firstLoad=no" in onLoad. Essentially, the cookie is just a boolean flag that is kept permanently until the page is actually reloaded.
  • which javascript files should I use to test this? I downloaded History plugin but it has alot JS files...which one is working with this?
  • I used this one with jQuery without any trouble.…
  • Note: the IF statement should be >= 1
  • I tried but nothing happens, I linked history js and latest jqeury js... Please check -
  • You are missing the script type for your <script> tag.
  • I was about to suggest the same thing, but the question indicates that he wants to see whether the currently loaded page is cached, if you fire an XHR off, then that would be indicative of the next request, not the current one.
  • Thanks, I cannot use this but, the link you sent me helps me with another issue.