Chrome's prefetching reloads desktop notifications

63 Views Asked by At

I have a strange bug when using the Notifications API in Chrome. I have a page that loads some notifications from the Cookie and displays them on page load using the Notifications API. If I then go to a second page using a hyperlink and then start changing the URL address, the moment I reach the first page's address even before hitting enter, I get the notifications popped up again. I think it has something to do with how Chrome is doing clever prefetching. Is there anyway that I can stop this?

This is the code I currently have:

var desktop_notification_options = {
  body: text.text(),
  icon: this.settings.icon,
  tag: text.text()
};

var desktop_notification = new Notification('', desktop_notification_options);

setTimeout(desktop_notification.close.bind(desktop_notification),
            this.settings.desktop_notification_timeout);

I've realized that if I add an alert('') on that page, this stops (presumably because chrome's prefetch can't skip the alert), however that's not a reasonable fix.

Any help is greatly appreciated.

1

There are 1 best solutions below

0
On

I found a quick fix. I checked document.visibilityState before showing the notifications and only showed them if it was set to visible. More info here: http://www.w3.org/TR/page-visibility/#dom-document-visibilitystate