☑ Cross-origin error when self-hosting

This error is because textures used by WebGL must either be served from the same domain (origin) as the main page or they must be served with the correct cross-origin headers.

For the server: if your assets are served from a different domain you must ensure they have the correct CORS headers.

For the game/client there are two options.

First: If you set the ASSET_PREFIX config in the index.html to a URL, then all assets will be loaded from this prefix. If the ASSET_PREFIX is an absolute URL (for example, starting with “https://”) then we enable cross-origin loading on the textures.

Second: You can manually enable cross origin texture loading with this code:

var handler = this.app.loader.getHandler("texture");
handler.crossOrigin = "anonymous";

You must run this code before any textures are requested.

1 Like