Skip to content

The browser blocks the WebAssembly build of the Naylib program due to cross-origin isolation restrictions. #175

Description

@sk-Prime

If you build a program for example tests/basic_window_web.nim it compiles perfectly but in browser if you open DevTools console you will see

Uncaught (in promise) DOMException: Failed to execute 'postMessage' on 'Worker': SharedArrayBuffer transfer requires self.crossOriginIsolated.
    at http://127.0.0.1:8000/main.js:1378:16
    at new Promise (<anonymous>)
    at loadWasmModuleToWorker (http://127.0.0.1:8000/main.js:1295:38)
    at Array.map (<anonymous>)
    at Object.loadWasmModuleToAllWorkers (http://127.0.0.1:8000/main.js:1394:66)
    at http://127.0.0.1:8000/main.js:1247:19
    at callRuntimeCallbacks (http://127.0.0.1:8000/main.js:1069:26)
    at preRun (http://127.0.0.1:8000/main.js:709:3)
    at run (http://127.0.0.1:8000/main.js:11533:3)
    at removeRunDependency (http://127.0.0.1:8000/main.js:820:7)

The workaround is in server, enable cross origin isolation. for example

from http.server import HTTPServer, SimpleHTTPRequestHandler

class IsolatedRequestHandler(SimpleHTTPRequestHandler):
    def end_headers(self):
        self.send_header("Cross-Origin-Opener-Policy", "same-origin")
        self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
        super().end_headers()

if __name__ == "__main__":
    PORT = 8000
    server = HTTPServer(("0.0.0.0", PORT), IsolatedRequestHandler)
    server.serve_forever()

Now it works. However, if you build a game using c language there is no issue about it. So something naylib is doing differently that is causing the above issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions