Search code examples
javascriptajaxhtmlbrowser-cachehtml5-appcache

Can I manually add pages to the browser's cache?


My page makes two requests: one to /login and one to /treeContents?rootID=9 for example. I'd like to combine them into one request, /loginAndTreeContents?rootID=9

But, the old way means that subsequent requests to /treeContents?rootID=9 will be retrieved from the cache, and the new way means that they won't.

So I was thinking, is there a way with javascript to manually take the response for /loginAndTreeContents?rootID=9 and jam it into the cache as if it was from /treeContents?rootID=9, so that subsequent requests will just return that?

Is there a way to manually do it with javascript? Or perhaps, can HTML5's appcache help me? Thanks!


Solution

  • No. You cannot make the response from one URL magically be the cached result from another URL. That is not how the cache works. It stores the page in the cache only as the URL that it actually came from.

    It is possible to precache pages by loading them into iframes that are not visible.

    It would also be possible to stuff the desired HTML into LocalStorage and then retrieve it from there with your own JS in some other page.