Search code examples
f#websharperwebsharper.ui.next

Can't get WebSharper Javascript Client to run hosted on Suave


I am trying out the example snippet titled "Adding client-side functionality" from the following page :

https://developers.websharper.com/docs/v4.x/fs/overview

It looks a bit outdated and doesn't compile as is, so based on the original repository where that code was snipped from, this is what I have now.

namespace TestSuaveWs

open WebSharper
open WebSharper.Sitelets
open WebSharper.UI
open WebSharper.UI.Html
open WebSharper.UI.Client

module Server =

    [<Rpc>]
    let DoWork (s: string) = 
        async {
            return System.String(List.ofSeq s |> List.rev |> Array.ofList)
        }

[<JavaScript>]
module Client =

    open WebSharper.JavaScript
    open WebSharper.Html.Client


    let Main () =
        let input = Input [ Attr.Value "" ]
        let output = H1 []
        Div [
            input
            Button [ Text "Send" ]
            |>! OnClick (fun _ _ ->
                    async {
                        let! data = Server.DoWork input.Value
                        output.Text <- data
                    }
                    |> Async.Start
            )
            HR []
            H4 [ Class "text-muted" ] -- Text "The server responded:"
            Div [ Class "jumbotron" ] -< [ output ]
        ]

module TheSite =

    open WebSharper.UI.Server

    [<Website>]
    let MySite =
        Application.SinglePage (fun ctx ->
            Content.Page(
                Body = [
                    h1 [] [ text "Say Hi to the server" ]
                    div [] [ client <@ Client.Main() @> ]
                ]
            )
        )

    open global.Suave
    open Suave.Web
    open WebSharper.Suave

    let webPart = WebSharperAdapter.ToWebPart(MySite, RootDirectory="../..")

Then there's the main program.

namespace TestSuaveWs

module Main =

    open System
    open System.Threading
    open Suave

    [<EntryPoint>]
    let main argv =
        let cts = new CancellationTokenSource()
        let conf = { defaultConfig with cancellationToken = cts.Token }
        let listening, server = startWebServerAsync conf TheSite.webPart
        Async.Start(server, cts.Token)
        printfn "Make requests now"
        Console.ReadKey true |> ignore
        cts.Cancel()
        0

The program runs, and I can see the text "Say Hi to the server" on localhost:8080, but there is nothing below that text. A picture on the page with the example shows what it should look like. There's supposed to be a text input field, a button, and a reply text.

When I open Developer Tools in my Chrome browser, I can see that there's a bunch of similar messages, differing only in the javascript filename, that says "Failed to load resource: the server responded with a status of 404 WebSharper.Main.min.js:1 (Not Found)"

There are no *.js files in the bin\Debug folder. I am running in VS 2019, with .NET Framework 4.7.1.

What am I missing?


Solution

  • I wasn't aware that this very example was available as a template named "WebSharper 4 Suave-hosted Site", which I only found after downloading and installing the WebSharper vsix. That template spun up a project doing exactly what I tried to achieve. So that's the answer. I wish this was hinted at in the documentation page.