I spent part of the afternoon today implementing offline functionality with "Service Workers" for the latest version my Cherry PC business apps.
The linked guide, the toolset they provide, and the example code provided made that very easy to do.
This tech has been moving pretty fast over the past few years, and especially this year, with both browser support and tools to implement Service Workers like "Workbox" and this is really the best one I've run across so far.
Following that guide I basically made a list of the paths to all the files used in my apps (js, css, html) and edited the example code they provide. I was done in about an hour.
I'm sure there's more I can do to fine tune how this works but that's all it took to get my apps running "offline first" and I think that's pretty amazing.
"Workbox" really works.
That's pretty cool! I've been looking into some PWA stuff. I even built a little standalone Chrome/ChromeOS app, which has some similarities.
I was kinda hoping your link would talk about APIs and caching that for offline use. I don't think there's a one-size-fits-all solution to it, but I'd love to have an offline app that keeps and syncs the data in it.
Take a look at the PouchDB.com API. I use that to build my Cherry PC apps and it makes working with both client and server DBs very easy.
The toughest part of building apps like I'm doing with Cherry PC is building CouchDB from source on your server. That's a fairly long process but it's easy going until you get to setting up SSL, and you have to do that for a production app.
I chose to go that route but there are services like https://www.couchbase.com (which I've not looked into yet) that should make that pretty easy.
I have looked into IBM's Cloudant services and you can get started with those for free. I like CouchDB better though.
For the most part, the Cherry PC apps are built with these tools:
server side: (DigitalOcean Ubuntu 16.04)
Apache Web Server
CouchDB
client side:
PouchDB
jQuery
jQueryUI
Bootstrap
The apps run almost entirely in the client, so almost all of the work is done with those client side APIs and those four tools work together so well they are essentially used like a single, very rich, API. You barely have to mess with CouchDB itself, you use PouchDB to work with it, and PouchDB has great docs and example code to get you started with.
PouchDB/CouchdB provide user authentication, server and client side data storage/management (CRUD), Sync on Demand, Live Sync, Replication, Snapshots, Search, and even multi-user management.
"Workbox" lets you point to a toolset like Bootstrap with just the path to "javascripts/bootstrap.min.js" and from there it finds and loads all the associated resources.
So, here's all I did to get my apps running offline:
In my "index.html" I added this at the bottom of the page:
------------------------------------------------------
<script>
// Check that service workers are registered
if ('serviceWorker' in navigator) {
// Use the window load event to keep the page load performant
window.addEventListener('load', () => {
});
}
</script>
--------------------------------------------------------------------------
In "sw.js" (the "serviceWorker" referenced above) I basically have what's shown below. (I actually list all the ".js", ".html", ".css", files I created for use in my apps)
--------------------------------------------------------------------------
console.log('Hello from sw.js v.00.02b');
importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.6.1/workbox-sw.js');
workbox.core.setCacheNameDetails({
prefix: 'CherryPC_Apps',
suffix: 'v.00.02b',
precache: 'cherrypc-precache',
runtime: 'cherrypc-runtime'
});
if (workbox) {
console.log("Yay! Workbox is loaded");
workbox.precaching.precacheAndRoute([
'index.html',
'javascripts/index.html',
'javascripts/pouchdb.js',
'javascripts/pouchdb.find.js',
'javascripts/jquery.js',
'javascripts/jquery-ui.js',
'javascripts/bootstrap.min.js',
'javascripts/mustache.min.js',
'javascripts/my.js',
'stylesheets/my.css',
]);
} else {
console.log("Boo! Workbox didn't load");
}
------------------------------------------------------
Wow, that's a very detailed reply. Thanks a lot :)
I'm going to have to look into CouchDB again. My backend for http://brisaboards.com is so simple, I actually wrote a demo version that stores everything in IndexedDB in the browser, which I imagine PouchDB does automatically. So I basically have an app that functions offline, but only if it's already open since I don't configure the PWA parts, and doesn't sync to my backend automatically. It's also basically a rich client with a "dumb" CRUD-ish backend.
I wanted to use a standard-ish open source stack since the app is open source, but maybe the Postgresql part is unnecessary if CouchDB provides more and can scale from 1 to many servers.
I also use DigitalOcean, and LetsEncrypt + nginx is pretty easy for me.
Thanks again, I'm going to look into Pouch a bit more today!
Wow, that's an awesome app you're creating there.
I'll have to play with it some more but I really do love what you're making there.