Skip to content

Commit

Permalink
Handle service worker through Vite PWA
Browse files Browse the repository at this point in the history
  • Loading branch information
elamperti committed Jan 6, 2024
1 parent 0608d31 commit a56be1f
Show file tree
Hide file tree
Showing 15 changed files with 843 additions and 333 deletions.
1 change: 0 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
PUBLIC_URL=/
REACT_APP_HOST=localhost:3000
REACT_APP_ANALYTICS_MEASUREMENT_ID=
REACT_APP_LASTFM_API_KEY=
Expand Down
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
*.md
src/utils/registerServiceWorker.js
cypress/fixtures/**/*.json
index.html
16 changes: 3 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openscrobbler",
"version": "2.8.4",
"version": "2.8.5",
"private": true,
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^6.5.1",
Expand Down Expand Up @@ -83,23 +83,13 @@
"vite": "^5.0.10",
"vite-bundle-visualizer": "^1.0.0",
"vite-plugin-externals": "^0.6.2",
"vite-plugin-pwa": "^0.17.4",
"vite-plugin-react-remove-attributes": "^1.0.3",
"vite-plugin-svgr": "^4.2.0",
"vite-tsconfig-paths": "^4.2.3",
"vitest": "^1.1.2",
"vitest-dom": "^0.1.1",
"workbox-background-sync": "^7.0.0",
"workbox-broadcast-update": "^7.0.0",
"workbox-cacheable-response": "^7.0.0",
"workbox-core": "^7.0.0",
"workbox-expiration": "^7.0.0",
"workbox-google-analytics": "^7.0.0",
"workbox-navigation-preload": "^7.0.0",
"workbox-precaching": "^7.0.0",
"workbox-range-requests": "^7.0.0",
"workbox-routing": "^7.0.0",
"workbox-strategies": "^7.0.0",
"workbox-streams": "^7.0.0"
"workbox-window": "^7.0.0"
},
"scripts": {
"prepare": "is-ci || husky install",
Expand Down
10 changes: 4 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Suspense, useEffect, useState } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { useSelector, useDispatch } from 'react-redux';
import { useDispatch } from 'react-redux';
import qs from 'qs';
import find from 'lodash/find';
import lazyWithPreload from 'react-lazy-with-preload';
Expand All @@ -9,6 +9,7 @@ import * as Sentry from '@sentry/react';
import { interceptAxios } from 'utils/axios';
import { languageList, fallbackLng } from 'utils/i18n';
import { useTranslation } from 'react-i18next';
import { useRegisterSW } from 'virtual:pwa-register/react';
import { useGrowthBook } from '@growthbook/growthbook-react';

import { useUserData } from 'hooks/useUserData';
Expand All @@ -21,7 +22,6 @@ import AlertZone from './components/AlertZone';
import AnalyticsListener from './components/AnalyticsListener';
import UpdateToast from './components/UpdateToast';

import { RootState } from 'store';
import { useQueryClient } from '@tanstack/react-query';
import { useLanguage } from 'hooks/useLanguage';

Expand All @@ -37,13 +37,11 @@ function App() {
const growthbook = useGrowthBook();
const { setLanguage } = useLanguage();
const { isLoggedIn, user } = useUserData();
const serviceWorker = useRegisterSW();

// This is used to trigger a suspense while i18n is loading
const { ready: i18nReady } = useTranslation();

// ToDo: remove this feature?
const versionUpdateReady = useSelector((state: RootState) => state.updates.newVersionReady);

const [modalIsOpen, setModalIsOpen] = useState(false);

useEffect(() => {
Expand Down Expand Up @@ -117,7 +115,7 @@ function App() {
</SettingsModalContext.Provider>
<div className="d-flex flex-column" style={{ height: 'calc(100vh - 84px)' }}>
{process.env.REACT_APP_ANALYTICS_CODE && <AnalyticsListener />}
{versionUpdateReady && <UpdateToast />}
{serviceWorker.needRefresh[0] && <UpdateToast onUpdate={serviceWorker.updateServiceWorker} />}

<AlertZone />
<main className="container flex-wrap flex-grow-1">
Expand Down
3 changes: 0 additions & 3 deletions src/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ export const COUNT_SCROBBLES_DISABLE = 'COUNT_SCROBBLES_DISABLE';
// Users store
export const USER_LOGGED_OUT = 'USER_LOGGED_OUT';

// Updates store
export const NEW_VERSION_READY = 'NEW_VERSION_READY';

// Referral link generation
export const getAmznLink = (artist, album) => {
if (artist === album) {
Expand Down
11 changes: 9 additions & 2 deletions src/components/UpdateToast.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { Trans } from 'react-i18next';
import ReactGA from 'react-ga-neo';

import { Alert, Button } from 'reactstrap';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faComment, faExclamation } from '@fortawesome/free-solid-svg-icons';

import './UpdateToast.css';

export default function UpdateToast() {
export default function UpdateToast({ onUpdate }: { onUpdate: () => void }) {
const reloadPage = () => {
window.location.reload();
ReactGA.event({
category: 'Interactions',
action: 'SW Manual Update',
});
onUpdate();
};

return (
Expand Down
11 changes: 1 addition & 10 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import './index.css';
import App from 'App';
import history from 'utils/history';
import ErrorPage from 'domains/error/ErrorPage';
import * as serviceWorkerRegistration from 'utils/serviceWorkerRegistration';
// import reportWebVitals from 'utils/reportWebVitals';

import 'bootswatch/dist/slate/bootstrap.min.css';
Expand All @@ -19,7 +18,7 @@ import * as Sentry from '@sentry/react';
import { GrowthBook, GrowthBookProvider } from '@growthbook/growthbook-react';

import 'utils/i18n';
import { NEW_VERSION_READY } from 'Constants';

import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
import { tweak } from 'utils/tweaks/Tweaks';
Expand Down Expand Up @@ -149,14 +148,6 @@ if (process.env.REACT_APP_GROWTHBOOK_API_KEY) {
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
root.render(wrappedApp);

serviceWorkerRegistration.register({
onUpdate: () => {
store.dispatch({
type: NEW_VERSION_READY,
});
},
});

// Measure performance Learn more: https://bit.ly/CRA-vitals
// ToDo: Analyze if this is worth using
// reportWebVitals(process.env.NODE_ENV === 'development' ? console.log : undefined);
86 changes: 0 additions & 86 deletions src/service-worker.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { loadState, saveState } from 'localstorage';

import alertReducer from './reducers/alertReducer';
import scrobbleReducer from './reducers/scrobbleReducer';
import updatesReducer from './reducers/updatesReducer';

const middlewares = [createDebounce(), promise];
const isDevEnvironment = process.env.NODE_ENV === 'development';
Expand All @@ -19,7 +18,6 @@ const store = createStore(
combineReducers({
alerts: alertReducer,
scrobbles: scrobbleReducer,
updates: updatesReducer,
}),
persistedState,
(isDevEnvironment && hasDevTools ? composeWithDevTools({}) : compose)(applyMiddleware(...middlewares))
Expand Down
24 changes: 0 additions & 24 deletions src/store/reducers/updatesReducer.ts

This file was deleted.

Loading

0 comments on commit a56be1f

Please sign in to comment.