feat(pwa): keep service workers from getting registerd outside the pwa
This commit is contained in:
parent
d0918ff384
commit
96b81d30b1
2 changed files with 10 additions and 57 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect } from "react";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
|
|
||||||
import { setDatabaseEntry } from "@tensamin/shared/indexedDb";
|
import { setDatabaseEntry } from "@tensamin/shared/indexedDb";
|
||||||
|
|
@ -6,11 +6,6 @@ import { isTauri } from "@tauri-apps/api/core";
|
||||||
|
|
||||||
import "./style.css";
|
import "./style.css";
|
||||||
|
|
||||||
type BeforeInstallPromptEvent = Event & {
|
|
||||||
prompt: () => Promise<void>;
|
|
||||||
userChoice: Promise<{ outcome: "accepted" | "dismissed" }>;
|
|
||||||
};
|
|
||||||
|
|
||||||
const launchedFiles: File[] = [];
|
const launchedFiles: File[] = [];
|
||||||
const fileListeners = new Set<(file: File) => void>();
|
const fileListeners = new Set<(file: File) => void>();
|
||||||
|
|
||||||
|
|
@ -27,9 +22,10 @@ export function subscribeTuFileLaunch(listener: (file: File) => void) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function isStandalone() {
|
function isInstalledPwa() {
|
||||||
return (
|
return (
|
||||||
window.matchMedia("(display-mode: standalone)").matches ||
|
window.matchMedia("(display-mode: standalone)").matches ||
|
||||||
|
window.matchMedia("(display-mode: window-controls-overlay)").matches ||
|
||||||
(navigator as Navigator & { standalone?: boolean }).standalone === true
|
(navigator as Navigator & { standalone?: boolean }).standalone === true
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -65,13 +61,9 @@ async function enablePush() {
|
||||||
await setDatabaseEntry("keys", "push-subscription", subscription.toJSON());
|
await setDatabaseEntry("keys", "push-subscription", subscription.toJSON());
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function PwaRuntime() {
|
function InstalledPwaRuntime() {
|
||||||
const [installPrompt, setInstallPrompt] =
|
|
||||||
useState<BeforeInstallPromptEvent | null>(null);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (
|
if (
|
||||||
isTauri() ||
|
|
||||||
!("serviceWorker" in navigator) ||
|
!("serviceWorker" in navigator) ||
|
||||||
!["http:", "https:"].includes(window.location.protocol)
|
!["http:", "https:"].includes(window.location.protocol)
|
||||||
) {
|
) {
|
||||||
|
|
@ -129,16 +121,6 @@ export default function PwaRuntime() {
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const handleInstallPrompt = (event: Event) => {
|
|
||||||
event.preventDefault();
|
|
||||||
setInstallPrompt(event as BeforeInstallPromptEvent);
|
|
||||||
};
|
|
||||||
window.addEventListener("beforeinstallprompt", handleInstallPrompt);
|
|
||||||
return () =>
|
|
||||||
window.removeEventListener("beforeinstallprompt", handleInstallPrompt);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const launchQueue = (
|
const launchQueue = (
|
||||||
window as Window & {
|
window as Window & {
|
||||||
|
|
@ -159,41 +141,7 @@ export default function PwaRuntime() {
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!installPrompt) return;
|
|
||||||
toast("Install Tensamin for a native app experience", {
|
|
||||||
duration: Infinity,
|
|
||||||
action: {
|
|
||||||
label: "Install",
|
|
||||||
onClick: () => {
|
|
||||||
void installPrompt.prompt().then(() => installPrompt.userChoice);
|
|
||||||
setInstallPrompt(null);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}, [installPrompt]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const isAppleMobile = /iPad|iPhone|iPod/.test(navigator.userAgent);
|
|
||||||
if (
|
if (
|
||||||
isTauri() ||
|
|
||||||
!isAppleMobile ||
|
|
||||||
isStandalone() ||
|
|
||||||
localStorage.getItem("pwa-ios-install-hint")
|
|
||||||
) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
localStorage.setItem("pwa-ios-install-hint", "shown");
|
|
||||||
toast(
|
|
||||||
"Install Tensamin from Safari's Share menu to enable background notifications.",
|
|
||||||
{
|
|
||||||
duration: 12_000,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (
|
|
||||||
!isStandalone() ||
|
|
||||||
!("Notification" in window) ||
|
!("Notification" in window) ||
|
||||||
Notification.permission !== "default" ||
|
Notification.permission !== "default" ||
|
||||||
localStorage.getItem("pwa-push-hint")
|
localStorage.getItem("pwa-push-hint")
|
||||||
|
|
@ -222,7 +170,6 @@ export default function PwaRuntime() {
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (
|
if (
|
||||||
isStandalone() &&
|
|
||||||
"Notification" in window &&
|
"Notification" in window &&
|
||||||
Notification.permission === "granted" &&
|
Notification.permission === "granted" &&
|
||||||
import.meta.env.VITE_WEB_PUSH_PUBLIC_KEY
|
import.meta.env.VITE_WEB_PUSH_PUBLIC_KEY
|
||||||
|
|
@ -235,3 +182,8 @@ export default function PwaRuntime() {
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default function PwaRuntime() {
|
||||||
|
if (isTauri() || !isInstalledPwa()) return null;
|
||||||
|
return <InstalledPwaRuntime />;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -128,6 +128,7 @@ export function tensaminPwa(): Plugin[] {
|
||||||
filename: "serviceWorker.ts",
|
filename: "serviceWorker.ts",
|
||||||
injectRegister: null,
|
injectRegister: null,
|
||||||
registerType: "prompt",
|
registerType: "prompt",
|
||||||
|
buildBase: "/",
|
||||||
manifestFilename: "manifest.json",
|
manifestFilename: "manifest.json",
|
||||||
includeAssets: ["favicon.ico", "icons/*.png"],
|
includeAssets: ["favicon.ico", "icons/*.png"],
|
||||||
manifest: {
|
manifest: {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue