Phaser and WebGL context restoration handling

So Phaser automatically handles WebGL context loss and restoration. But does it try to restore the scene or game or does it just handle the context restoration itself?

I’m wondering whether I have to rerun all my game setup logic or if it partly restores the game.

This is a codepen I’m testing out context loss in: https://codepen.io/orjandh/pen/poWQWXy

@orjan, have you found a solution?

As I understand it, Phaser 3 does not support context loss handling, but this is apparently a planned feature for Phaser 4.

I take it that if the error is down, we can recover only if we reload the game?

I’m bumping this up; I have exactly the same problem. Is there anyone who knows how to deal with this?

All you can do is listen for CONTEXT_LOST and recreate the game or reload the tab.

2 Likes

I am running into the same problem but worse (I think this may be related to ipadOS and ios 17 update but not 100% sure).
Once the webGL context is lost, reloading the tab does not fix the problem. WebGL is unavailable when the game tries to load. Even more troubling is that if I go to another site using webGL, it’s broken too. For instance, I tried to go to get.webgl.org and I don’t see the spinning cube. It’s like the browser remembers webGL was lost and does not even try to restore it when the tab refreshes or a new tab is started.
What’s even more weird is that if I clear browsing data for webgl.org, things start working again on webgl.org (I see the spinning cube) but also on my site. Any idea what’s going on? And more importantly how can I work around this? I can’t just ask users to keep clearing their browsing data.

It seems like an iOS bug. :frowning:

Yes, I found some threads where people noticed the problem in beta 8 and reported it to Apple a month ago. But it looks like they released with the bug anyway. Perhaps it’s deliberate: another attempt at pushing developers towards native apps (which are more profitable for them)?
In any case, we can’t fix Apple’s code, so the question is what can we do about it from our end. I did more testing on this and it seems to happen when the device is closed while the game is up. When you reopen the device and come back to the game, the webGL context is lost (for all tabs, even after refresh). The only 2 things I found to clear it is exiting safari and restarting it, and clearing the cache. Both require user action. Programmatically, the only thing I can think of is to try to create the game with type AUTO and if it fails try it again with CANVAS. The tint (which I use in several places) won’t work but at least it will start. Can anybody think of another approach?

What exactly happens here?

I’m not sure exactly what happens internally within Safari. But here are the steps I followed and what I observed:

  1. Load game. WebGL is initialized and the game works fine.
  2. Leave the game open and lock the ipad (it may also happen automatically after a period of inactivity)
  3. Open the ipad again and go back the page with the game.
  4. Sometimes the page refreshes automatically, sometimes the game is frozen (console says that webGL context is lost)
  5. Refresh the page. When the game object tries to initialize (type: Phaser.AUTO), it fails. console show “webGL not supported”.
  6. Open another tab and the same problem occurs (even if you open another unrelated site that also uses webGL, it will fail). So even though it’s a new tab, it’s like the browser “remembers” that webGL was off and keeps it off forever more.

That means that once the webGL context is lost, it is lost for good until Safari is shutdown and reopened OR cache is cleared.

I found this thread on the Apple developer forum that describes similar issues from multiple people. No resolution. "WebGL: context lost." error when … | Apple Developer Forums

It’s odd because the AUTO check relies on a test creating a WebGL context.

It looks like this will be fixed in 17.1

1 Like