Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service worker performance #2734

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,60 @@ const NotificationsHandler: FC = () => {
const userId = user?.uid;
const [isRegistered, setIsRegistered] = useState(false);

function initServiceWorker() {
navigator.serviceWorker
.register("/firebase-messaging-sw.js")
.then((registration) => {
setIsRegistered(true);
return registration;
})
.catch((err) => {
console.log("ServiceWorker registration failed: ", err);
});
}

// Check if the service worker is already registered or register a new one
useEffect(() => {
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("/firebase-messaging-sw.js")
.then((registration) => {
setIsRegistered(true);
return registration;
.getRegistration("/firebase-messaging-sw.js")
.then((existingRegistration) => {
if (existingRegistration) {
setIsRegistered(true);
} else {
initServiceWorker();
}

return;
})
.catch((err) => {
console.log("ServiceWorker registration failed: ", err);
console.log("Error checking service worker registration: ", err);
});
}
}, []);

// Handle notification permissions and foreground message listener
useEffect(() => {
if (!userId && !isRegistered) {
if (!userId || !isRegistered) {
return;
}

let unsubscribeOnMessage;
(async () => {
const hasPermissions = await NotificationService.requestPermissions();
if (hasPermissions) {
await NotificationService.saveFCMToken();

unsubscribeOnMessage = NotificationService.onForegroundMessage();
if (!hasPermissions) {
console.log("Notification permissions denied");
return;
}

await NotificationService.saveFCMToken();
unsubscribeOnMessage = NotificationService.onForegroundMessage();
})();

return () => {
unsubscribeOnMessage && unsubscribeOnMessage();
if (unsubscribeOnMessage) {
unsubscribeOnMessage();
}
};
}, [userId, isRegistered]);

Expand Down
7 changes: 5 additions & 2 deletions src/shared/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useDispatch, useSelector } from "react-redux";
import { Link, RouteProps, useHistory } from "react-router-dom";
import classNames from "classnames";
import { Routes } from "@/pages/MyAccount/components/Routes";
import { NotificationService } from "@/services";
import { Loader } from "@/shared/components";
import {
useAnyMandatoryRoles,
Expand Down Expand Up @@ -82,7 +83,8 @@ const Header = () => {
setShowAccountLinks(isMyAccountRoute);
}, [showMenu, isMyAccountRoute]);

const handleLogIn = useCallback(() => {
const handleLogIn = useCallback(async () => {
await NotificationService.requestPermissions();
dispatch(setLoginModalState({ isShowing: true }));
setShowMenu(false);
}, [dispatch]);
Expand Down Expand Up @@ -116,7 +118,8 @@ const Header = () => {
dispatch(logOut());
};

const handleLaunchApp = () => {
const handleLaunchApp = async () => {
await NotificationService.requestPermissions();
history.push(ROUTE_PATHS.INBOX);
};

Expand Down
1 change: 1 addition & 0 deletions src/shared/utils/firebase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function handlePersistenceError(err: any) {
clearFirestoreCache();
} else {
console.error("Error enabling persistence:", err);
reinitializeFirestoreWithPersistence();
}
}

Expand Down
Loading