Creating a Dynamic Manifest File
Now that you’re comfortable with how the offline app cache works, let’s apply it to the Kilo example we’ve been working on. Kilo consists of quite a few files and manually listing them all in a manifest file would be a pain. Plus, a single typo would invalidate the entire manifest file and prevent the application from working offline.
To address this issue, we’re going to write a little PHP file that reads the contents of the application directory (and subdirectories) and creates the file list for us. Create a new file in your Kilo directory named manifest.php and add the following code:
<?php header('Content-Type: text/cache-manifest'); echo "CACHE MANIFEST\n"; $dir = new RecursiveDirectoryIterator("."); foreach(new RecursiveIteratorIterator($dir) as $file) { if ($file->IsFile() && $file->getFilename() != "manifest.php" && substr($file->getFilename(), 0, 1) != "." && !strpos($file, DIRECTORY_SEPARATOR . '.')) { $file_name = $file->getPathName(); if (DIRECTORY_SEPARATOR ...
Get Building Android Apps with HTML, CSS, and JavaScript, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.