I'm trying to generate SVGs server-side, passing them in to a client using WebSharper's RPC mechanism. I'm basing this off the websharper-web
template, using dotnet core.
For the sake of thoroughness, I render the template like so:
// Site.fs
let Draw ctx =
Templating.Main ctx EndPoint.Home "Home" [
h1 [] [text "Say Hi to the server!"]
div [] [client <@ Artery.Main() @>]
]
I've defined an RPC method that returns my SVG:
module Server =
[<Rpc>]
let FetchDrawing () =
let center = {Svg.Point.X=50; Svg.Point.Y=50}
let r = 25
let circle = Svg.Shape.Circle(center=center, radius=r)
async {
return circle |> Svg.render
}
// Returns an SvgElement.circle with cx, cy, and r set
This is fine so far, but I cannot, for the life of me, figure out how load this properly in the client. The closest I've gotten is:
[<JavaScript>]
module Artery =
let Main() =
let circle = Server.FetchDrawing()
div [] [
SvgElements.svg [SvgAttributes.height "100"
SvgAttributes.width "100"] [
Doc.Async circle
]
]
This:
- Type checks and compiles
- Runs
- Throws runtime JavaScript errors
Specifically, I get TypeError: x is not a function
. SourceMaps link the error deep into a WebSharper library called Snap.fs
, and which sheds... comparatively little light.
Is this a bug in WebSharper? Should this be working? What's the correct way to include an Async<Doc>
in a client-side doc like this? If possible, I'd prefer not to use jQuery -- I'd rather return JS that asynchronously loads in the SVG, rather than having jQuery plug it in later -- but I'm open to JQuery answers if that's the best practice here.