From e2db59509e5491557b18205428374826a8c6bf6d Mon Sep 17 00:00:00 2001 From: Vince Picone Date: Mon, 21 Sep 2020 11:44:45 -0500 Subject: [PATCH] feat: make service worker optional and disabled by default --- packages/gatsby-theme-carbon/gatsby-config.js | 7 +++++-- .../src/components/LeftNav/LeftNavItem.js | 10 +++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/gatsby-theme-carbon/gatsby-config.js b/packages/gatsby-theme-carbon/gatsby-config.js index a9ba2fc5d..8f74245ba 100644 --- a/packages/gatsby-theme-carbon/gatsby-config.js +++ b/packages/gatsby-theme-carbon/gatsby-config.js @@ -23,10 +23,12 @@ module.exports = (themeOptions) => { gatsbyRemarkPlugins = [], remarkPlugins = [], gatsbyPluginSharpOptions = {}, - offline = true, + isServiceWorkerEnabled = false, } = themeOptions; - const optionalPlugins = [offline ? `gatsby-plugin-offline` : undefined]; + const optionalPlugins = [ + isServiceWorkerEnabled ? `gatsby-plugin-offline` : undefined, + ]; if (mediumAccount) { optionalPlugins.push({ @@ -60,6 +62,7 @@ module.exports = (themeOptions) => { siteMetadata: { isSearchEnabled, navigationStyle, + isServiceWorkerEnabled, title: 'Gatsby Theme Carbon', description: 'Add a description by supplying it to siteMetadata in your gatsby-config.js file.', diff --git a/packages/gatsby-theme-carbon/src/components/LeftNav/LeftNavItem.js b/packages/gatsby-theme-carbon/src/components/LeftNav/LeftNavItem.js index 515554b17..f4e1e6b3a 100644 --- a/packages/gatsby-theme-carbon/src/components/LeftNav/LeftNavItem.js +++ b/packages/gatsby-theme-carbon/src/components/LeftNav/LeftNavItem.js @@ -14,19 +14,23 @@ import styles from './LeftNav.module.scss'; import NavContext from '../../util/context/NavContext'; import usePathprefix from '../../util/hooks/usePathprefix'; +import useMetadata from '../../util/hooks/useMetadata'; export const SERVICE_WORKER_UPDATE_FOUND = 'myAppServiceWorkerUpdateFound'; const LeftNavItem = (props) => { const { items, category, hasDivider } = props; const { toggleNavState } = useContext(NavContext); + const { isServiceWorkerEnabled } = useMetadata(); const isOnline = useNetwork(); const handleClick = (event, to) => { toggleNavState('leftNavIsOpen', 'close'); - if (isOnline && window[SERVICE_WORKER_UPDATE_FOUND] === true) { - event.preventDefault(); - window.location.href = to; + if (isServiceWorkerEnabled) { + if (isOnline && window[SERVICE_WORKER_UPDATE_FOUND] === true) { + event.preventDefault(); + window.location.href = to; + } } };