←back to thread

153 points yar-kravtsov | 10 comments | | HN request time: 0.214s | source | bottom

I built a Vite plugin that lets you write Go code directly in .js files using a "use golang" directive. It compiles to WebAssembly automatically.
1. liampulles ◴[] No.45719276[source]
Just be careful with this backend-code-in-frontend stuff. If it's needed for some computationally expensive logic that is logically client side, then fine. But be wary of letting the client dictate business rules and having open-for-anything APIs (GraphQL is particularly prone to this).

I've seen teams do this in the wild more than once.

replies(3): >>45720892 #>>45721373 #>>45724829 #
2. nesarkvechnep ◴[] No.45720892[source]
REST is the solution to this but it's reduced to JSON RPC over HTTP nowadays.
replies(1): >>45740465 #
3. rs186 ◴[] No.45721373[source]
Well, the "Is this a good idea?" section in the README already addresses the issue.
4. tkzed49 ◴[] No.45724829[source]
it's not backend code, it generates wasm that runs in the browser.
replies(1): >>45725113 #
5. liampulles ◴[] No.45725113[source]
What I meant was using a backed oriented language for frontend oriented work. My shorthand was unclear, apologies.
replies(1): >>45730257 #
6. solumunus ◴[] No.45730257{3}[source]
> What I meant was using a backed oriented language for frontend oriented work.

And why exactly? Your original comment made sense but it was irrelevant to the OP. This one just doesn’t make sense but I could be missing something.

replies(1): >>45731481 #
7. liampulles ◴[] No.45731481{4}[source]
Yes you are right: this is a subsequent point and not related to my original point. Apologies for that.

Did you want me to expand my thoughts on "backed oriented language for frontend oriented work", or does that address your query?

replies(1): >>45735242 #
8. solumunus ◴[] No.45735242{5}[source]
Yes, I don’t understand your issue with using “back end languages” in the front end.
replies(1): >>45740412 #
9. liampulles ◴[] No.45740412{6}[source]
Ok sure, here are my thoughts.

This is obvious but it needs to be said: Backend languages are designed for backend work, and frontend languages for frontend work. Where this becomes a real pain point is where the design goals of the language run counter, and probably the chief one is around modelling business rules.

It is the job of the backend to constrain itself down into fitting the business rules and nothing else, and backend languages aid this by allowing one to model well-defined types and put defensive guards and deal with bad behaviour in a number of ways (e.g. there is often a distinction between a runtime error and non-runtime errors).

It is the job of the frontend (or at least, what my ideal frontend would be) to have good UX and to delegate to the backend for business rules. Indeed in my ideal, the backend would dictate the HTML content to the greatest degree possible. The coding that is needed on the frontend is for DOM manipulation to add some dynamic feel to the UI, and avoid full page reloads for small adjustments. A dynamically typed scripting language (e.g. Javascript) is good for this, because it is quick to hack and tweak an experimental view, review it with users, adjust, and repeat (which is at least how I go about getting the UX good enough).

Using a typed backend language on the frontend would get in the way of me just hacking the view, which is the appropriate mode of programming for a dumb client (dumb client being my ideal).

Also, and where it does tie in with my original comment, is that I do think using a backend language on the frontend invites putting business rules in the UI code. I think that because I've been on projects where that has happened, and I understand the instinct- why pivot from my frontend coding and go and figure out what I need to modify on the backend to code a feature when it is seemingly easy for me to model it on the frontend just as well? Infact, why not put all the logic in the frontend and let the backend be a dumb CRUD REST API/a GraphQL layer above the DB?

Conversely, if it is not easy to do much beyond DOM manipulation on the frontend (because the language and setup don't make it easy), and I am forced to modify the business rules in the backend, then fantastic.

10. liampulles ◴[] No.45740465[source]
If we're talking about a HTML server (a REST API) then I agree, but if it is a choice between JSON REST and JSON RPC, I'd take JSON RPC any day to be honest with you.

a REST API needs to be descriptive enough and have a wide enough contract with the client that the response can modify the behaviour of the client so as to deal with any multitude of situations going on with the server. This works great if the response is HTML and the client is a browser, as the HTML dictates where and how to interact with the server (e.g. a link is a GET request to XYZ, followed by a page load). For JSON REST to meet that bar one needs JSON+HATEOAS, and having worked on a project that tried that, let me tell you that there is HATE aplenty to be found in trying to make that work.

So if we abandon the strict notion of what REST is, then what does JSON REST mean? In my experience, its been a lot of arguing over what paths and methods and resources to use, which at best are a waste of time (because no one is going to see the choice, its just whatever your JS lib is going to call and your backend is going to return) and at worse it puts bad constraints on how the backend is modeled by forcing one to do it in terms of Resources for ones REST API to work effectively.

In my opinion, its much better to use an RPC API which simply describes API "functions". These APIs can work over any number of actual db resources (and sometimes none) and importantly, leave you the time and the freedom to model your backend in terms of business rules and not "RESTful" norms.