Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web worker bundle DCE-d incorrectly in an SPA project #1308

Closed
diegopego opened this issue Jan 6, 2023 · 4 comments
Closed

web worker bundle DCE-d incorrectly in an SPA project #1308

diegopego opened this issue Jan 6, 2023 · 4 comments
Assignees
Projects

Comments

@diegopego
Copy link
Contributor

I wrote a web worker sample and got this error on the developer tools console:

Uncaught TypeError: Cannot read properties of undefined (reading 'prototype')

I used this template:
dotnet new websharper-spa -lang f# -n MySPA

namespace MySPA

open WebSharper
open WebSharper.JavaScript
open WebSharper.UI
open WebSharper.UI.Client
open WebSharper.UI.Templating

[<JavaScript>]
module Client =
    // The templates are loaded from the DOM, so you just can edit index.html
    // and refresh your browser, no need to recompile unless you add or remove holes.
    type IndexTemplate = Template<"wwwroot/index.html", ClientLoad.FromDocument>

    let People =
        ListModel.FromSeq [
            "John"
            "Paul"
        ]

    [<SPAEntryPoint>]
    let Main () =
        let myWorker = new Worker(fun self ->
            Console.Log "This was written from the worker!"
            self.PostMessage("This worker's job is done, it can be terminated.")
        )
        
        myWorker.Onmessage <- fun event ->
            Console.Log event.Data
            myWorker.Terminate()

        let newName = Var.Create ""

        IndexTemplate.Main()
            .ListContainer(
                People.View.DocSeqCached(fun (name: string) ->
                    IndexTemplate.ListItem().Name(name).Doc()
                )
            )
            .Name(newName)
            .Add(fun _ ->
                People.Add(newName.Value)
                newName.Value <- ""
            )
            .Doc()
        |> Doc.RunById "main"

@Jand42
Copy link
Member

Jand42 commented Jan 7, 2023

@diegopego Thanks for the report!

This must be a problem with web workers defined within an SPA project. When bundling runs for the web worker, WebSharper mistakenly also includes the all the code based on SPAEntryPoint in the MySPA.worker.js bundle too, which in this case has WS.UI stuff using DOM Elements, not available in web worker context.

To fix: web worker bundling should ignore the SPAEntryPoint and JavaScriptExport attributes.

@granicz
Copy link
Member

granicz commented Jan 7, 2023

Well, just to be clear, this needs a compiler fix.

@diegopego
Copy link
Contributor Author

I'm always willing to learn more in order to help. Thank you!

@Jand42 Jand42 changed the title web worker Uncaught TypeError (reading 'prototype') web worker bundle DCE-d incorrectly in an SPA project Jan 8, 2023
@Jand42
Copy link
Member

Jand42 commented Jan 9, 2023

@diegopego Thanks for the report, I have pushed small fix, will do a CI build soon

@Jand42 Jand42 closed this as completed Jan 9, 2023
@granicz granicz added this to High priority for current version in Releases May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Releases
High priority for current version
Development

No branches or pull requests

3 participants