phaser: Loader can't load from file:// [$50]
This Issue is about a bug in the API (v3.3.0):
I have tried wrapping a Phaser app in Cordova/ React Native.
It mostly worked but there are issues with loading files. Neither images nor json can be loaded, they fail without a proper error.
After close debugging I noticed that the “Request Type” comes up as XHR whereas with Phaser 2.x it’s image/ json like it should be.
I have set up a very basic example textures/sprite sheet.js and a more complex project, both fail.
Could this be related to #2670 or #2485? Did this fix get lost in 3.x?
Any help is very appreciated, did anybody manage to wrap a Phaser 3 project with Cordova?
Cheers!
p.s.: www.zip the content of www within the Cordova app
UPDATE 29.03:
I have looked into this more. It seems like the issue has to do with the Phaser 3 loader not being able loading resources from file://. When I open the basic texture examples from Phaser 2 without being served from a server they work just fine, the Phaser 3 ones don’t. Seems the loader does something differently so CORS becomes an issue.
There is a $50 open bounty on this issue. Add to the bounty at Bountysource. </bountysource-plugin>
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 25 (6 by maintainers)
Hi guys, Thanks for investigating this, I guess if it’s not a pure Phaser problem, then this can be closed. However, I would believe that we need an “official” way of dealing with this, since we will not be the only ones having this issue. A Cordova section in tutorials or somewhere exposed should clarify how Phaser can work with Android/iOS. There might be other “tweaks” to do for mobile development as well and having a dedicated section will greatly help everyone.
I was very excited to start a project that uses phaser3-typescript-webpack-cordova. Everything works perfectly until I start testing the cordova with the build package. The phaser assets loader isn’t designed for loading assets from IOS. Really hope there’s an official instruction of how to load assets from Cordova wrapped app. Thanks.
Adding the cordova-plugin-ionic-webview as suggested by @ghybs and @sftsk worked for me.
At least it started loading my json file then. For the spritesheets to show up i had to change:
to:
In the cordova index.html. Adding the
blob:in at the end.Still need to add sound to my current project though. If i need to do anything special for that i’ll post it here.
I have finally had time looking into this and got into the same solution as proposed by @ghybs .
Installing https://github.com/ionic-team/cordova-plugin-ionic-webview with Cordova fixes the issue. This will only be a fix for iOS, although probably for Android exists something similar.
I don’t think there is anything to be done within Phaser as this
XHR Cross Originpolicy seems to be a limitation that can’t be easily worked around on mobile. Chrome has a flag--disable-web-securitythat will allow the requests, not sure if that could be an option for Android.Anyway, thanks for all the help, issue can be closed from my point of view. Perhaps add some instructions to the manual of needing the mentioned plugin when wrapping an app with Cordova.
Hi,
@sftsk: incompatibility of Phaser 3 with direct loading through
file://protocol looks to be a design choice, as explained in the Getting Started tutorial part 1: https://phaser.io/tutorials/getting-started-phaser3As described the solution for desktop browsers is to serve files through a server, which is a very standard practice nowadays, even for development.
As for Cordova in iOS, which is OP’s described situation, the above solution is more complex to implement, but it is still a known and widely used practice as well. There are many Cordova plugins that patch the Webview, for example:
While @Huararanga’s workaround is probably valid, a drawback is that all files are now base64 strings in your code, making it much heavier. Furthermore, browsers and Webview cache files loaded the standard way, so rendering multiple instances of them is no problem. On the contrary, performance degrades when using base64 data multiple times.
Hope this helps.