0

I have dynamic pages that I would like to make available offline. I'd like to use HTML5's application cache to do this. The problem is that I don't know all of the resources (JavaScript and CSS) for the page upfront as these parts are dynamic.

I have a manifest.appcache file that looks like this:

CACHE MANIFEST
#1330475607
CACHE:
/

But this seems to only cache the page's HTML and not the rest of the page's resources. Is there a way to do something like this?

If not, might it be possible to programmatically set the cache with JavaScript (I suppose I could loop through link and script tags)?

1
  • If you are okay to use PHP, this could be done. Commented Apr 19, 2013 at 3:49

3 Answers 3

2

In cache manifest file cache section, every resource file has to be mentioned individually.

So you should generate your cache manifest file dynamically along with the page. This way every time page changes its manifest file is also updated.

The resources in manifest file are downloaded along with the page. Most of the times page's javascript is executed after these downloads complete. So there is no way we can set cache manifest with javascript.

Sign up to request clarification or add additional context in comments.

Comments

0

As far as I know, you can't use a wildcard under the cache section (althoug it works under the failback section) You also can't loop in javascript, since the manifest is loaded before the javascript file.

Is it possible to use a server side script that pack all the js/css in one file ?

Comments

0

If you go old school (not restful) and use url parameters you can do this like so.

I solved this by using local storage (I used the jquery localstorage plugin to help with this).

The process is

  1. Internally from the page when you would normally href from an anchor or redirect, instead call a function to redirects for you. This function stores the parameters from the url to localstorage, and then only redirects to the url without the parameters.
  2. On the receiving target page. Get the parameters from localstorage.

Redirect code

function redirectTo(url) {
    if (url.indexOf('?') === -1) {
        document.location = url;
    } else {
        var params = url.split('?')[1];
        $.localStorage.set("pageparams", params);
        document.location = url.split('?')[0];
    };
}

Target page code

var myParams = GetPageParamsAsJson();
var page = myParams.page;

function GetPageParamsAsJson() {
    return convertUrlParamsToJson($.localStorage.get('pageparams'));
}

function convertUrlParamsToJson(params) {
    if (params) {
        var json = '{"' + decodeURI(params).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}';
        return JSON.parse(json);
    }
    return [];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.