Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching php pages in HTML5 appcache

I am developing an offline application which has a set of .php pages too. Currently I have put those php files in a .appcache manifest files and it works fine. But the issue is, even though I am online, when I try to access a php page, it loads the cached version. What I prefer is a functionality like this,

  • If online - connect to the server and load the up to date information, and overwrite the cached one with new information.
  • If offline - show the last updated static html page.

Here is my .appcache manifest file content

CACHE MANIFEST
#2
taskmanager.php
public/css/bootstrap.css.map
public/css/bootstrap.min.css
public/css/bootstrap-theme.css.map
public/css/bootstrap-theme.min.css
public/css/main.css
public/css/task-manager.css
public/js/app.js
public/js/taskmanager.js
public/js/offlink.js
public/js/jquery-2.1.4.js
public/js/bootstrap.min.js

NETWORK:
*
http://*

What the taskmanager.php do is read tasks from a database and show it. When I cache it like above, it will always show the list of tasks when it was loaded form the first time. Even when I am online, it does not call the database and get the new entries. Instead it is loaded from the cache. So, my solution was to put it in the FALLBACK section as mentioned by the first answer. Even if I put the taskmanager.php file under the FALLBACK section like below,

FALLBACK
taskmanager.php static_taskmanager.php

Now, if I have internet connection, taskmanager.php will run and will show me the latest tasks. But I want to make static_taskmanager in such a way that it will be synchronized with those latest set of tasks. Which means, when the user goes offline, static_taskmanager.php will show the most recent list of tasks which was returned by the taskmanager.php when the user was online). But at the moment it works as a complete static page.

  1. Is it possible to do this?
  2. How can I address this issue?

EDIT

As I understood by searching over SO and Google, one way of achieving this is load the dynamic content using AJAX. But I wonder is it possible to do by using only the manifest file itself.

like image 829
Aruna Tebel Avatar asked Nov 10 '22 08:11

Aruna Tebel


1 Answers

What about using FALLBACK?

FALLBACK

The FALLBACK section tells the browser what to serve when the user tries to access an uncached resource while offline. Because of this, it looks a bit different than CACHE and NETWORK. It contains two values per line, separated by a space. The first value is the request URI to match, and the second is the resource sent upon matching. It caches the resource on the right for offline use, so this should be an explicit path. (http://html5doctor.com/go-offline-with-application-cache/)

FALLBACK:
/main.php /static.php

With this configuration, requests for /main.php will be served unless the user is offline, in which case the user will see the last cached version of /static.php.

Remember that updating the assets on your server will not trigger a cache update. You must modify the manifest file. In that case, you might want to have the manifest updated with a version or timestamp comment when a new version of static.php is created.

# Generated: 2015-07-23 10:34a

Later notes

In this case, I would recommend removing main.php from the cache, or having a static fallback that lets the user know they are looking at older content. The reason is that even if static.php is updated, if the user is off line and doesn't get that update, they will see old content anyway.

If you are seeing performance issues and would like to reduce the load on the server, you could create a static version of main.php using wget, like so:

wget http://example.com/main.php -O main.html

This will store the page content as HTML. Run as a cron job, it could be refreshed every five minutes or so.

The alternative is to update static.php and the appcache each time which would likely cause all the assets to be reloaded - thus defeating the intent of the cache.

like image 182
user2182349 Avatar answered Nov 14 '22 21:11

user2182349