Skip to content

Commit

Permalink
fix(frontend): service worker registration (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
floreks authored Nov 17, 2022
1 parent 79a388c commit e1753d7
Show file tree
Hide file tree
Showing 8 changed files with 639 additions and 29 deletions.
1 change: 1 addition & 0 deletions www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
"stylelint": "14.15.0",
"typescript": "4.9.3",
"vite": "3.2.4",
"vite-plugin-pwa": "0.13.3",
"vitest": "0.25.2",
"wait-on": "6.0.1"
}
Expand Down
Empty file removed www/public/service-worker.js
Empty file.
2 changes: 0 additions & 2 deletions www/src/components/layout/WithApplicationUpdate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import {
} from 'react'

import * as serviceWorker from '../../serviceWorkerRegistration'

import PluralConfigurationContext from '../../contexts/PluralConfigurationContext'

const COMMIT_KEY = 'git-commit'

export const DEFAULT_COMMIT = 'plural-default-commit'

export const getCommit = () => sessionStorage.getItem(COMMIT_KEY) || DEFAULT_COMMIT
export const setCommit = sha => sessionStorage.setItem(COMMIT_KEY, sha)

Expand Down
2 changes: 0 additions & 2 deletions www/src/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ clientsClaim()

// Precache all 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
// Filter out the "index.html" so that it is not cached as it breaks
// the application when the code changes and SW caches it
// @ts-expect-error
Expand Down
10 changes: 5 additions & 5 deletions www/src/serviceWorkerRegistration.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const isProduction = import.meta.env.PROD
const url = import.meta.env.BASE_URL || ''
const url = import.meta.url || ''
const isServiceWorkerAvailable = 'serviceWorker' in navigator
const serviceWorkerURL = `${url}/service-worker.js`
const serviceWorkerURL = '/service-worker.js'
const isLocalhost = Boolean(window.location.hostname === 'localhost'
// [::1] is the IPv6 localhost address.
|| window.location.hostname === '[::1]'
Expand All @@ -25,7 +25,7 @@ export const register = (config: Config): void => {
if (publicURL.origin !== window.location.origin) {
// Our service worker won't work if PUBLIC_URL is on a different origin
// from what our page is served on. This might happen if a CDN is used to
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
// serve assets
return
}

Expand Down Expand Up @@ -53,7 +53,7 @@ const onLoad = config => async () => {
// service worker/PWA documentation.
navigator.serviceWorker.ready.then(() => {
console.log('This web app is being served cache-first by a service '
+ 'worker. To learn more, visit https://cra.link/PWA')
+ 'worker.')
})
}

Expand Down Expand Up @@ -97,7 +97,7 @@ const onServiceWorkerStateChange = (registration: ServiceWorkerRegistration, con
}

console.log('New content is available and will be used when all '
+ 'tabs for this page are closed. See https://cra.link/PWA.')
+ 'tabs for this page are closed.')

// Execute callback
if (config?.onUpdate) {
Expand Down
2 changes: 1 addition & 1 deletion www/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"lib": ["DOM", "DOM.Iterable", "ESNext", "WebWorker"],
"allowJs": false,
"skipLibCheck": true,
"esModuleInterop": false,
Expand Down
7 changes: 7 additions & 0 deletions www/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import basicSsl from '@vitejs/plugin-basic-ssl'
import GlobalPolyFill from '@esbuild-plugins/node-globals-polyfill'
import { VitePWA } from 'vite-plugin-pwa'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
basicSsl(),
react(),
VitePWA({
injectRegister: null,
filename: 'service-worker.ts',
srcDir: 'src',
strategies: 'injectManifest',
}),
],
server: {
port: 3001,
Expand Down
Loading

0 comments on commit e1753d7

Please sign in to comment.