|
| 1 | +# Service Worker example |
| 2 | + |
| 3 | +Service worker is an exciting technology that is available in most major browsers. To summarize, it can be used to cache |
| 4 | +assets, providing a positive offline experience. Additionally, service worker has the ability to send notifications that |
| 5 | +are generated both locally from a web application as well as from a remote server. |
| 6 | + |
| 7 | +The example in this crate demonstrates the following features: |
| 8 | + |
| 9 | +1. Use service worker to cache all assets (including the generated wasm file). |
| 10 | +1. Register the service worker. |
| 11 | +1. If the service worker is not yet activated, an even listener will be registered, waiting for the |
| 12 | + state to reach "activated". |
| 13 | +1. When the state reaches "activated", the Notification object will request permission for notifications. |
| 14 | +1. If permission is granted, the PushManager will subscribe to the service using an example vapid key. |
| 15 | +1. Finally, a PushSubscription will be returned, containing the information that can be passed to a |
| 16 | + notification back-end server. |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## Prerequisites - Openssl Installation |
| 21 | + |
| 22 | +The server requires that openssl be installed. For OS X systems, openssl can be installed using [`Homebrew`](https://brew.sh/). |
| 23 | +For windows, openssl can be installed by running the following: |
| 24 | + |
| 25 | +1. `git clone https://github.com/microsoft/vcpkg` |
| 26 | +1. cd vcpkg |
| 27 | +1. ./bootstrap-vcpkg.bat |
| 28 | +1. ./vcpkg integrate install |
| 29 | +1. ./vcpkg install openssl-windows:x64-windows |
| 30 | +1. ./vcpkg install openssl:x64-windows-static-md |
| 31 | + |
| 32 | +## Running |
| 33 | + |
| 34 | +Running this example requires both a server and a client to be running at the same time. This is easily achieved by running each |
| 35 | +in its own terminal from the root of the seed project: |
| 36 | + |
| 37 | +Terminal A: |
| 38 | + |
| 39 | +```bash |
| 40 | +cargo make start_server service_worker |
| 41 | +``` |
| 42 | + |
| 43 | +Terminal B: |
| 44 | + |
| 45 | +```bash |
| 46 | +cargo make start service_worker |
| 47 | +``` |
| 48 | + |
| 49 | +1. Open [http://127.0.0.1:8000/](http://127.0.0.1:8000/) in your browser. This will cache the assets. |
| 50 | +1. Kill the running cargo process to terminate the local dev server. |
| 51 | +1. Refresh the browser and notice that the page loads with all assets. |
| 52 | +1. Click the `Request Push Subscription` button. This will register the subscription with the Push Manager. |
| 53 | +1. Click on the `Send Push Notification` button. A notification should pop up on the browser. |
| 54 | +1. Open Dev Tools and select the `Application` tab. |
| 55 | +1. Click on the `Service Workers` item and view the registered service worker. |
| 56 | +1. In the web page, click the `Unregister Service Worker` button and notice that the service worker is unregistered. |
| 57 | +1. View the `Cache Storage` item in dev tools and take note of the cached items. |
| 58 | +1. In the web page, click the `Clear Cache` button and notice the cache is erased in Dev Tools (Firefox is a bit finicky at showing this). |
0 commit comments