Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: @webdoc/default-template service worker #190

Merged
merged 6 commits into from
Aug 25, 2022

Conversation

ShukantPal
Copy link
Member

@ShukantPal ShukantPal commented Aug 25, 2022

Synopsis

This PR adds a service worker to the sites generated by the default template to

  • cache all core assets (CSS & JavaScript)
  • add support for a offline storage setting (in the new settings page)

It also adds an IndexedDB layer that holds the manifest data. The manifest is used to get all the pages in the site to be cached.

Service Worker

The protocol/ folder in the site source contains the interface b/w the main & service-worker threads.

  • webdocService lets the main application initialize and pass messages to the service worker.
  • webdocDB is a wrapper around IndexedDB for data persistence. The "settings" store holds the application settings and the manifest url & hash.

The service worker accepts 2 types of messages:

  • "lifecycle:init" − This is to be sent whenever the manifest changes (or the site is loaded for the first time). The manifest hash is to be fetched on each page load to check for changes.
  • "trigger:settings" − This lets the service worker know if a setting changed (like offline storage).

Caching

There are 2 types of caches:

  • The main cache is permanent and holds the core assets like react, react-dom scripts & icons that are versioned or won't change.
  • The ephemeral cache. This cache evicts items when the manifest hash changes, which is detected by attaching a "x-manifest-hash" header to cached responses and comparing it to the hash in the settings from IndexedDB (the exact settings object is found by the request origin).

The "offline storage" setting lets the user cache the whole website immediately (well all the pages in the manifest registry, which doesn't include things like README and settings pages). It's an opt-in setting; however, the service worker will cache each HTML page when the user visits them into the ephemeral cache anyway.

IndexedDB

There are 2 object stores in the current database model:

  • "settings" − This holds the application settings (identified by the application name or the site origin). It holds the manifest hash and the manifest URL.
  • "hyperlinks" − This holds all the documentation hyperlinks in the website. Offline storage uses it to identify which pages need to be cached.

UI

Toolbar in explorer & settings page

This toolbar has the new settings page link [button]

Screen Shot 2022-08-25 at 6 37 50 AM

The settings page lets you toggle offline storage:

Screen Shot 2022-08-25 at 6 38 44 AM

Toasts for offline / stale pages

These toasts let the user know if

  • the page was served from their offline cache (i.e. verifying the manifest hash failed)
  • the page is stale (the fetched hash was different from what was in IndexedDB).

Screen Shot 2022-08-25 at 6 33 45 AM

Screen Shot 2022-08-25 at 6 34 18 AM

@ShukantPal ShukantPal marked this pull request as ready for review August 25, 2022 10:48
@ShukantPal ShukantPal self-assigned this Aug 25, 2022
@ShukantPal ShukantPal added the 📦 @webdoc/default-template Issue related to @webdoc/default-template label Aug 25, 2022
@ShukantPal ShukantPal merged commit a75202f into master Aug 25, 2022
@ShukantPal ShukantPal deleted the feature/service-worker branch August 25, 2022 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 @webdoc/default-template Issue related to @webdoc/default-template
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant