| 1 | package Mojolicious::Plugin::ServiceWorker; | ||||||||||||||||||||
| 2 | 1 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 1 | ▶ | use Mojo::JSON; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 1 | our $VERSION = '0.02'; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | my $SW_URL = 'serviceworker.js'; | |||||||||||||||||||
| 8 | 1 | my @COPY_KEYS = qw(debug precache_urls network_only cache_only network_first); | |||||||||||||||||||
| 9 | 1 | my %DEFAULT_LISTENERS = ( | |||||||||||||||||||
| 10 | install => [ <<'EOF' ], | ||||||||||||||||||||
| 11 | event => { | ||||||||||||||||||||
| 12 | console.log("Installing SW..."); | ||||||||||||||||||||
| 13 | event.waitUntil(caches.open(cachename).then(cache => { | ||||||||||||||||||||
| 14 | console.log("Caching: ", config.precache_urls); | ||||||||||||||||||||
| 15 | return cache.addAll(config.precache_urls); | ||||||||||||||||||||
| 16 | }).then(() => console.log("The SW is now installed"))); | ||||||||||||||||||||
| 17 | } | ||||||||||||||||||||
| 18 | EOF | ||||||||||||||||||||
| 19 | fetch => [ <<'EOF' ], | ||||||||||||||||||||
| 20 | event => { | ||||||||||||||||||||
| 21 | var url = event.request.url; | ||||||||||||||||||||
| 22 | if (maybeMatch(config, 'network_only', url)) { | ||||||||||||||||||||
| 23 | if (config.debug) console.log('network_only', url); | ||||||||||||||||||||
| 24 | return event.respondWith(fetch(event.request).catch(() => {})); | ||||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | return caches.open(cachename).then( | ||||||||||||||||||||
| 27 | cache => cache.match(event.request) | ||||||||||||||||||||
| 28 | ).then(cacheResponse => { | ||||||||||||||||||||
| 29 | if (cacheResponse && maybeMatch(config, 'cache_only', url)) { | ||||||||||||||||||||
| 30 | if (config.debug) console.log('cache_only', url); | ||||||||||||||||||||
| 31 | return cacheResponse; | ||||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | if (maybeMatch(config, 'network_first', url)) { | ||||||||||||||||||||
| 34 | if (config.debug) console.log('network_first', url); | ||||||||||||||||||||
| 35 | return cachingFetchOrCached(event.request, cacheResponse); | ||||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | if (config.debug) console.log('cache_first', url); | ||||||||||||||||||||
| 38 | var cF = cachingFetch(event.request).catch(() => {}); | ||||||||||||||||||||
| 39 | return cacheResponse || cF; | ||||||||||||||||||||
| 40 | }); | ||||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | EOF | ||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 2 | ▶ | sub register { | ||||||||||||||||||
|
Subroutine
register: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 46 | 2 | my ($self, $app, $conf) = @_; | |||||||||||||||||||
| 47 | 2 | ▶ | my %config = %{ $conf || {} }; | ||||||||||||||||||
|
Condition50%1/2
$conf || {}
MC/DC0%0/1
$conf || {}
$conf
Truth table50%1/2
A
A= $conf
| |||||||||||||||||||||
| 48 | 2 | ▶ | my $sw_route = $conf->{route_sw} || $SW_URL; | ||||||||||||||||||
|
Condition66%2/3
$conf->{'route_sw'} || $SW_URL
MC/DC0%0/2
$conf->{'route_sw'} || $SW_URL
$conf->{'route_sw'}
$SW_URL
Truth table66%2/3
A || B
A= $conf->{'route_sw'}B= $SW_URL
| |||||||||||||||||||||
| 49 | 2 | my $r = $app->routes; | |||||||||||||||||||
| 50 | 2 | ▶ | $r->get($sw_route => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 51 | 2 | my ($c) = @_; | |||||||||||||||||||
| 52 | 2 | $c->render( | |||||||||||||||||||
| 53 | template => 'serviceworker', | ||||||||||||||||||||
| 54 | format => 'js', | ||||||||||||||||||||
| 55 | listeners => $c->serviceworker->event_listeners, | ||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||
| 57 | 2 | }, 'serviceworker.route'); | |||||||||||||||||||
| 58 | 2 | ▶ | $app->helper('serviceworker.route' => sub { $sw_route }); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 59 | $config{precache_urls} = [ | ||||||||||||||||||||
| 60 | 2 | ▶ | @{ $config{precache_urls} || [] }, | ||||||||||||||||||
|
Condition100%2/2
$config{'precache_urls'} || []
MC/DC100%1/1
$config{'precache_urls'} || []
$config{'precache_urls'}
Truth table100%2/2
A
A= $config{'precache_urls'}
| |||||||||||||||||||||
| 61 | $sw_route, | ||||||||||||||||||||
| 62 | ]; | ||||||||||||||||||||
| 63 | 2 | ▶ | my %config_copy = map {$config{$_} ? ($_ => $config{$_}) : ()} @COPY_KEYS; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 2 | ▶ | $app->helper('serviceworker.config' => sub { \%config_copy }); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 65 | 2 | push @{ $app->renderer->classes }, __PACKAGE__; | |||||||||||||||||||
| 66 | 2 | my %event_listeners = %DEFAULT_LISTENERS; | |||||||||||||||||||
| 67 | 2 | ▶ | $app->helper('serviceworker.event_listeners' => sub { \%event_listeners }); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 68 | 1 | ▶ | $app->helper('serviceworker.add_event_listener' => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 69 | 1 | my ($c, $event, $expr) = @_; | |||||||||||||||||||
| 70 | 1 | ▶ | $event_listeners{$event} = [ @{ $event_listeners{$event} || [] }, $expr ]; | ||||||||||||||||||
|
Condition50%1/2
$event_listeners{$event} || []
MC/DC0%0/1
$event_listeners{$event} || []
$event_listeners{$event}
Truth table50%1/2
A
A= $event_listeners{$event}
| |||||||||||||||||||||
| 71 | 2 | }); | |||||||||||||||||||
| 72 | 2 | $self; | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 1 | 1; | |||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =encoding utf8 | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | =head1 NAME | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | Mojolicious::Plugin::ServiceWorker - plugin to add a Service Worker | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | =head1 SYNOPSIS | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | # Mojolicious::Lite | ||||||||||||||||||||
| 86 | plugin 'ServiceWorker' => { | ||||||||||||||||||||
| 87 | route_sw => '/sw2.js', | ||||||||||||||||||||
| 88 | precache_urls => [ | ||||||||||||||||||||
| 89 | ], | ||||||||||||||||||||
| 90 | }; | ||||||||||||||||||||
| 91 | app->serviceworker->add_event_listener(push => <<'EOF'); | ||||||||||||||||||||
| 92 | function(event) { | ||||||||||||||||||||
| 93 | if (event.data) { | ||||||||||||||||||||
| 94 | console.log('This push event has data: ', event.data.text()); | ||||||||||||||||||||
| 95 | } else { | ||||||||||||||||||||
| 96 | console.log('This push event has no data.'); | ||||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | EOF | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | =head1 DESCRIPTION | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | L<Mojolicious::Plugin::ServiceWorker> is a L<Mojolicious> plugin. | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | =head1 METHODS | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | L<Mojolicious::Plugin::ServiceWorker> inherits all methods from | ||||||||||||||||||||
| 108 | L<Mojolicious::Plugin> and implements the following new ones. | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | =head2 register | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | my $p = $plugin->register(Mojolicious->new, \%conf); | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | Register plugin in L<Mojolicious> application, returning the plugin | ||||||||||||||||||||
| 115 | object. Takes a hash-ref as configuration, see L</OPTIONS> for keys. | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | =head1 OPTIONS | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | =head2 route_sw | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | The service worker route. Defaults to C</serviceworker.js>. Note that | ||||||||||||||||||||
| 122 | you need this to be in your app's top level, since the service worker | ||||||||||||||||||||
| 123 | can only affect URLs at or below its "scope". | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | =head2 debug | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | If a true value, C<console.log> will be used to indicate various events | ||||||||||||||||||||
| 128 | including SW caching choices. | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =head2 precache_urls | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | An array-ref of URLs that are relative to the SW's scope to load into | ||||||||||||||||||||
| 133 | the SW's cache on installation. The SW URL will always be added to this. | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | =head2 network_only | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | An array-ref of URLs. Any fetched URL in this list will never be cached, | ||||||||||||||||||||
| 138 | and always fetched over the network. | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =head2 cache_only | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | As above, except the matching URL will never be re-checked. Use only | ||||||||||||||||||||
| 143 | where you cache-bust by including a hash in the filename. | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =head2 network_first | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | As above, except the matching URL will be fetched from the network | ||||||||||||||||||||
| 148 | every time and used if possible. The cached value will only be used if | ||||||||||||||||||||
| 149 | that fails. | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | B<Any URL not matching these three criteria> will be treated with a | ||||||||||||||||||||
| 152 | "cache first" strategy, also known as "stale while revalidate": the cached | ||||||||||||||||||||
| 153 | version will immediately by returned to the web client for performance, | ||||||||||||||||||||
| 154 | but also fetched over the network and re-cached for freshness. | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | =head1 HELPERS | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | =head2 serviceworker.route | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | my $route_name = $c->serviceworker->route; | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | The configured L</route_sw> route. | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | =head2 serviceworker.config | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | my $config = $c->serviceworker->config; | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | The SW configuration (a hash-ref). Keys: C<debug>, C<precache_urls>, | ||||||||||||||||||||
| 169 | C<network_only>, C<cache_only>, C<network_first>. | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | =head2 serviceworker.add_event_listener | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | my $config = $c->serviceworker->add_event_listener(push => <<'EOF'); | ||||||||||||||||||||
| 174 | function(event) { | ||||||||||||||||||||
| 175 | if (event.data) { | ||||||||||||||||||||
| 176 | console.log('This push event has data: ', event.data.text()); | ||||||||||||||||||||
| 177 | } else { | ||||||||||||||||||||
| 178 | console.log('This push event has no data.'); | ||||||||||||||||||||
| 179 | } | ||||||||||||||||||||
| 180 | } | ||||||||||||||||||||
| 181 | EOF | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | Add to the service worker an event listener. Arguments are the event | ||||||||||||||||||||
| 184 | name, and a JavaScript function expression that takes the correct args | ||||||||||||||||||||
| 185 | for that event. | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | =head2 serviceworker.event_listeners | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | my $listeners = $c->serviceworker->event_listeners; | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | Returns a hash-ref mapping event name to array-ref of function | ||||||||||||||||||||
| 192 | expressions as above. C<install> and C<fetch> are provided by default. | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | =head1 TEMPLATES | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | Various templates are available for including in the app's templates: | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | =head2 serviceworker-install.html.ep | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | A snippet of JavaScript that will install the supplied service | ||||||||||||||||||||
| 201 | worker. Include it within a C<script> element: | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | <script> | ||||||||||||||||||||
| 204 | %= include 'serviceworker-install' | ||||||||||||||||||||
| 205 | </script> | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | =head1 SEE ALSO | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | =cut | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | __DATA__ | ||||||||||||||||||||