Search code examples
javascriptajaxcachingxmlhttprequestservice-worker

Use cache if server not responding?


Is it possible (I need some resources, since I can not find any) to show link from cache if server not responding?

I would like to use something like this on high traffic server. If server hits bottle neck, or something similar, I would like to show cached version of website/link.

Thank you.


Solution

  • You could use local storage to store JS and CSS. However, local storage has 5Mb (mobile) and 10Mb (desktop) per domain limitation. When one browser visits the site for the first time, JS & CSS are put inline. The JS also has two more jobs:
    1) Store JS & CSS into local storage
    2) Set key/value you will look the next (2nd visit) for plus you should provide a version (for obvious invalidation purposes). This works then after the second time the site is accessed and also if the assets are not in localStorage (for some reason)

    When the browser accesses the site for the second time, app reads the key/value and version if it fits you know the browser already has the related JS & CSS. So the render HTML has inline JS to read JS & CSS from local storage and insert into DOM tree. Read about possible implications: https://www.modpagespeed.com/doc/filter-local-storage-cache
    So on css/js heavy apps you can reduce server load. And be aware Local Storage is not supported with most browsers in private mode over the current session