File Coverage

blib/lib/Mojolicious/Plugin/ServiceWorker.pm
Criterion Covered Total %
statement 25 26 96.1
branch 5 8 62.5
condition 1 3 33.3
subroutine 5 6 83.3
pod 1 1 100.0
total 37 44 84.0


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::ServiceWorker;
2 1     1   651 use Mojo::Base 'Mojolicious::Plugin';
  1         2  
  1         7  
3 1     1   182 use Mojo::JSON;
  1         2  
  1         367  
4              
5             our $VERSION = '0.01';
6              
7             my $SW_URL = '/serviceworker.js';
8             my @COPY_KEYS = qw(debug precache_urls network_only cache_only network_first);
9              
10             sub register {
11 1     1 1 38 my ($self, $app, $conf) = @_;
12 1 50       2 my %config = %{ $conf || {} };
  1         6  
13 1   33     4 my $sw_route = $conf->{route_sw} || $SW_URL;
14 1         5 my $r = $app->routes;
15             $r->get($sw_route => sub {
16 1     1   14929 my ($c) = @_;
17 1         8 $c->render(template => 'serviceworker', format => 'js');
18 1         16 }, 'serviceworker.route');
19 1     0   396 $app->helper('serviceworker.route' => sub { $sw_route });
  0         0  
20 1 50       357 if (!$config{serviceworker_route}) {
21             # config only if SW not overridden
22             $config{precache_urls} = [
23 1 50       2 @{ $config{precache_urls} || [] },
  1         4  
24             $sw_route,
25             ];
26             }
27 1 100       2 my %config_copy = map {$config{$_} ? ($_ => $config{$_}) : ()} @COPY_KEYS;
  5         12  
28 1     1   8 $app->helper('serviceworker.config' => sub { \%config_copy });
  1         6710  
29 1         238 push @{ $app->renderer->classes }, __PACKAGE__;
  1         3  
30 1         11 $self;
31             }
32              
33             1;
34              
35             =encoding utf8
36              
37             =head1 NAME
38              
39             Mojolicious::Plugin::ServiceWorker - plugin to add a Service Worker
40              
41             =head1 SYNOPSIS
42              
43             # Mojolicious::Lite
44             plugin 'ServiceWorker' => {
45             route_sw => '/sw2.js',
46             precache_urls => [
47             ],
48             };
49              
50             =head1 DESCRIPTION
51              
52             L is a L plugin.
53              
54             =head1 METHODS
55              
56             L inherits all methods from
57             L and implements the following new ones.
58              
59             =head2 register
60              
61             my $p = $plugin->register(Mojolicious->new, \%conf);
62              
63             Register plugin in L application, returning the plugin
64             object. Takes a hash-ref as configuration, see L for keys.
65              
66             =head1 OPTIONS
67              
68             =head2 route_sw
69              
70             The service worker route. Defaults to C. Note that
71             you need this to be in your app's top level, since the service worker
72             can only affect URLs at or below its "scope".
73              
74             =head2 debug
75              
76             If a true value, C will be used to indicate various events
77             including SW caching choices.
78              
79             =head2 precache_urls
80              
81             An array-ref of URLs (relative is fine) to load into the SW's cache
82             on installation. The SW URL will always be added to this.
83              
84             =head2 network_only
85              
86             An array-ref of URLs. Any fetched URL in this list will never be cached,
87             and always fetched over the network.
88              
89             =head2 cache_only
90              
91             As above, except the matching URL will never be re-checked. Use only
92             where you cache-bust by including a hash in the filename.
93              
94             =head2 network_first
95              
96             As above, except the matching URL will be fetched from the network
97             every time and used if possible. The cached value will only be used if
98             that fails.
99              
100             B will be treated with a
101             "cache first" strategy, also known as "stale while revalidate": the cached
102             version will immediately by returned to the web client for performance,
103             but also fetched over the network and re-cached for freshness.
104              
105             =head1 HELPERS
106              
107             =head2 serviceworker.route
108              
109             my $route_name = $c->serviceworker->route;
110              
111             The configured L route.
112              
113             =head2 serviceworker.config
114              
115             my $config = $c->serviceworker->config;
116              
117             The SW configuration (a hash-ref). Keys: C, C,
118             C, C, C.
119              
120             =head1 TEMPLATES
121              
122             Various templates are available for including in the app's templates:
123              
124             =head2 serviceworker-install.html.ep
125              
126             A snippet of JavaScript that will install the supplied service
127             worker. Include it within a C
132              
133             =head1 SEE ALSO
134              
135             L, L, L.
136              
137             =cut
138              
139             __DATA__