Service worker js

This commit is contained in:
Michel 2018-07-04 12:36:46 +02:00
parent 2a37361586
commit bde15f3cf2
7 changed files with 44 additions and 14 deletions

View File

@ -8,13 +8,13 @@ composer require pixelspin/silverstripe-progressivewebapp
``` ```
## Usage ## Usage
Install the module, run dev/build and fill in the settings in the siteconfig - Install the module, run dev/build and fill in the settings in the siteconfig
Create a symlink at the root of you website to vendor/pixelspin/silverstripe-progressivewebapp/service-worker.js - Include the required js to register the service worker
Place the link to the manifest file (<link rel="manifest" href="{$BaseHref}manifest.json">) in the head of your pages and add the color meta data as well (<meta name="theme-color" content="$SiteConfig.ManifestColor">) ```
Include the js (Requirements::javascript('pixelspin/silverstripe-progressivewebapp:resources/js/progressivewebapp.js');) Requirements::javascript('pixelspin/silverstripe-progressivewebapp:resources/js/progressivewebapp.js');
```
## Todo - Add the following tags to the head of your website
- Add "add to homescreen" prompt ```
- Add offline support <meta name="theme-color" content="$SiteConfig.ManifestColor">
- Create an you are offline page <link rel="manifest" href="{$BaseHref}manifest.json">
- Improve documentation ```

View File

@ -4,7 +4,8 @@ Name: progressivewebapp
SilverStripe\Control\Director: SilverStripe\Control\Director:
rules: rules:
'manifest.json': 'Pixelspin\ProgressiveWebApp\Controllers\ProgressiveWebAppController' 'manifest.json': 'Pixelspin\ProgressiveWebApp\Controllers\ManifestController'
'service-worker.js': 'Pixelspin\ProgressiveWebApp\Controllers\ServiceWorkerController'
SilverStripe\SiteConfig\SiteConfig: SilverStripe\SiteConfig\SiteConfig:
extensions: extensions:

View File

@ -1,3 +1,6 @@
if ('serviceWorker' in navigator) { if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js'); var baseHref = (document.getElementsByTagName('base')[0] || {}).href;
if(baseHref){
navigator.serviceWorker.register(baseHref + 'service-worker.js');
}
} }

View File

@ -1 +0,0 @@
self.addEventListener('fetch', (event) => {});

View File

@ -5,7 +5,7 @@ namespace Pixelspin\ProgressiveWebApp\Controllers;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\SiteConfig\SiteConfig; use SilverStripe\SiteConfig\SiteConfig;
class ProgressiveWebAppController extends Controller { class ManifestController extends Controller {
/** /**
* @var array * @var array

View File

@ -0,0 +1,26 @@
<?php
namespace Pixelspin\ProgressiveWebApp\Controllers;
use SilverStripe\Control\Controller;
class ServiceWorkerController extends Controller {
/**
* @var array
*/
private static $allowed_actions = [
'index'
];
/**
* Default controller action for the service-worker.js file
*
* @return mixed
*/
public function index($url) {
$this->getResponse()->addHeader('Content-Type', 'application/javascript; charset="utf-8"');
return $this->renderWith('ServiceWorker');
}
}

View File

@ -0,0 +1 @@
self.addEventListener('fetch', (event) => {});