Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetching assets is broken in web workers #12125

Closed
allsey87 opened this issue Feb 26, 2024 · 2 comments · Fixed by #12134
Closed

Fetching assets is broken in web workers #12125

allsey87 opened this issue Feb 26, 2024 · 2 comments · Fixed by #12134
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Bug An unexpected or incorrect behavior O-Web Specific to web (WASM) builds

Comments

@allsey87
Copy link
Contributor

allsey87 commented Feb 26, 2024

Bevy version

0.13

Relevant system information

WebAssembly/Web Worker

What you did

Ran Bevy in a Web Worker with an OffscreenCanvas and routed events over postMessage.

What went wrong

Everything was working nicely until I had to fetch a GLTF asset and I kept getting a panic. Turns out that HttpWasmAssetReader::fetch_bytes from bevy_asset assumes at wasm.rs#41 that it is running in a window and not in a worker.

Note that gloo-net used to have a similar issue but resolved the issue by probing the global object to see whether it was a web_sys::Window or a web_sys::WorkerGlobalScope in request.rs#323.

Additional information

This issue could be resolved by either adding a dependency on gloo-net or duplicating its logic into bevy_asset. For the latter approach, it might also be an idea to then add an option field to the HttpWasmAssetReader that contains the probed global object type once it has been "resolved".

@allsey87 allsey87 added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Feb 26, 2024
@mockersf mockersf added A-Assets Load files from disk to use for things like images, models, and sounds O-Web Specific to web (WASM) builds and removed S-Needs-Triage This issue needs to be labelled labels Feb 26, 2024
@mockersf
Copy link
Member

Would you have an example project that you could share with this setup?

@allsey87
Copy link
Contributor Author

allsey87 commented Feb 26, 2024

@mockersf I think it would be nice to have a standalone web worker example but I also think it needs to be discussed first since there are various approaches that can be taken.

github-merge-queue bot pushed a commit that referenced this issue Mar 25, 2024
# Objective

This PR fixes #12125

## Solution

The logic in this PR was borrowed from gloo-net and essentially probes
the global Javascript context to see if we are in a window or a worker
before calling `fetch_with_str`.

---------

Co-authored-by: Zachary Harrold <zac@harrold.com.au>
mockersf pushed a commit that referenced this issue Apr 1, 2024
# Objective

This PR fixes #12125

## Solution

The logic in this PR was borrowed from gloo-net and essentially probes
the global Javascript context to see if we are in a window or a worker
before calling `fetch_with_str`.

---------

Co-authored-by: Zachary Harrold <zac@harrold.com.au>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Bug An unexpected or incorrect behavior O-Web Specific to web (WASM) builds
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants