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

"beforeinstallprompt" Event Listener not triggered on page load. Next.js 14, Next-PWA #1

Closed
ChiragChrg opened this issue Nov 28, 2023 · 1 comment
Assignees
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed

Comments

@ChiragChrg
Copy link
Owner

Im trying to manually trigger PWA install prompt using my UI.
The PWA id called from root page.tsx & Sidebar component.
HandlePWAInstall function is triggered from this page

pwa.ts

interface DeferredPrompt {
    prompt: () => void;
    userChoice: Promise<{ outcome: string }>;
}

let deferredPrompt: DeferredPrompt | null = null;

// Show PWA install alert triggered by user button click
export const HandlePWAInstall = async () => {
    console.log("deferredPWAPrompt", deferredPrompt);

    if (deferredPrompt) {
        deferredPrompt.prompt();
        const { outcome } = await deferredPrompt.userChoice;
        console.log(outcome);
        deferredPrompt = null;
    } else {
        alert("Prompt Failed");
    }
};

const PWA = () => {
    console.log("PWA Triggered")
    window.addEventListener("beforeinstallprompt", (e) => {
        console.log("PWA Event", e)
        e.preventDefault();
        deferredPrompt = e as unknown as DeferredPrompt;
    });

    window.addEventListener("appinstalled", () => {
        deferredPrompt = null;
        console.log("PWA was installed");
    });
};

export default PWA;

I have used a similar method on another project and it works correctly. (prompt is triggered using button on Toast notification)

@ChiragChrg ChiragChrg added bug Something isn't working help wanted Extra attention is needed good first issue Good for newcomers labels Nov 28, 2023
@ChiragChrg ChiragChrg self-assigned this Nov 28, 2023
@ChiragChrg ChiragChrg closed this as not planned Won't fix, can't repro, duplicate, stale Nov 28, 2023
@ChiragChrg ChiragChrg reopened this Nov 28, 2023
@ChiragChrg
Copy link
Owner Author

Issue is resolved. Apparently the issue persists only on local development. The PWA prompt is triggered on button click in the live production deployment 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant