2

I am developing a web application using JSF 2.3.9 (Mojarra) and OmniFaces 3.4 in a Tomcat 9 container. On a page I am displaying informations from a org.omnifaces.cdi.ViewScoped bean. This bean is allocating resources in @PostConstruct and should release them as soon as the user is navigating to an other page by invoking the @PreDestroy method.

This works fine on browsers like Safari on OsX, Firefox, Chrome. But when the web application is launched from Safari on iOS, the @PreDestroy method is not called e.g. when the page is left. It seems to me that this happens because the unload() event handler ist not supported in Safari on iOS and as I can see the script unload.js from Omnifaces binds to this event.

Forgot to mention - it is the latest iOS version 13.2.3...

Has anybody an idea how to fix this?

Selaron
  • 6,105
  • 4
  • 31
  • 39
MichaelRS
  • 51
  • 3
  • IMO one should not strictly depend on destructors being invoked. Why do you need this cleanup? What if the user has multiple tabs open or just kills their browser? – Selaron Nov 21 '19 at 08:19
  • Technically speaking, this is a bug in OmniFaces. – BalusC Nov 21 '19 at 09:12
  • @Selaron I absolutely agree with you concerning depending on destructors when releasing resources. But I need this cleanup because the ViewScoped Server-Bean streams data from a network device to a JavaScript client implementation. When the user has left the page, the bean should close the resources. Since the bean is ViewScoped, multiple tabs are no problem. – MichaelRS Nov 21 '19 at 13:08
  • I created issue in OmniFaces https://github.com/omnifaces/omnifaces/issues/531 – BalusC Nov 22 '19 at 09:40
  • Can you try 3.4.1-SNAPSHOT to see if it's fixed for you? – BalusC Nov 23 '19 at 15:38
  • Where can I find 3.4.1-SNAPSHOT? – MichaelRS Nov 25 '19 at 05:35
  • You can find it here https://oss.sonatype.org/content/repositories/snapshots/org/omnifaces/omnifaces/3.4.1-SNAPSHOT/ If you're using Maven, check this https://stackoverflow.com/q/7715321 how to enable including snapshots in your pom.xml, then you can just use `3.4.1-SNAPSHOT`. – BalusC Nov 25 '19 at 10:15

1 Answers1

0

This is technically a bug in OmniFaces. It did never really consider whether beforeunload or unload event is actually supported. It also did never check the pagehide event.

It's reported in issue 531 and fixed for OmniFaces 3.4.1 (and 2.7.3).

Basically, the check was improved from

var unloadEvent = window.onbeforeunload ? "unload" : "beforeunload";

to

var unloadEvent = ("onpagehide" in window) ? "pagehide"
    : ("onbeforeunload" in window && !window.onbeforeunload) ? "beforeunload" : "unload";

It will first check if pagehide is actually supported by testing "onpagehide" in window, and when absent, do the same for beforeunload where it also checks if there is no (legacy) custom handler, else fall back to unload.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555