Giter Site home page Giter Site logo

panic when DOM is not present about vecty HOT 7 CLOSED

hexops avatar hexops commented on May 21, 2024
panic when DOM is not present

from vecty.

Comments (7)

slimsag avatar slimsag commented on May 21, 2024 1

Oh, wow, oops, sorry @shurcooL ! And thank you!

I tagged you but it seems I didn't fully understand the issue at hand. I had (very wrongly) thought that gopherjs run markdown.go somehow left out another Go file like when using go run a.go with b.go in the same directory -- and that GopherJS somehow tried to 'best-effort' run it (I was surprised it got past compilation). I see now it was purely due to the runtime js calls -- not due to missing Go code -- as you said.

I imagine to prevent users from running into this by accident I could add a panic if js.Global.Get("document") == undefined, similar to what I've done in https://github.com/gopherjs/vecty/blob/master/gopherjs_only.go -- that might be better than just documenting the package as such.

from vecty.

slimsag avatar slimsag commented on May 21, 2024 1

We already do some environment checks such as https://github.com/gopherjs/vecty/blob/master/gopherjs_only.go so I don't think environment checks are bad. I've already proven they can work properly with unit tests, as well (haven't pushed this code yet, though). For sure, if/when we support server side rendering we'd have to relax the constraints.

I think this would be as simple as placing a check for document == null (psuedocode) inside init.

from vecty.

slimsag avatar slimsag commented on May 21, 2024

Try gopherjs serve and then navigate to http://localhost:8080/github.com/gopherjs/vecty/examples/markdown/

Does that work? (I presume gopherjs run is like go run and hence doesn't do what you expect -- it runs only that one file without dependencies or something..)

from vecty.

hemantasapkota avatar hemantasapkota commented on May 21, 2024

Awesome. That worked. Thanks for speedy response.

from vecty.

slimsag avatar slimsag commented on May 21, 2024

@hemantasapkota awesome :)

@shurcooL would you expect gopherjs to provide a better error message here? or no? I can file an issue if so

from vecty.

dmitshur avatar dmitshur commented on May 21, 2024

What would be a better error?

The error happens on this line:

js.Global.Get("document").Set("title", title)

node's global object doesn't have document and returns undefined. Setting title on undefined causes:

TypeError: Cannot set property 'title' of undefined

It looks like the problem here was a misunderstanding of environments where a Go program compiled to JavaScript can run. That Go package was meant to be run inside of a browser, not in the terminal via node. I usually document such packages to say that.

from vecty.

anxiousmodernman avatar anxiousmodernman commented on May 21, 2024

This particular error occurred on SetTitle, but not everyone will call this function when they're getting started. They might jump straight to RenderBody, or the planned RenderInto, etc.

Package-level init is one place to do this, perhaps the only place. Alternatively, you might have some kind of new function, vecty.BrowserInit, but then everyone would need to call this.

FWIW, I might caution against adding environment checks like this in vecty. If you have a goal to be able to run unit tests against vecty code in a non-browser environment, or do server side rendering, it may not be possible to do these kinds of environment checks in a way that works for every use case, existing or planned.

from vecty.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.