Sha256: b24f5843c2a05f052fde3dbaf2cca6f45d1a56529d52a2ac77e1f4d848d4b37f

Contents?: true

Size: 1.38 KB

Versions: 24

Compression:

Stored size: 1.38 KB

Contents

const visitedPages = JSON.parse(sessionStorage.getItem("visitedPages")) || []
const DELAYED_VISITS = 2
let deferredPrompt = null

const shouldCountVisitedPages = () => sessionStorage.getItem("userChoice") !== "dismissed" && visitedPages.length < DELAYED_VISITS && !visitedPages.includes(location.pathname)
const shouldPrompt = () => {
  // Disable the application install prompt showing constantly.
  if (localStorage.getItem("pwaInstallPromptSeen")) {
    return false
  }

  return deferredPrompt && sessionStorage.getItem("userChoice") !== "dismissed" && visitedPages.length >= DELAYED_VISITS
}

window.addEventListener("beforeinstallprompt", (event) => {
  event.preventDefault()
  deferredPrompt = event

  // allow the user browse through different locations before prompt them anything
  if (shouldCountVisitedPages()) {
    sessionStorage.setItem("visitedPages", JSON.stringify([...visitedPages, location.pathname]))
  }
});

// to trigger the prompt, it must be a "user gesture"
window.addEventListener("click", async (event) => {
  if (shouldPrompt()) {
    event.preventDefault()
    deferredPrompt.prompt()

    const { outcome } = await deferredPrompt.userChoice;
    // store the user choice to avoid asking again in the current session
    sessionStorage.setItem("userChoice", outcome)
    sessionStorage.removeItem("visitedPages")
    localStorage.setItem("pwaInstallPromptSeen", true)
  }
});

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/packs/src/decidim/sw/a2hs.js
decidim-core-0.28.4 app/packs/src/decidim/sw/a2hs.js
decidim-core-0.27.9 app/packs/src/decidim/sw/a2hs.js
decidim-core-0.29.0 app/packs/src/decidim/sw/a2hs.js
decidim-core-0.28.3 app/packs/src/decidim/sw/a2hs.js
decidim-core-0.27.8 app/packs/src/decidim/sw/a2hs.js
decidim-core-0.29.0.rc4 app/packs/src/decidim/sw/a2hs.js
decidim-core-0.29.0.rc3 app/packs/src/decidim/sw/a2hs.js
decidim-core-0.29.0.rc2 app/packs/src/decidim/sw/a2hs.js
decidim-core-0.29.0.rc1 app/packs/src/decidim/sw/a2hs.js
decidim-core-0.28.2 app/packs/src/decidim/sw/a2hs.js
decidim-core-0.27.7 app/packs/src/decidim/sw/a2hs.js
decidim-core-0.28.1 app/packs/src/decidim/sw/a2hs.js
decidim-core-0.27.6 app/packs/src/decidim/sw/a2hs.js
decidim-core-0.28.0 app/packs/src/decidim/sw/a2hs.js
decidim-core-0.27.5 app/packs/src/decidim/sw/a2hs.js
decidim-core-0.28.0.rc5 app/packs/src/decidim/sw/a2hs.js
decidim-core-0.28.0.rc4 app/packs/src/decidim/sw/a2hs.js
decidim-core-0.27.4 app/packs/src/decidim/sw/a2hs.js
decidim-core-0.27.3 app/packs/src/decidim/sw/a2hs.js