1

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.

  • While there don't seem to be many of us using them yet, I think ServiceWorker should probably be a tag – Ben Brocka Oct 12 '17 at 21:03
0

Not quite the solution I was expecting, but I found another plugin that handles Service Worker much better and adds the features I needed, though I have to list posts manually rather than programmatically.

The better plugin is Above The Fold Optimization (ignore the 1 star reviews, it's one of those plugins where if you use it wrong it WILL break your site, definitely not recommended for amateurs though). It has an optional ServiceWorker you can install including precaching and a lot more options than the seemingly abandoned Mozilla plugin. I even find it more useful and faster than Autoptimize at deferring my CSS/Javascript in a more cache-friendly way.

I'm still not entirely sure why my earlier attempt with the other ServiceWorker implementation wasn't working, but for now this does in fact fit my criteria of caching specific posts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.