Precaching Wordpress posts with a ServiceWorker
I've been using the Offline Content plugin by Mozilla to implement offline functionality for my site and I've been mostly happy, but it's options for precaching online includes Pages. My most important content, my guides, are in Posts.
The Mozilla plugin seems abandoned so I figured I'd make a modified version of it for my own use. I found the code where Offline Content grabs the pages and feeds them to the serviceworker cache here:
private function get_precache_list() {
$precache_options = $this-options-get('offline_precache');
$precache_list = array();
if ($precache_options['pages']) {
foreach (get_pages() as $page) {
$precache_list[get_page_link($page)] = $page-post_modified;
}
}
return $precache_list;
}
I added the following, hoping to cache my posts tagged Guides
$args = array( 'category_name' = 'Guides', 'numberposts'= 10 );
foreach (get_posts( $args ) as $page) {
$precache_list[get_page_link($page)] = $page-post_modified;
}
It does appear to load the pages on install of the serviceworker; I see them being called in the Network tab with 200 status. But if I go offline and navigate to the Posts that were loaded, they don't work (regular offline browser error shows), while the preloaded Pages do work.
I'll admit I'm not entirely sure if the problem is in how I'm grabbing Wordpress content or the ServiceWorker, but the only difference between working and non-working content seems to be the fact that they're Posts not Pages. Is there some different way I need to precache content to get these posts to work?
I was pretty surprised to find there seems to be almost no other ServiceWorker implementation in the Plugin library. If there's an existing plugin that will do this for me instead I'm open to that, I'd just like to be able to precache these in ServiceWorker without having to change how I post articles; while Pages cache fine, there's no reason to repost this content as a Page simply to cache it.
Topic offline plugin-development Wordpress
Category Web