Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/realityforge/gwt-serviceworker-linker


https://github.com/realityforge/gwt-serviceworker-linker

Last synced: about 2 months ago
JSON representation

Awesome Lists containing this project

README

        

# gwt-serviceworker-linker

[![Build Status](https://api.travis-ci.com/realityforge/gwt-serviceworker-linker.svg?branch=master)](http://travis-ci.com/realityforge/gwt-serviceworker-linker)
[](https://search.maven.org/search?q=g:org.realityforge.gwt.serviceworker)

The [ServiceWorkers](https://www.w3.org/TR/service-workers/) specification enables applications
to take advantage of persistent background processing, including hooks to enable bootstrapping
of web applications while offline. This project attempts to basic AppCache behaviour using
ServiceWorkers.

## Quick Start

The simplest way to serviceworker enable a GWT application is to;

* add the following dependencies into the build system. i.e.

```xml

org.realityforge.gwt.serviceworker
gwt-serviceworker-linker
0.02
provided

```

* add the following snippet into the .gwt.xml file.

```xml

...




```

* launch the service worker from within the application using [Elemental2](https://github.com/google/elemental2).

```java
import static elemental2.dom.DomGlobal.*;

...

if ( null != navigator.serviceWorker )
{
navigator.serviceWorker
.register( GWT.getModuleName() + "-sw.js" )
.then( registration -> {
console.log( "ServiceWorker registration successful with scope: " + registration.getScope() );

// Every minute attempt to update the serviceWorker. If it does update
// then the "controllerchange" event will fire.
DomGlobal.setInterval( v -> registration.update(), 60000 );

return null;
}, error -> {
console.log( "ServiceWorker registration failed: ", error );
return null;
} );

navigator.serviceWorker.addEventListener( "controllerchange", e -> {
// This fires when the service worker controlling this page
// changes, eg a new worker has skipped waiting and become
// the new active worker.
console.log( "ServiceWorker updated ", e );
} );
}
...
```

This should be sufficient to get your application using a serviceworker to cache static assets.