phronmophobic/membrane

Script tag in head causes error

hoclun-rigsep opened this issue · 3 comments

Not a membrane issue per se but it cost me way too much of my life when trying to follow the "Membrane with WebGL target" doc: putting the script tag in the head rather than the body broke everything.

Sorry that set you back. Do you have a stacktrace for the error you ran into? Maybe it's possible to update the code to work regardless of where the script tag is placed.

I think it's just that the DOM isn't loaded yet, so document.body is undefined.

TypeError: Cannot read properties of null (reading 'appendChild')
    at Function.eval [as cljs$core$IFn$_invoke$arity$4] (webgl.cljs:162:24)
    at Function.eval [as cljs$core$IFn$_invoke$arity$3] (webgl.cljs:141:5)
    at eval (webgl.cljs:165:2)
    at eval (<anonymous>)
    at goog.globalEval (main.js:434:11)
    at env.evalLoad (main.js:1405:12)
    at main.js:1568:12

In (load-font):

   (let [link (.createElement js/document "link")]
     (doto link
       (.setAttribute "rel" "stylesheet")
       (.setAttribute "href" stylesheet-url))
     (.appendChild (-> js/document .-body)            ; this throws
                   link))))

Sounds good. The membrane.webgl namespace probably shouldn't do anything on load anyway. It seems like this issue and #49 can be fixed by:

  • Only running setup when necessary
  • Having setup make sure the page has loaded

With those changes, it should support any script tag location or loading mechanism.