From 3f70d0b5f03cc1711784e914b024305293c14438 Mon Sep 17 00:00:00 2001 From: Eric Jinks Date: Wed, 16 May 2018 10:49:16 +1000 Subject: [PATCH] Add CMS service worker field --- content/settings/global.yml | 1 + public/admin/config.yml | 1 + src/index.js | 12 +++++++++--- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/content/settings/global.yml b/content/settings/global.yml index 16609bd..ebae2d0 100644 --- a/content/settings/global.yml +++ b/content/settings/global.yml @@ -9,3 +9,4 @@ socialMediaCard: twitterSiteAccount: '' headerScripts: |- +enableServiceWorker: false diff --git a/public/admin/config.yml b/public/admin/config.yml index 503a4cb..3e0c7a0 100644 --- a/public/admin/config.yml +++ b/public/admin/config.yml @@ -103,3 +103,4 @@ collections: # A list of collections the CMS should be able to edit - {label: Twitter Site Account, name: twitterSiteAccount, widget: string, required: false} - {label: Twitter Creator Account, name: twitterCreatorAccount, widget: string, required: false} - {label: Header Meta / Scripts, name: headerScripts, widget: text, required: false} + - {label: Enable Service Worker Pre-Cache, name: enableServiceWorker, widget: boolean, required: false} diff --git a/src/index.js b/src/index.js index c2d5f01..55be430 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,8 @@ import { render } from 'react-snapshot' import 'normalize.css' import './globalStyles.css' import App from './App' -import registerServiceWorker from './registerServiceWorker' +import registerServiceWorker, { unregister } from './registerServiceWorker' +import data from './data.json' const rootEl = document.getElementById('root') render(, rootEl) @@ -16,8 +17,13 @@ if (module.hot) { }) } -registerServiceWorker() - if (process.env.REACT_APP_SITE_URL && 'localStorage' in window) { window.localStorage.setItem('netlifySiteURL', process.env.REACT_APP_SITE_URL) } + +const globalSettings = + data.settings && data.settings.filter(doc => doc.name === 'global')[0] + +if (globalSettings) { + globalSettings.enableServiceWorker ? registerServiceWorker() : unregister() +}