scala.js - Writing scala-js frontend framework with server-side rendering. Unable to use scala-js-dom on server -
i'm writing scala-js frontend framework, key feature of server-side rendering. idea there components manipulate dom document.createelement
, element.appendchild
, others. on server i'd subclass htmldocument
, element
, others, override methods server dom implementation can converted plain string html. added scalajs-dom_sjs
dependency server module , tried that. htmldocument
, element
, other classes have calls js.native
inside constructors throw exceptions saying "use jvm version of library". doesn't exist obviously. use other way , implement own dom library, twice work, cause i'd have implement on server , client, while using first approach i'd implement once on server.
so question is: why forbidden use scala-js library versions on server strictly , there work around it?
the reason forbidden that, noticed, dom api full of js.native
s. these classes not implemented in scala. part of browser's dom api, not have equivalent on jvm. cannot use types defined in scalajs-dom
on jvm , expect them useful. implementations of methods come from?
you indeed need implement own dom-like library jvm side. if not want "reimplement" on client side, reuse org.scalajs.dom
namespace classes, , give them same structure , types in scalajs-dom
(except won't extend js.any
, obviously).
note semantically dubious. types extending js.any
not have same semantics normal scala types. might able come "compatible enough" api normal use, it's still dubious.
usually, enable so-called isomorphic dom manipulations on server , client, 1 write dom-agnostic cross-compiling library. on client side, offer "rendering" function actual dom nodes; , on server side, render strings sent client in html.
this precisely scalatags does.
Comments
Post a Comment