All Projects → mozilla → wp-sw-manager

mozilla / wp-sw-manager

Licence: GPL-2.0 license
INACTIVE - http://mzl.la/ghe-archive - Service Worker infrastructure for WordPress plugins.

Programming Languages

PHP
23972 projects - #3 most used programming language
javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to wp-sw-manager

sauropod
INACTIVE - http://mzl.la/ghe-archive - Sauropod is a secure storage system for user data.
Stars: ✭ 12 (-72.73%)
Mutual labels:  inactive, unmaintained
soup
INACTIVE - http://mzl.la/ghe-archive - OpenWebApps on Android
Stars: ✭ 12 (-72.73%)
Mutual labels:  inactive, unmaintained
pdf.js-bot
INACTIVE - http://mzl.la/ghe-archive - Cloud test scripts for the pdf.js project
Stars: ✭ 16 (-63.64%)
Mutual labels:  inactive, unmaintained
pymake
INACTIVE - http://mzl.la/ghe-archive - make implementation in Python
Stars: ✭ 79 (+79.55%)
Mutual labels:  inactive, unmaintained
vinz-clortho
INACTIVE - http://mzl.la/ghe-archive - BrowserID Keymaster for LDAP enabled Identity Providers
Stars: ✭ 16 (-63.64%)
Mutual labels:  inactive, unmaintained
openbadges-specification
INACTIVE - http://mzl.la/ghe-archive - Specs related to Open Badges
Stars: ✭ 23 (-47.73%)
Mutual labels:  inactive, unmaintained
layerscope
INACTIVE - http://mzl.la/ghe-archive - LayerScope Viewer
Stars: ✭ 16 (-63.64%)
Mutual labels:  inactive, unmaintained
mortar-layouts
INACTIVE - http://mzl.la/ghe-archive - A small library for constructing app UIs with backbone.js.
Stars: ✭ 19 (-56.82%)
Mutual labels:  inactive, unmaintained
calculator
INACTIVE - http://mzl.la/ghe-archive - Resurrection of the calculator app that was pulled from gaia
Stars: ✭ 16 (-63.64%)
Mutual labels:  inactive, unmaintained
icongrid
INACTIVE - http://mzl.la/ghe-archive - IconGrid.js makes it easy to display a scrollable grid of icons!
Stars: ✭ 25 (-43.18%)
Mutual labels:  inactive, unmaintained
learning-networks
INACTIVE - http://mzl.la/ghe-archive - This repo is for tracking initiatives of the Mozilla Learning Networks team.
Stars: ✭ 12 (-72.73%)
Mutual labels:  inactive, unmaintained
Garmr
INACTIVE - Security Testing Tool
Stars: ✭ 105 (+138.64%)
Mutual labels:  inactive, unmaintained
webdev-bootcamp
INACTIVE - http://mzl.la/ghe-archive - How to be a Web developer at Mozilla
Stars: ✭ 57 (+29.55%)
Mutual labels:  inactive, unmaintained
wsoh
INACTIVE - http://mzl.la/ghe-archive - World Series of Hack
Stars: ✭ 44 (+0%)
Mutual labels:  inactive, unmaintained
fx-share-addon
INACTIVE - http://mzl.la/ghe-archive - new addon version from fx-share branch
Stars: ✭ 13 (-70.45%)
Mutual labels:  inactive, unmaintained
Campus-Program
INACTIVE - http://mzl.la/ghe-archive - Rocking out the campus campaign!
Stars: ✭ 21 (-52.27%)
Mutual labels:  inactive, unmaintained
friendlycode
INACTIVE - http://mzl.la/ghe-archive - World's friendliest HTML editor.
Stars: ✭ 47 (+6.82%)
Mutual labels:  inactive, unmaintained
cleopatra
INACTIVE - http://mzl.la/ghe-archive - UI for the gecko profiler
Stars: ✭ 26 (-40.91%)
Mutual labels:  inactive, unmaintained
build-relengapi
INACTIVE - http://mzl.la/ghe-archive - Your Interface to Release Engineering Automation -
Stars: ✭ 14 (-68.18%)
Mutual labels:  inactive, unmaintained
id-specs
INACTIVE - http://mzl.la/ghe-archive - Specifications for Mozilla's Identity Effort
Stars: ✭ 91 (+106.82%)
Mutual labels:  inactive, unmaintained

wp-sw-manager

Service Worker infrastructure for WordPress plugins.

Motivation

Service Workers enable web applications to send push notifications, work offline or perform background tasks periodically. Currently the standard only allows one service worker per scope making it hard for plugin developers to combine different focused and isolated functionality.

The WP_SW_Manager library provides a collaborative way to generate service workers. It is as simple as registering a callback for writing your service worker functionality:

include_once(plugins_url(__FILE__) . /vendor/mozilla/wp-sw-manager);

WP_SW_Manager::get_manager()->sw()->add_content(write_sw);

function write_sw() {
    echo 'console.log("Here is my plugin!")';
}

Installation

Add this entry to your composer.json file:

"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/mozilla/wp-sw-manager"
    }
],
"require": {
    "mozilla/wp-sw-manager": "dev-master"
}

Usage

First, include the library and get the manager:

include_once(plugins_url(__FILE__) . /vendor/mozilla/wp-sw-manager);
$swmgr = WP_SW_Manager::get_manager();

Adding your functionality

A service worker is identified with the scope at which it will be registered. Select the proper service worker with:

$swmgr->sw('/scope/path');

If you omit the scope parameter, it will default to home_url('/', 'relative').

To add your content to the service worker you use:

$swmgr->sw()->add_content(write_sw);

function write_sw() {
    echo 'console.log("Here is my plugin!")';
}

You can pass an array instead to deal with class or instance methods:

$swmgr->sw()->add_content(array($this, 'write_sw'));

public function write_sw() {
    echo 'console.log("Here is my plugin!")';
}

If you have a file with the contents you want to add, you can include it when generating the code:

$swmgr->sw()->add_content(array($this, 'write_sw'));

public function write_sw() {
    $message = 'Welcome to my plugin!';
    include('path/to/my-sw-functionality.js')
}
// path/to/my-sw-functionality.js
console.log('<?php echo $message; ?>');

Add your content generators always at load time, before WordPress init action.

Writing service workers

When writing your own service worker functionality, the last form from above is preferred to improve maintainability.

It is strongly recommended you enclose your functionality inside an IIFE and try to not pollute the global namespace. A good template could be:

(function(self) {
    // here goes my functionality
})(self);

Storage

Since version 0.3.0 the library embeds localForage JavaScript library which enables easy access to IndexedDB through service workers. To avoid name collisions you should create your own namespace with:

// contents for your service worker
(function(self, localforage) {
  var store = localforage.createInstance({ name: '__my_wp_plugin' });
})(self, localforage);

Accessing service worker registration

It is possible you want to add some client code dependent on the service worker registration. To do this register your script indicating a dependency with WP_SW_Manager::SW_REGISTRAR_SCRIPT.

wp_register_script('my-plugin-script', '/path/to/my/plugin/script.js', array(WP_SW_Manager::SW_REGISTRAR_SCRIPT));

Your script will be added after the code to register the service workers.

To access the registration promise, use the $swRegistration object. This object contains service worker registrations per unique key. Although this key is currently the scope of the service worker, this could change in the future so you should not rely on this assumption. Instead, you should retrieve the unique key in PHP and pass to the client JavaScript with a localized script.

wp_register_script('my-plugin-script', '/path/to/my/plugin/script.js', array(WP_SW_Manager::SW_REGISTRAR_SCRIPT));
wp_localize_script('my-plugin-script', 'ServiceWorker', array('key' => WP_SW_Manager::get_js_id()));
wp_enqueue_script('my-plugin-script');

And in the client code:

$swRegistrations[ServiceWorker.key]
.then(registration => console.log('Success:', registration))
.catch(error => console.error('Error:', error));
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].