Skip to content

Commit 84c0318

Browse files
committed
fix: fix pwa install life cycle
1 parent 0085f76 commit 84c0318

File tree

2 files changed

+45
-41
lines changed

2 files changed

+45
-41
lines changed

src/components/PWAInstaller.tsx

+40-35
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,67 @@
11
"use client";
22

33
import "@khmyznikov/pwa-install";
4-
import {useEffect} from "react";
4+
import { useEffect } from "react";
55
export default function PWAInstaller({ ...props }) {
66
return (
77
// @ts-ignore
8-
<pwa-install {...props}></pwa-install>
8+
<pwa-install id={`pwa-install`} {...props}></pwa-install>
99
);
1010
}
1111

1212
export type PWAInstallerMethods = {
1313
install: (force?: boolean) => void;
14-
addEventListener: (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions) => void;
14+
isListening?: boolean;
15+
showDialog: (force?: boolean) => void;
16+
addEventListener: (
17+
type: string,
18+
listener: EventListenerOrEventListenerObject,
19+
options?: boolean | AddEventListenerOptions,
20+
) => void;
1521
};
1622

1723
export function usePWAInstaller() {
1824
const getInstallerElement = (): PWAInstallerMethods =>
19-
document.getElementsByTagName(
20-
"pwa-install",
21-
)[0] as unknown as PWAInstallerMethods;
25+
document.getElementById("pwa-install") as unknown as PWAInstallerMethods;
2226

23-
useEffect(() => {
24-
const installer = getInstallerElement();
25-
if (installer) {
26-
// register events
27+
return {
28+
install: (force?: boolean) => {
29+
const installer = getInstallerElement();
30+
installer?.showDialog(force);
2731

28-
installer.addEventListener("pwa-install-success-event", (e) => {
29-
console.log("[installer] installation success:", e);
30-
});
32+
console.log(
33+
`[installer] ${force ? "forced" : "prompted"} installation to:`,
34+
installer,
35+
);
3136

32-
installer.addEventListener("pwa-install-fail-event", (e) => {
33-
console.error("[installer] installation failed:", e);
34-
});
37+
if (installer && !installer.isListening) {
38+
// register events
39+
installer.isListening = true;
3540

36-
installer.addEventListener("pwa-install-available-event", (e) => {
37-
console.log("[installer] installation available:", e);
38-
});
41+
installer.addEventListener("pwa-install-success-event", (e) => {
42+
console.log("[installer] installation success:", e);
43+
});
3944

40-
installer.addEventListener("pwa-user-choice-result-event", (e) => {
41-
console.log("[installer] user choice result:", e);
42-
});
45+
installer.addEventListener("pwa-install-fail-event", (e) => {
46+
console.error("[installer] installation failed:", e);
47+
});
4348

44-
installer.addEventListener("pwa-install-how-to-event", (e) => {
45-
console.log("[installer] installation how to:", e);
46-
});
49+
installer.addEventListener("pwa-install-available-event", (e) => {
50+
console.log("[installer] installation available:", e);
51+
});
4752

48-
installer.addEventListener("pwa-install-gallery-event", (e) => {
49-
console.log("[installer] installation gallery:", e);
50-
});
51-
}
52-
}, []);
53+
installer.addEventListener("pwa-user-choice-result-event", (e) => {
54+
console.log("[installer] user choice result:", e);
55+
});
5356

54-
return {
55-
install: (force?: boolean) => {
56-
const installer = getInstallerElement();
57-
installer?.install(force);
57+
installer.addEventListener("pwa-install-how-to-event", (e) => {
58+
console.log("[installer] installation how to:", e);
59+
});
5860

59-
console.log(`[installer] ${force ? "forced" : "prompted"} installation to:`, installer);
61+
installer.addEventListener("pwa-install-gallery-event", (e) => {
62+
console.log("[installer] installation gallery:", e);
63+
});
64+
}
6065
},
6166
};
6267
}

src/pages/_app.tsx

+5-6
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,17 @@ export default function App({ Component, pageProps }: AppProps) {
3434
<meta name="viewport" content="width=device-width, initial-scale=1" />
3535
</Head>
3636
<Toaster />
37-
<PWAInstaller
38-
manifest-url="/manifest.json"
39-
name="Next Whois UI"
40-
description="🧪 Your Next Generation Of Whois Lookup Tool With Modern UI. Support Domain/IPv4/IPv6/ASN/CIDR Whois Lookup And Powerful Features."
41-
icon="/icons/icon-192x192.png"
42-
/>
4337
<ThemeProvider
4438
attribute="class"
4539
defaultTheme="system"
4640
enableSystem
4741
disableTransitionOnChange
4842
>
43+
<PWAInstaller
44+
manifest-url="/manifest.json"
45+
name="Next Whois UI"
46+
description="🧪 Your Next Generation Of Whois Lookup Tool With Modern UI. Support Domain/IPv4/IPv6/ASN/CIDR Whois Lookup And Powerful Features."
47+
/>
4948
<div className={cn(`relative w-full h-full`, inter.className)}>
5049
<div
5150
className={cn(

0 commit comments

Comments
 (0)