It works in GTK3, and even weirder, if I write exactly (as much as possible) the same code in C, even using the Gwebgl wrappers instead of calling glClear etc directly, that works in GTK4 and GTK3. I’ve tried it in X and Wayland, on AMD and Intel graphics and even on Fedora as well as Arch. They all do the same thing, gjs and gtk4 doesn’t work.
Also, in every case, GTK/GDK ignores the required_version and use_es settings, and returns a GL version of 4.6. This apparently isn’t backwards compatible with OpenGL (ES) 2.0 which is the API Gwebgl uses to simplify the bindings. So that could have something to do with the problem, but then all versions should fail.
Maybe I could use a “dumb” widget, get its low-level X11/Wayland handle, and create an OpenGL ES 2 context with EGL?