N
Nuxt8mo 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)
// }
})
}
})
// 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?
1 Reply
Tincan
TincanOP8mo ago
hmm yes I was just arriving at a tentative conclusion that page has already loaded so thats not firing in that case i could remove the eventlistener and go ahead and load register the sw file issues a little further down the line, but that appears to have worked. thanks!
Want results from more Discord servers?
Add your server