by xmonkee · 4y ago · 0 comments
This isn't wrong, but going one level down, this is how it bites you:
- The imperative DOM api just suuuucks. Please try it for yourself to see. Even if you're writing a very limited bit of reactivity in your app, you will be forced to invent your own declarative framework very soon.
- Very hard to make reusable components (at least before webcomponents, which I haven't used personally)
If you want to stay close to vanilla, I recommend one of the following solutions:
1) https://htmx.org/ 2) https://github.com/hyperhype/hyperscript 3) https://github.com/vuejs/petite-vue
This looks like pug/slim and other templating to remove xml from pages but instead of just something like
body
p
|
This is a test of the text block.
you get something like: var h = require('hyperscript')
h('p', "This is a test of the text block")
and I want to know why would you want the entire page in JS objects?personally I like the way htmx, vue, alpinejs do things with basically embedding actions/events on the existing xml structures. It's more an abstraction over existing html, than totally redoing or replacing html. That seems like a case of 'we knew we could do it, but never stopped to ask ourselves if we should' ...
Turbolinks + stimulusjs is also another great stack for mini-frontend frameworks. Also super fast.. turbolinks can go well w/ anything though including htmx or alpinejs, but there's a lot of tutorials on stimulus+turbolinks.
on click toggle .hidden on .modal
Whereas the parent post is talking about: https://github.com/hyperhype/hyperscript
Unfortunately, the creator of https://hyperscript.org/ hijacked the name even when the term "hyperscript" is still used to refer to dominictarr's project and the `h('p', 'hello world') API of declaring templates in JavaScript (as seen in React, Preact, Mithril, etc.).
The fact that this works, didn't take long to write (though I'm very much still learning this stuff) and is very fast leads me to question your statement that "The imperative DOM api just sucks".
For a small demo like this vanilla JS might make sense but scaling this up does not work (once your rendered HTML reaches a certain size it's jarring when it re-renders, same deal if you have interactive UI that you are rendering out) and if you need to make 10x of these demo sites you are copy/pasting a bunch of code versus leveraging a framework that handles that stuff for you. Yes, frameworks raise your "floor" of code-size but they provide an excellent foundation to build on and skills you can reuse across projects instead of them all being one-off or having to copy/paste a bunch of code.
Yes and no. What are those apps really doing that's fundamentally so much more complicated ? Obviously there's a lot more to their UI's and you'd need to be careful about doing full re-renders. I suspect anything that affects global page layout could become problematic. But I think it should be possible to predetermine the page layout (unless the viewport size changes, then you'd need to re-render everything, but that should be uncommon) and only re-render within div's of preset size.
> I looked at your code and you had to write your own render loop to accomplish this which would break down if you had interactive stuff that you were rendering out
You mean like generating forms/controls ?
I have some other code I'm working on that does that and also doesn't seem particularly hard. For example I'm working on a chrome extension with an options page that updates another page and the local storage as soon as a user changes an option setting.
They have more views, more data, more data that needs to be kept in sync across multiple views. You cannot rerender (and inject/replace) every time the underlying data changes. It works up to a point but once you cross that rubicon it because very painful. I don't know how better to say this or explain it. I write webapps professionally and for side-projects, maintaining any of them in vanilla js would be a non-starter. I have even tried that for "smaller" webapps that "don't need a build step", it doesn't work. SCSS is vastly superior to CSS, TS is superior to JS, and templates that support things like `v-for="item of items"` or `v-if="condition"` or `@click="doTheThing"` are superior to HTML templates with event handlers, loops spitting out html, and conditional rendering of blocks.
> You mean like generating forms/controls ?
Yes. Those things make this all more complicated. I've built exactly what you have in the past and at a certain point it falls down hard. There is a noticeable lag when you change data and go to re-render. If the user is focused on an input and you re-render they lose focus unless you make sure to reset the focus after the render. If you wanted to do something like "As the user types re-render an element to show what they typed" (think example/preview, or even something like putting the name you are typing into the UI as a header) it gets really gross really fast. So you might start having smaller render areas "I'll just re-render this 1 part" but that also gets complicated very quickly and at the end of the day you are just building a worse framework without the benefit of the community.
Then this happened in multiple places. Or hackers didn't like how something worked so they came up with their own spin. I believe React came out of or is strongly influenced by FB, which makes sense - a Single Page App is the design and progressively loading more ads, ahh I mean content.
And here we are building client/server apps with a technology invented for document sharing.
There are 2 frameworks solving the "keep track of state" problem on the server and allowing for dynamic/reactive UI via web sockets instead of XHR: Ruby/Rails 7 and Elixir/Phoenix LiveView. They still use JS but you can limit it to client-side things and maintain state and validations on server.
It's funny to see how responses in this thread jump immediately to reactivity or "skills you can reuse across projects" but without making the case: why?
Facebook is pretty complicated already, once you factor in: * infinite scroll
* different sorting algorithms (top vs new, do they even still have that?)
* real-time commenting and post updates, with responses, reactions, edits, deletions
* dynamic image uploads, resizes, rotations, captions, etc.
* auth
* multi-device sync
* real-time customizable notifications
* real-time sharing
And then it gets even more complicated when you throw in other features like:
* ad management, posting, analytics, event tracking
* chat
* marketplace
* page management and all the permissions and delegations that involves
There's nothing really "static" about Facebook. And even it was designed as a mix of PHP and clientside JS, probably due to its history... if they started from scratch today, who knows what architecture they'd use, but probably not vanilla JS and HTML...
I think the "why" should be aimed more at vanilla, as in "why would you want to reinvent the wheel when there are so many battle-tested solutions to choose from, each made by a team hundreds of times more resourced than you"
I don't think you can judge if the DOM api sucks if you are using a "simple demo app" as your baseline, though.
as the functionality of the app progresses beyond a certain complexity (admittedly, this is nebulously defined), increasing changes to complexity require more time to effect.
beyond a certain point of functionality, the need for a a framework to manage these things becomes emergent. at that point, a single coder can resume making more complex changes with the aide of the framework, than she could do without it.
you're not wrong, you just are not considering a big enough scope over which to evaluate what the manual manipulation of software primitives is worth.
in the end, frameworks aide results beyond the ability of a single coder to implement on their own.
They are of course the valid opinions of a neophyte but don't expect anyone to take you seriously.
I'm always amused at people who make assumptions about who they're talking to online.
Look how slow the text renders when you hit reload.
The content of the <code id="code-container"></code> is not going to change. Just put it inline:
<code id="code-container">
<li>async function n2sort( data, view ) {</li>
<li> const n = data.length;</li>
<li> for( let i = 0; i < n; ++i ) {</li>
<li> for( let j = 0; j < n; ++j ) {</li>
<li> if( data[i] < data[j] ) {</li>
<li> swap( data, i, j );</li>
<li> }</li><li> }</li>
<li> await view.addRow( data, `Step ${i + 1}` );</li>
<li> }</li>
<li>}</li>
</code>
(As a joke you could put the actual <script> tag there and style it like script{white-space:pre;display:block} )The external <link rel="stylesheet" type="text/css" href="style.css"> can also be pasted inline between <style></style> tags.
Same for the script at the end of the page, just put the content between <script></script> tags.
All the way at the top a document type would be useful <!DOCTYPE html>
If you want the steps could be replaced by rendering the entire table and hide each line with css. Pressing step only has to reveal the next line.
I personally consider it a good habit to have the buttons update the location.search part of the url then listen for url changes with window.addEventListener('locationchange', function) and modify the page accordingly. It's not particularly useful for this app but its nice to have an url that represents the state of the application.
> Much better than any framework already.
I doubt that. If my first attempt at a dynamic web app in like 10 years is better than the current frameworks than the state of web development must be much worse than I realized.
> Look how slow the text renders when you hit reload.
You're right, there is a noticeable rendering glitch that I hadn't noticed before, thanks. Maybe this is already an early indication of the limitations of the approach I used here, or at least one of the trade-offs.
> The content of the <code id="code-container"></code> is not going to change. Just put it inline:
One of my goals with this demo was to display the exact same code in the blurb on the left as was being run on the right. Another goal was to minimize the amount of change needed to the basic algorithm implementation to update the display. I was able to get it down to one extra line, but maybe there's a way to avoid that entirely.
> All the way at the top a document type would be useful <!DOCTYPE html>
It's there, you must have missed it (in chrome "view page source" colors that line with very low contrast).
Your other comments about inlining go against most advice I've seen on best practices (with which I happen to agree). For one thing it's much nicer to edit files that are written in one language than in 2 or 3.
> I personally consider it a good habit to have the buttons update the location.search part of the url
Interesting, I'll have to give that one some thought.
> I doubt that. If my first attempt at a dynamic web app in like 10 years is better than the current frameworks than the state of web development must be much worse than I realized.
The state of webdev is indeed terrible, there is progress being made but its hard to tell where and if it is real progress. There was a fun article the other day onhere that explained why React became popular and how that no longer necessarily applies but the popularity persists because it is well known. I cant objectively tell how true that is. What is the better stuff is like... peoples opinion? Frameworks are there because html/css and js have their shortcomings (what those are is more opinions) but I much prefer working with those over hiding them. An abstraction on top of something bad doesn't make it great.
People can glue code fantastic things really fast nowadays but when it stops working it takes a wizard to fix things. With many components of your thing getting updated it is easier for conflicts to arise.
Perhaps the question is: How much performance and stability can you exchange for fast development? Guessing how much performance you are going to lose is not easy to guess, it might be completely irrelevant for the use case, it might be horrific. Will you be around to fix things in the future? Will others have to work on your code? A reasonably sane standard way of doing something is going to be a huge advantage there.
> Your other comments about inlining go against most advice I've seen on best practices (with which I happen to agree). For one thing it's much nicer to edit files that are written in one language than in 2 or 3.
Nah, its all the same. You are gaining nothing. You could ask : how much editing files are you going to do vs how many times are people going to load the page? A http request takes time, the browser doesn't know it has to load a css file and 2 scripts until it receives and parses the html, a new request is made which takes time again. Depending where the user and the server are located a request may take a long time.
I find it quite silly how we need a gaming pc to parse websites nowadays. The user is also in some remote corner of some remote country on ancient hardware.
You should absolutely have external css and js if the user is likely to browse multiple pages. Inline they would have to load it for every page view.
People may also block external js and css files. Add blockers may get rid of your code section and if your app is many files spread out over many folders it is much more likely to break than a single file that contains everything.
The single file will either load and work or the server is not working.
In an ideal world we could have things inline on the front page and tie it to an url so that it can be cached and used on other pages.
Is it trying to provide compatibility with quick templates etc or aim to reach jinja level features parity ?
I like the idea of QuickTemplate a lot but the I find it lacking and annoying to use in various ways. For example, I find its syntax is annoying to write, I don't like having to re-declare the types of my variables whenever I use them and it's error reporting is near useless.
Currently, Neon is just barely at MVP. I'm dogfooding it and adding to it as I find I need things.
https://github.com/frameable/el
It's just ~150 lines / 2kb, and leverages existing browser functionality to accomplish most of the hard parts. Has observability, reactive templates, scoped CSS, no need for a build process, etc.
I just feel afraid to run into a problem that has already been solved in a big framework and get stuck.
Hear about something new, then immediately hear about its multiple successors/replacements.
Check it out, it's the kind of framework you pick and stick with for 20+ years. Zero bullshit compared to its peers.
- mithril.js is an actual web framework. I’ve not tried it myself. - the next comment referenced adamant as being better. There is an MMORPG called RuneScape in which weapons and armour can be smithed of various metals, and treats adamant as 1 tier better than mithril. - similarly orichalcum is a couple of tiers above mithril.
"The unobtrusive JavaScript framework for server-side web applications Unpoly enables fast and flexible frontends for server-rendered HTML views. It has no dependencies and plays nice with existing code."