Mojolicious-Plugin-ServiceWorker
view release on metacpan or search on metacpan
lib/Mojolicious/Plugin/ServiceWorker.pm view on Meta::CPAN
function(event) {
if (event.data) {
console.log('This push event has data: ', event.data.text());
} else {
console.log('This push event has no data.');
}
}
EOF
=head1 DESCRIPTION
L<Mojolicious::Plugin::ServiceWorker> is a L<Mojolicious> plugin.
=head1 METHODS
L<Mojolicious::Plugin::ServiceWorker> inherits all methods from
L<Mojolicious::Plugin> and implements the following new ones.
=head2 register
my $p = $plugin->register(Mojolicious->new, \%conf);
Register plugin in L<Mojolicious> application, returning the plugin
object. Takes a hash-ref as configuration, see L</OPTIONS> for keys.
=head1 OPTIONS
=head2 route_sw
The service worker route. Defaults to C</serviceworker.js>. Note that
you need this to be in your app's top level, since the service worker
can only affect URLs at or below its "scope".
=head2 debug
If a true value, C<console.log> will be used to indicate various events
including SW caching choices.
=head2 precache_urls
An array-ref of URLs that are relative to the SW's scope to load into
the SW's cache on installation. The SW URL will always be added to this.
=head2 network_only
An array-ref of URLs. Any fetched URL in this list will never be cached,
and always fetched over the network.
=head2 cache_only
As above, except the matching URL will never be re-checked. Use only
where you cache-bust by including a hash in the filename.
=head2 network_first
As above, except the matching URL will be fetched from the network
every time and used if possible. The cached value will only be used if
that fails.
B<Any URL not matching these three criteria> will be treated with a
"cache first" strategy, also known as "stale while revalidate": the cached
version will immediately by returned to the web client for performance,
but also fetched over the network and re-cached for freshness.
=head1 HELPERS
=head2 serviceworker.route
my $route_name = $c->serviceworker->route;
The configured L</route_sw> route.
=head2 serviceworker.config
my $config = $c->serviceworker->config;
The SW configuration (a hash-ref). Keys: C<debug>, C<precache_urls>,
C<network_only>, C<cache_only>, C<network_first>.
=head2 serviceworker.add_event_listener
my $config = $c->serviceworker->add_event_listener(push => <<'EOF');
function(event) {
if (event.data) {
console.log('This push event has data: ', event.data.text());
} else {
console.log('This push event has no data.');
}
}
EOF
Add to the service worker an event listener. Arguments are the event
name, and a JavaScript function expression that takes the correct args
for that event.
=head2 serviceworker.event_listeners
my $listeners = $c->serviceworker->event_listeners;
Returns a hash-ref mapping event name to array-ref of function
expressions as above. C<install> and C<fetch> are provided by default.
=head1 TEMPLATES
Various templates are available for including in the app's templates:
=head2 serviceworker-install.html.ep
A snippet of JavaScript that will install the supplied service
worker. Include it within a C<script> element:
<script>
%= include 'serviceworker-install'
</script>
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
=cut
( run in 2.619 seconds using v1.01-cache-2.11-cpan-5837b0d9d2c )