diff --git a/web/package.json b/web/package.json index 83ac8035..2ba46b83 100644 --- a/web/package.json +++ b/web/package.json @@ -31,7 +31,19 @@ "typescript": "^4.5.5", "util": "^0.12.4", "uuid": "^3.4.0", - "web-vitals": "^2.1.4" + "web-vitals": "^2.1.4", + "workbox-background-sync": "^5.1.4", + "workbox-broadcast-update": "^5.1.4", + "workbox-cacheable-response": "^5.1.4", + "workbox-core": "^5.1.4", + "workbox-expiration": "^5.1.4", + "workbox-google-analytics": "^5.1.4", + "workbox-navigation-preload": "^5.1.4", + "workbox-precaching": "^5.1.4", + "workbox-range-requests": "^5.1.4", + "workbox-routing": "^5.1.4", + "workbox-strategies": "^5.1.4", + "workbox-streams": "^5.1.4" }, "scripts": { "start": "GENERATE_SOURCEMAP=true craco start", @@ -60,6 +72,7 @@ ] }, "devDependencies": { + "@types/node": "^17.0.16", "eslint": "^8.8.0", "eslint-config-react-app": "^7.0.0", "eslint-plugin-flowtype": "^8.0.3", diff --git a/web/src/components/core/Header.tsx b/web/src/components/core/Header.tsx index 86b56654..af486777 100644 --- a/web/src/components/core/Header.tsx +++ b/web/src/components/core/Header.tsx @@ -1,12 +1,10 @@ import React from 'react'; -import { getTheme, MessageBarButton } from '@fluentui/react'; +import { getTheme } from '@fluentui/react'; import { CommandBar, ICommandBarItemProps } from '@fluentui/react/lib/CommandBar'; -import { MessageBar, MessageBarType } from '@fluentui/react/lib/MessageBar'; import SettingsModal, { SettingsChanges } from '~/components/settings/SettingsModal'; import AboutModal from '~/components/modals/AboutModal'; import config from '~/services/config'; -import api from '~/services/api'; import { getSnippetsMenuItems, SnippetMenuItem } from '~/utils/headerutils'; import ChangeLogModal from '~/components/modals/ChangeLogModal'; import SharePopup from '~/components/utils/SharePopup'; @@ -34,7 +32,6 @@ interface HeaderState { showAbout?: boolean showChangelog?: boolean loading?: boolean - showUpdateBanner?: boolean showShareMessage?: boolean } @@ -61,9 +58,7 @@ export class Header extends React.Component { showAbout: false, showChangelog: false, loading: false, - showUpdateBanner: false, - // showShareMessage: false - showShareMessage: true + showShareMessage: false }; } @@ -73,13 +68,6 @@ export class Header extends React.Component { fileElement.accept = '.go'; fileElement.addEventListener('change', () => this.onItemSelect(), false); this.fileInput = fileElement; - - // show update popover - api.getVersion().then(r => { - const { version } = r; - if (!version) return; - this.setState({ showUpdateBanner: version !== config.appVersion }); - }).catch(err => console.warn('failed to check server API version: ', err)); } onItemSelect() { @@ -249,23 +237,6 @@ export class Header extends React.Component { const { snippetName } = this.props; return
- this.setState({ showUpdateBanner: false })} - dismissButtonAriaLabel="Close" - isMultiline={false} - actions={ -
- { - this.setState({ showUpdateBanner: false }); - config.forceRefreshPage(); - }}>Action -
- } - > - Web application was updated, click Reload to apply changes -
, document.getElementById('root')); // If you want your app to work offline and load faster, you can change // unregister() to register() below. Note this comes with some pitfalls. -// Learn more about service workers: https://bit.ly/CRA-PWA -serviceWorker.unregister(); +// Learn more about service workers: https://cra.link/PWA +serviceWorkerRegistration.register(); diff --git a/web/src/service-worker.ts b/web/src/service-worker.ts new file mode 100644 index 00000000..65a2abab --- /dev/null +++ b/web/src/service-worker.ts @@ -0,0 +1,95 @@ +/// +/* eslint-disable no-restricted-globals */ + +// This service worker can be customized! +// See https://developers.google.com/web/tools/workbox/modules +// for the list of available Workbox modules, or add any other +// code you'd like. +// You can also remove this file if you'd prefer not to use a +// service worker, and the Workbox build step will be skipped. + +import { clientsClaim } from 'workbox-core'; +import { ExpirationPlugin } from 'workbox-expiration'; +import { precacheAndRoute, createHandlerBoundToURL } from 'workbox-precaching'; +import { registerRoute } from 'workbox-routing'; +import { StaleWhileRevalidate } from 'workbox-strategies'; + +declare const self: ServiceWorkerGlobalScope; + +clientsClaim(); + +// Precache all of the assets generated by your build process. +// Their URLs are injected into the manifest variable below. +// This variable must be present somewhere in your service worker file, +// even if you decide not to use precaching. See https://cra.link/PWA +precacheAndRoute(self.__WB_MANIFEST); + +// Set up App Shell-style routing, so that all navigation requests +// are fulfilled with your index.html shell. Learn more at +// https://developers.google.com/web/fundamentals/architecture/app-shell +const fileExtensionRegexp = new RegExp('/[^/?]+\\.[^/]+$'); +registerRoute( + // Return false to exempt requests from being fulfilled by index.html. + ({ request, url }: { request: Request; url: URL }) => { + // If this isn't a navigation, skip. + if (request.mode !== 'navigate') { + return false; + } + + // If this is a URL that starts with /_, skip. + if (url.pathname.startsWith('/_')) { + return false; + } + + // If this looks like a URL for a resource, because it contains + // a file extension, skip. + if (url.pathname.match(fileExtensionRegexp)) { + return false; + } + + // Return true to signal that we want to use the handler. + return true; + }, + createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html') +); + +// An example runtime caching route for requests that aren't handled by the +// precache, in this case same-origin .png requests like those from in public/ +registerRoute( + // Add in any other file extensions or routing criteria as needed. + ({ url }) => url.origin === self.location.origin && url.pathname.endsWith('.png'), + // Customize this strategy as needed, e.g., by changing to CacheFirst. + new StaleWhileRevalidate({ + cacheName: 'images', + plugins: [ + // Ensure that once this runtime cache reaches a maximum size the + // least-recently used images are removed. + new ExpirationPlugin({ maxEntries: 50 }), + ], + }) +); + +// Cache WebAssembly and Go assets +const goWasmAssetsRegExp = new RegExp('^/(wasm_exec.js|worker.wasm)$'); +const DAY_IN_SECONDS = 24 * 60 * 60; +registerRoute( + ({ url }) => url.origin === self.location.origin && goWasmAssetsRegExp.test(url.pathname), + new StaleWhileRevalidate({ + cacheName: 'wasm', + plugins: [ + // Ensure that once this runtime cache reaches a maximum size the + // least-recently used images are removed. + new ExpirationPlugin({ maxAgeSeconds: 5 * DAY_IN_SECONDS }), + ], + }) +) + +// This allows the web app to trigger skipWaiting via +// registration.waiting.postMessage({type: 'SKIP_WAITING'}) +self.addEventListener('message', (event) => { + if (event.data && event.data.type === 'SKIP_WAITING') { + self.skipWaiting(); + } +}); + +// Any other custom service worker logic can go here. diff --git a/web/src/serviceWorker.ts b/web/src/serviceWorkerRegistration.ts similarity index 79% rename from web/src/serviceWorker.ts rename to web/src/serviceWorkerRegistration.ts index a7e02f16..df279621 100644 --- a/web/src/serviceWorker.ts +++ b/web/src/serviceWorkerRegistration.ts @@ -1,3 +1,4 @@ +/// // This optional code is used to register a service worker. // register() is not called by default. @@ -8,19 +9,22 @@ // resources are updated in the background. // To learn more about the benefits of this model and instructions on how to -// opt-in, read https://bit.ly/CRA-PWA +// opt-in, read https://cra.link/PWA const isLocalhost = Boolean( window.location.hostname === 'localhost' || // [::1] is the IPv6 localhost address. window.location.hostname === '[::1]' || // 127.0.0.0/8 are considered localhost for IPv4. - window.location.hostname.match( - /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ - ) + window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/) ); -export function register(config) { +type Config = { + onSuccess?: (registration: ServiceWorkerRegistration) => void; + onUpdate?: (registration: ServiceWorkerRegistration) => void; +}; + +export function register(config?: Config) { if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { // The URL constructor is available in all browsers that support SW. const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); @@ -43,7 +47,7 @@ export function register(config) { navigator.serviceWorker.ready.then(() => { console.log( 'This web app is being served cache-first by a service ' + - 'worker. To learn more, visit https://bit.ly/CRA-PWA' + 'worker. To learn more, visit https://cra.link/PWA' ); }); } else { @@ -54,10 +58,10 @@ export function register(config) { } } -function registerValidSW(swUrl, config) { +function registerValidSW(swUrl: string, config?: Config) { navigator.serviceWorker .register(swUrl) - .then(registration => { + .then((registration) => { registration.onupdatefound = () => { const installingWorker = registration.installing; if (installingWorker == null) { @@ -71,7 +75,7 @@ function registerValidSW(swUrl, config) { // content until all client tabs are closed. console.log( 'New content is available and will be used when all ' + - 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' + 'tabs for this page are closed. See https://cra.link/PWA.' ); // Execute callback @@ -93,17 +97,17 @@ function registerValidSW(swUrl, config) { }; }; }) - .catch(error => { + .catch((error) => { console.error('Error during service worker registration:', error); }); } -function checkValidServiceWorker(swUrl, config) { +function checkValidServiceWorker(swUrl: string, config?: Config) { // Check if the service worker can be found. If it can't reload the page. fetch(swUrl, { - headers: { 'Service-Worker': 'script' } + headers: { 'Service-Worker': 'script' }, }) - .then(response => { + .then((response) => { // Ensure service worker exists, and that we really are getting a JS file. const contentType = response.headers.get('content-type'); if ( @@ -111,7 +115,7 @@ function checkValidServiceWorker(swUrl, config) { (contentType != null && contentType.indexOf('javascript') === -1) ) { // No service worker found. Probably a different app. Reload the page. - navigator.serviceWorker.ready.then(registration => { + navigator.serviceWorker.ready.then((registration) => { registration.unregister().then(() => { window.location.reload(); }); @@ -122,16 +126,18 @@ function checkValidServiceWorker(swUrl, config) { } }) .catch(() => { - console.log( - 'No internet connection found. App is running in offline mode.' - ); + console.log('No internet connection found. App is running in offline mode.'); }); } export function unregister() { if ('serviceWorker' in navigator) { - navigator.serviceWorker.ready.then(registration => { - registration.unregister(); - }); + navigator.serviceWorker.ready + .then((registration) => { + registration.unregister(); + }) + .catch((error) => { + console.error(error.message); + }); } } diff --git a/web/yarn.lock b/web/yarn.lock index 3aa4fed4..71d74420 100644 --- a/web/yarn.lock +++ b/web/yarn.lock @@ -1988,6 +1988,11 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.15.tgz#97779282c09c09577120a2162e71d8380003590a" integrity sha512-zWt4SDDv1S9WRBNxLFxFRHxdD9tvH8f5/kg5/IaLFdnSNXsDY4eL3Q3XXN+VxUnWIhyVFDwcsmAprvwXoM/ClA== +"@types/node@^17.0.16": + version "17.0.16" + resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.16.tgz#e3733f46797b9df9e853ca9f719c8a6f7b84cd26" + integrity sha512-ydLaGVfQOQ6hI1xK2A5nVh8bl0OGoIfYMxPWHqqYe9bTkWCfqiVvZoh2I/QF2sNSkZzZyROBoTefIEI+PB6iIA== + "@types/parse-json@^4.0.0": version "4.0.0" resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0" @@ -9062,6 +9067,13 @@ workbox-background-sync@6.4.2: idb "^6.1.4" workbox-core "6.4.2" +workbox-background-sync@^5.1.4: + version "5.1.4" + resolved "https://registry.yarnpkg.com/workbox-background-sync/-/workbox-background-sync-5.1.4.tgz#5ae0bbd455f4e9c319e8d827c055bb86c894fd12" + integrity sha512-AH6x5pYq4vwQvfRDWH+vfOePfPIYQ00nCEB7dJRU1e0n9+9HMRyvI63FlDvtFT2AvXVRsXvUt7DNMEToyJLpSA== + dependencies: + workbox-core "^5.1.4" + workbox-broadcast-update@6.4.2: version "6.4.2" resolved "https://registry.yarnpkg.com/workbox-broadcast-update/-/workbox-broadcast-update-6.4.2.tgz#5094c4767dfb590532ac03ee07e9e82b2ac206bc" @@ -9069,6 +9081,13 @@ workbox-broadcast-update@6.4.2: dependencies: workbox-core "6.4.2" +workbox-broadcast-update@^5.1.4: + version "5.1.4" + resolved "https://registry.yarnpkg.com/workbox-broadcast-update/-/workbox-broadcast-update-5.1.4.tgz#0eeb89170ddca7f6914fa3523fb14462891f2cfc" + integrity sha512-HTyTWkqXvHRuqY73XrwvXPud/FN6x3ROzkfFPsRjtw/kGZuZkPzfeH531qdUGfhtwjmtO/ZzXcWErqVzJNdXaA== + dependencies: + workbox-core "^5.1.4" + workbox-build@6.4.2: version "6.4.2" resolved "https://registry.yarnpkg.com/workbox-build/-/workbox-build-6.4.2.tgz#47f9baa946c3491533cd5ccb1f194a7160e8a6e3" @@ -9120,11 +9139,23 @@ workbox-cacheable-response@6.4.2: dependencies: workbox-core "6.4.2" +workbox-cacheable-response@^5.1.4: + version "5.1.4" + resolved "https://registry.yarnpkg.com/workbox-cacheable-response/-/workbox-cacheable-response-5.1.4.tgz#9ff26e1366214bdd05cf5a43da9305b274078a54" + integrity sha512-0bfvMZs0Of1S5cdswfQK0BXt6ulU5kVD4lwer2CeI+03czHprXR3V4Y8lPTooamn7eHP8Iywi5QjyAMjw0qauA== + dependencies: + workbox-core "^5.1.4" + workbox-core@6.4.2: version "6.4.2" resolved "https://registry.yarnpkg.com/workbox-core/-/workbox-core-6.4.2.tgz#f99fd36a211cc01dce90aa7d5f2c255e8fe9d6bc" integrity sha512-1U6cdEYPcajRXiboSlpJx6U7TvhIKbxRRerfepAJu2hniKwJ3DHILjpU/zx3yvzSBCWcNJDoFalf7Vgd7ey/rw== +workbox-core@^5.1.4: + version "5.1.4" + resolved "https://registry.yarnpkg.com/workbox-core/-/workbox-core-5.1.4.tgz#8bbfb2362ecdff30e25d123c82c79ac65d9264f4" + integrity sha512-+4iRQan/1D8I81nR2L5vcbaaFskZC2CL17TLbvWVzQ4qiF/ytOGF6XeV54pVxAvKUtkLANhk8TyIUMtiMw2oDg== + workbox-expiration@6.4.2: version "6.4.2" resolved "https://registry.yarnpkg.com/workbox-expiration/-/workbox-expiration-6.4.2.tgz#61613459fd6ddd1362730767618d444c6b9c9139" @@ -9133,6 +9164,13 @@ workbox-expiration@6.4.2: idb "^6.1.4" workbox-core "6.4.2" +workbox-expiration@^5.1.4: + version "5.1.4" + resolved "https://registry.yarnpkg.com/workbox-expiration/-/workbox-expiration-5.1.4.tgz#92b5df461e8126114943a3b15c55e4ecb920b163" + integrity sha512-oDO/5iC65h2Eq7jctAv858W2+CeRW5e0jZBMNRXpzp0ZPvuT6GblUiHnAsC5W5lANs1QS9atVOm4ifrBiYY7AQ== + dependencies: + workbox-core "^5.1.4" + workbox-google-analytics@6.4.2: version "6.4.2" resolved "https://registry.yarnpkg.com/workbox-google-analytics/-/workbox-google-analytics-6.4.2.tgz#eea7d511b3078665a726dc2ee9f11c6b7a897530" @@ -9143,6 +9181,16 @@ workbox-google-analytics@6.4.2: workbox-routing "6.4.2" workbox-strategies "6.4.2" +workbox-google-analytics@^5.1.4: + version "5.1.4" + resolved "https://registry.yarnpkg.com/workbox-google-analytics/-/workbox-google-analytics-5.1.4.tgz#b3376806b1ac7d7df8418304d379707195fa8517" + integrity sha512-0IFhKoEVrreHpKgcOoddV+oIaVXBFKXUzJVBI+nb0bxmcwYuZMdteBTp8AEDJacENtc9xbR0wa9RDCnYsCDLjA== + dependencies: + workbox-background-sync "^5.1.4" + workbox-core "^5.1.4" + workbox-routing "^5.1.4" + workbox-strategies "^5.1.4" + workbox-navigation-preload@6.4.2: version "6.4.2" resolved "https://registry.yarnpkg.com/workbox-navigation-preload/-/workbox-navigation-preload-6.4.2.tgz#35cd4ba416a530796af135410ca07db5bee11668" @@ -9150,6 +9198,13 @@ workbox-navigation-preload@6.4.2: dependencies: workbox-core "6.4.2" +workbox-navigation-preload@^5.1.4: + version "5.1.4" + resolved "https://registry.yarnpkg.com/workbox-navigation-preload/-/workbox-navigation-preload-5.1.4.tgz#30d1b720d26a05efc5fa11503e5cc1ed5a78902a" + integrity sha512-Wf03osvK0wTflAfKXba//QmWC5BIaIZARU03JIhAEO2wSB2BDROWI8Q/zmianf54kdV7e1eLaIEZhth4K4MyfQ== + dependencies: + workbox-core "^5.1.4" + workbox-precaching@6.4.2: version "6.4.2" resolved "https://registry.yarnpkg.com/workbox-precaching/-/workbox-precaching-6.4.2.tgz#8d87c05d54f32ac140f549faebf3b4d42d63621e" @@ -9159,6 +9214,13 @@ workbox-precaching@6.4.2: workbox-routing "6.4.2" workbox-strategies "6.4.2" +workbox-precaching@^5.1.4: + version "5.1.4" + resolved "https://registry.yarnpkg.com/workbox-precaching/-/workbox-precaching-5.1.4.tgz#874f7ebdd750dd3e04249efae9a1b3f48285fe6b" + integrity sha512-gCIFrBXmVQLFwvAzuGLCmkUYGVhBb7D1k/IL7pUJUO5xacjLcFUaLnnsoVepBGAiKw34HU1y/YuqvTKim9qAZA== + dependencies: + workbox-core "^5.1.4" + workbox-range-requests@6.4.2: version "6.4.2" resolved "https://registry.yarnpkg.com/workbox-range-requests/-/workbox-range-requests-6.4.2.tgz#050f0dfbb61cd1231e609ed91298b6c2442ae41b" @@ -9166,6 +9228,13 @@ workbox-range-requests@6.4.2: dependencies: workbox-core "6.4.2" +workbox-range-requests@^5.1.4: + version "5.1.4" + resolved "https://registry.yarnpkg.com/workbox-range-requests/-/workbox-range-requests-5.1.4.tgz#7066a12c121df65bf76fdf2b0868016aa2bab859" + integrity sha512-1HSujLjgTeoxHrMR2muDW2dKdxqCGMc1KbeyGcmjZZAizJTFwu7CWLDmLv6O1ceWYrhfuLFJO+umYMddk2XMhw== + dependencies: + workbox-core "^5.1.4" + workbox-recipes@6.4.2: version "6.4.2" resolved "https://registry.yarnpkg.com/workbox-recipes/-/workbox-recipes-6.4.2.tgz#68de41fa3a77b444b0f93c9c01a76ba1d41fd2bf" @@ -9185,6 +9254,13 @@ workbox-routing@6.4.2: dependencies: workbox-core "6.4.2" +workbox-routing@^5.1.4: + version "5.1.4" + resolved "https://registry.yarnpkg.com/workbox-routing/-/workbox-routing-5.1.4.tgz#3e8cd86bd3b6573488d1a2ce7385e547b547e970" + integrity sha512-8ljknRfqE1vEQtnMtzfksL+UXO822jJlHTIR7+BtJuxQ17+WPZfsHqvk1ynR/v0EHik4x2+826Hkwpgh4GKDCw== + dependencies: + workbox-core "^5.1.4" + workbox-strategies@6.4.2: version "6.4.2" resolved "https://registry.yarnpkg.com/workbox-strategies/-/workbox-strategies-6.4.2.tgz#50c02bf2d116918e1a8052df5f2c1e4103c62d5d" @@ -9192,6 +9268,14 @@ workbox-strategies@6.4.2: dependencies: workbox-core "6.4.2" +workbox-strategies@^5.1.4: + version "5.1.4" + resolved "https://registry.yarnpkg.com/workbox-strategies/-/workbox-strategies-5.1.4.tgz#96b1418ccdfde5354612914964074d466c52d08c" + integrity sha512-VVS57LpaJTdjW3RgZvPwX0NlhNmscR7OQ9bP+N/34cYMDzXLyA6kqWffP6QKXSkca1OFo/v6v7hW7zrrguo6EA== + dependencies: + workbox-core "^5.1.4" + workbox-routing "^5.1.4" + workbox-streams@6.4.2: version "6.4.2" resolved "https://registry.yarnpkg.com/workbox-streams/-/workbox-streams-6.4.2.tgz#3bc615cccebfd62dedf28315afb7d9ee177912a5" @@ -9200,6 +9284,14 @@ workbox-streams@6.4.2: workbox-core "6.4.2" workbox-routing "6.4.2" +workbox-streams@^5.1.4: + version "5.1.4" + resolved "https://registry.yarnpkg.com/workbox-streams/-/workbox-streams-5.1.4.tgz#05754e5e3667bdc078df2c9315b3f41210d8cac0" + integrity sha512-xU8yuF1hI/XcVhJUAfbQLa1guQUhdLMPQJkdT0kn6HP5CwiPOGiXnSFq80rAG4b1kJUChQQIGPrq439FQUNVrw== + dependencies: + workbox-core "^5.1.4" + workbox-routing "^5.1.4" + workbox-sw@6.4.2: version "6.4.2" resolved "https://registry.yarnpkg.com/workbox-sw/-/workbox-sw-6.4.2.tgz#9a6db5f74580915dc2f0dbd47d2ffe057c94a795"