juicycleff / flutter-unity-view-widget

Embeddable unity game engine view for Flutter. Advance demo here https://github.com/juicycleff/flutter-unity-arkit-demo
BSD 3-Clause "New" or "Revised" License
2.15k stars 525 forks source link

MIME Type Issues Even With Brotli/Disabled Compression Formatting #970

Open nsdod opened 3 months ago

nsdod commented 3 months ago

Hi! I'm seeing an issue where the Unity Flutter widget doesn't load properly when navigated to from another page (WebGL). The error log appears as follows:

errors.dart:296 Uncaught (in promise) Error
    at Object.throw_ [as throw] (errors.dart:296:3)
    at Object.assertFailed (errors.dart:29:3)
    at get page (page_view.dart:170:17)
    at page_view.PageController.new.nextPage (page_view.dart:223:26)
    at onboarding_widget._OnboardingWidgetState.new.<anonymous> (onboarding_widget.dart:41:40)
    at Generator.next (<anonymous>)
    at async_patch.dart:45:50
    at _RootZone.runUnary (zone.dart:1661:54)
    at _FutureListener.thenAwait.handleValue (future_impl.dart:163:18)
    at handleValueCallback (future_impl.dart:847:44)
    at _Future._propagateToListeners (future_impl.dart:876:13)
    at [_complete] (future_impl.dart:643:7)
    at future.dart:420:15
    at internalCallback (isolate_helper.dart:48:11)
[...]

js_primitives.dart:28 Created Unity scene: attach controller
flutter.js:3 Exception while loading service worker: DOMException: Failed to register a ServiceWorker for scope ('http://localhost:55405/modEdit/UnityLibrary/') with script ('http://localhost:55405/modEdit/UnityLibrary/flutter_service_worker.js?v='): The script has an unsupported MIME type ('text/html').
(anonymous) @ flutter.js:3
Promise.catch (async)
loadEntrypoint @ flutter.js:3
(anonymous) @ index.html:66
load (async)
(anonymous) @ index.html:64

index.html:1 Refused to execute script from 'http://localhost:55405/modEdit/UnityLibrary/main.dart.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
2errors.dart:296 Uncaught Error
    at Object.throw_ [as throw] (errors.dart:296:3)
    at Object._failedAsCheck (rti.dart:1388:3)
    at dart_rti.Rti.new._generalAsCheckImplementation (rti.dart:1366:3)
    at Object._argumentErrors (operations.dart:243:33)
    at Object._checkAndCall (operations.dart:422:22)
    at Object.callMethod (operations.dart:494:10)
    at Object.dsend (operations.dart:498:5)
    at web_unity_widget_controller.dart:114:27

The error references a MIME type issue that, from previous reports, appear to usually be caused by selecting the GZIP compression format. However, this Unity project is built to WEBGL with the compression formatting disabled (and changing it to Brotli doesn't fix the issue). The UnityLibrary assets appear to be in the correct place after exporting.

The 'modEdit' reference as seen in this error is the previous page from which the Unity widget's page is navigated to. Could this have something to do with how the iframe used to contain the Unity application loads into the widget, or a strange interaction with the Navigation stack? The base application is made with Flutterflow. However, the Unity Flutter widget is added to the project manually, after exporting the project to into Dart/Flutter code. Lottie animations are in use for page transitions.

Browser: Google Chrome 126.0.6478.127 Unity version: 2022.3.27f1 Flutter: 3.22.2 Dart: 3.4.3

nsdod commented 3 months ago

Update! I have more information on what's going on here.

So, the iframe generated by the Unity Widget (I think) is trying to access the UnityLibrary directory from a parent directory that does not exist in the build. In particular, the page with the Unity widget on it is navigated to from a page called 'modEdit' via a button press. The iframe that's meant to have the Unity app in it looks like this:

But no such directory/file http://localhost:55405/modEdit/UnityLibrary/main.dart.js exists. I'm not sure why the iframe is pointed to this location when it should be looking in the root directory of the app. Does anyone have any ideas on what's causing this or how to correct it? Any additional information needed?

timbotimbo commented 3 months ago

Does it make a difference if you use the previous version of the plugin (2022.2.0) ? 2022.2.1 included a PR to allow unity to work when Flutter is not at the root of the domain, see the change here.

I believe that is also the only location in the plugin where a web directory is defined.

If this doesn't help, maybe try creating a small reproduction project that triggers this, which uses as few other plugins as possible.