NuxtN
Nuxt2y ago
Tincan

Why isn't window.addeventListener working in plugin file?

// eslint-disable-next-line
export default defineNuxtPlugin((nuxtApp) => {
  if ("serviceWorker" in navigator) {
    console.log(" sw-register.js inside if")

    window.addEventListener("load", () => {
      console.log("sw-register.js inside addevent listener")

      // try {
      //   await navigator.serviceWorker.register("/sw.js")
      //   console.log("Registered SW")
      // } catch (error) {
      //   console.log(error)
      // }
    })
  }
})


I have this file sw-register.js being loaded as a plugin and I see the "inside if" console log, but not the "inside addevent listener". I have confirmed that window exists, but it does not appear to have the addEventListener function listed in the console. Any ideas?
Was this page helpful?