News for hackers. Stuff that matters.
Re:Alpine.js: A minimal framework for composing JavaScript b... by lf-non on Monday February 10, 2020 @06:01AM (#22288048)
Pretty much all mainstream frontend libraries have moved away from dom based templating (or augmenting dynamic behavior through attributes) for good reason. It simply becomes unmaintainable for anything more complex than basic interactivity.

This approach can't leverage type systems for javascript (like TS, BS, flow etc.)

It is easy to end up with mistakes that don't surface as javascript errors and are swallowed completely.

You hit weird quirks when browser injects additional nodes (eg. tbody in tables) or restructures invalid markup (eg. div inside p).

Ostensibly this approach would help with progressive enhancement, but too often careless usage results in flashes of broken UI which would then fix themselves when javascript kicks in. Then you need hacks to circumvent those with hacks like ng-cloak.

Also, in an ideal world this would help with handoff when working with web-designers who are familiar with only html/css but that rarely happens in practice. Every time a web-designer rearranges things in the dom hierarchy the parent-child associations break and that again needs comprehensive testing.

Having worked with applications written using knockoutjs as well angular 1 (both of which used similar approaches) which organically grew over multiple years and (to put gently) didn't age gracefully, I wouldn't recommend this approach to anyone.

If you can't (or don't want to) go down the road of a full SPA but do want to build interactive applications with non-trivial amounts of javascript, lit-element [1], Vue [2], Strudel [3] and SPF [4] are much better solutions.

[1] https://lit-element.polymer-project.org/

[2] https://vuejs.org/

[3] https://strudeljs.org/

[4] https://github.com/youtube/spfjs

Re:Alpine.js: A minimal framework for composing JavaScript b... by mstade on Monday February 10, 2020 @09:16AM (#22289250)
I'm not the author and I've never used this library, but I think your comment is unnecessarily dismissive anyway. You're not necessarily wrong, but I feel there's ample room in the space between wanting to hack something together and built according to this week's best practices.

Seems to me this tool sits squarely in that first category, enabling rapid prototyping at a lower cost, while still being flexible enough to allow for a wide range of capabilities. I don't get the feeling at all from the introduction that this is meant to compete with Vue, React, Angular et al. but rather be a complement – the tool to reach for when you have an idea and want to hash it out without being encumbered by process.

For what it's worth, I work like this all the time. I have a collection of simple utility functions that I'll include in an html file – F5 is my build process. It's useless for building anything serious, that's when I reach for more structure. I can definitely see something like Alpine replace my homegrown tools for this.

Re:Alpine.js: A minimal framework for composing JavaScript b... by rolae on Monday February 10, 2020 @06:14AM (#22288106)
Alpine.js really was created for javascript sprinkles, where even vue.js might be overkill. For example, if you have an app, but you need to have dropdowns and modals. Then something like alpine.js can be nice.

So it is actually not intended for things more complex than basic interactivity. And what you build with it, should be simple enough you don't need any type system.

For people interested, there is a good episode on FullStackRadio on the topic http://www.fullstackradio.com/132

Re:Alpine.js: A minimal framework for composing JavaScript b... by lf-non on Monday February 10, 2020 @11:52AM (#22290801)
Thanks for clarifying on this. This was not the message I inferred by browsing through the README and the examples.

The wording in the README says that Alpine "offers you the reactive and declarative nature of big frameworks like Vue or React at a much lower cost" which seemed to indicate that it was targeting a similar audience as those libraries.

The intro also didn't really mention anything about it being suited primarily for applications without needing complex composition of logic or any indication of what the authors feel about how big is too big to be suitable for alpine.

Re:Alpine.js: A minimal framework for composing JavaScript b... by kreetx on Monday February 10, 2020 @09:03AM (#22289149)
What about Svelte as a light-weight framework -- it compiles itself out (there is no library included in the final app).
Re:Alpine.js: A minimal framework for composing JavaScript b... by ratww on Monday February 10, 2020 @09:05AM (#22289162)
Svelte is great but requires a compilation step, which is probably the kind of complexity that potential users of Alpine.js are trying to avoid.
Re:Alpine.js: A minimal framework for composing JavaScript b... by ecmascript on Monday February 10, 2020 @09:42AM (#22289475)
Wow, such an elitist comment.

So you mean endless webpack configurations with tons of javascript libs are more maintainable?

The reverse can be said for SPA-like frontends in which you put forward as the better choice. It's unnecessarily complex and one error and the whole page becomes unresponsive and crashes. How many times haven't we all clicked on a button and because the developer didn't handle some error in a good way the entire page must be reloaded and all state is lost?

You want a good placement on search engines? Well, good luck chuck. Unless you utilize server rendered pages you are out of luck basically. This is also a big hassle. Also server rendered pages are usually slow as fuck compared to a normal multi page app.

I am using lit-html myself and like it, but it's not necessarily better than dom based templating imo. For some things I believe it to be worse. Right now I am on a slow and shaky connection and will be for a while, sites like HN which is a classic multi page app without any javascript loads fast and works great. Pages like youtube and the new reddit (old works great) works like total shite.

Re:Alpine.js: A minimal framework for composing JavaScript b... by eyelidlessness on Monday February 10, 2020 @10:44AM (#22290045)
Wow, you responded to a pretty thoughtful post with insults, hyperbole and strawmen, and FUD.

> So you mean endless webpack configurations with tons of javascript libs are more maintainable?

I don't think that's what the comment claimed at all. However, I think the answer to the less hyperbolic version of this question is: maybe. It's pretty orthogonal to the discussion though.

> The reverse can be said for SPA-like frontends in which you put forward as the better choice. It's unnecessarily complex and one error and the whole page becomes unresponsive and crashes. How many times haven't we all clicked on a button and because the developer didn't handle some error in a good way the entire page must be reloaded and all state is lost?

I have no idea where you got the idea that this is some defining feature of SPAs, but this is something that can happen with jQuery spaghetti and it's something that almost never happens with React.

> You want a good placement on search engines? Well, good luck chuck. Unless you utilize server rendered pages you are out of luck basically. This is also a big hassle. Also server rendered pages are usually slow as fuck compared to a normal multi page app.

So... I don't know how you're doing this, and admittedly I was away from frontend dev for a few years so I'm sure I missed some pain in the meantime, but I recently started a project with Next.js, and... this is the exact opposite of my experience. SSR is super fast, it only happens once, you get static HTML output and your site is basically as fast as your web server/CDN. All of this without any config.

Re:Alpine.js: A minimal framework for composing JavaScript b... by ecmascript on Monday February 10, 2020 @11:06AM (#22290276)
> So... I don't know how you're doing this, and admittedly I was away from frontend dev for a few years so I'm sure I missed some pain in the meantime, but I recently started a project with Next.js, and... this is the exact opposite of my experience. SSR is super fast, it only happens once, you get static HTML output and your site is basically as fast as your web server/CDN. All of this without any config.

This is only partly true if you use a big framrework like Next or Nuxt but it also means you will heavily buy into whatever these frameworks choose for you. For example, a node backend.

The pages are not faster, especially after adding some complexity like most websites require. The time to render is higher, input lag is commonplace. You're sending the html for that site + all the javascript that needs to be parsed to get that SPA-app that everyone wants.

And as soon as you step outside what the framework requires you too, for example using another backend. Then you're back to a classical SPA-application.

I like SPA-applications that are done well or when they are required, but the complexity to create one is a lot higher because the developers needs to reimplement what the browser originally did for you.

Re:Alpine.js: A minimal framework for composing JavaScript b... by eyelidlessness on Monday February 10, 2020 @12:00PM (#22290892)
> This is only partly true if you use a big framrework like Next or Nuxt but it also means you will heavily buy into whatever these frameworks choose for you. For example, a node backend.

I mean this in the kindest way: I don't think you fully understand what you're talking about. Next may provide some batteries, but it's hardly a "framework". It does not require you to use any particular part of its functionality. For instance, the site I'm building does not have a backend, node or otherwise, at all. If your server-rendered pages don't need to perform anything asynchronously to render, it just spits out HTML files and you can use whatever you want for a backend.

> The pages are not faster, especially after adding some complexity like most websites require. The time to render is higher, input lag is commonplace. You're sending the html for that site + all the javascript that needs to be parsed to get that SPA-app that everyone wants.

This is simply not an accurate representation of what I am experiencing with Next. It produces static HTML files. They load as fast as any other HTML files. If you do need to dynamically load data on the server, yes you are constrained by the server's performance generating that data, but that is true of any server-rendered dynamic data on any platform.

> And as soon as you step outside what the framework requires you too, for example using another backend. Then you're back to a classical SPA-application.

Okay? Yes, using a tool for otherwise than its intended purpose is definitely more complex than using it for its intended purpose. But like... Next with another backend is basically Create React App with a simple built in router and some compilation optimizations that you don't have to configure.

> I like SPA-applications that are done well or when they are required, but the complexity to create one is a lot higher because the developers needs to reimplement what the browser originally did for you.

You really don't. To the extent SPAs "reimplement" anything, these things are all basically solved problems.

- - -

I just want to add that there are use cases for something like Next that aren't necessarily "single page app". I am using it as a static site generator that lets me express my components as React components, which is renderer-agnostic should I ever want to render to another target, and which has a huge ecosystem I can tap into (for example I am using mdxjs to produce most of this site's content in Markdown with the ability to customize how certainn parts of the MD content is rendered using... React components).

This site is fully static. It's not "slow", it doesn't require a "framework" or a "backend". It is by far the best static site dev experience I've ever had.

Re:Alpine.js: A minimal framework for composing JavaScript b... by lf-non on Monday February 10, 2020 @11:44AM (#22290709)
I didn't mean to imply that everyone should start using SPAs or people should eschew server-rendered pages.

While search engines have gotten quite better handling javascript, but that was not the point of my comment.

I was mainly outlining my own subpar experience working with libraries having similar approaches in medium-size teams (~30 devs, about 60% of them juniors).

I just hope that people evaluate the potential drawbacks of this approach before jumping into this because it is convenient to get started with.

Angular 1 was also pretty popular in its glory days and as I have learnt the hard way popularity is not a good indicator of either longevity or quality.

I also suggested some alternatives that I have found in my experience to be better suited towards the task of enhancing server-rendered markup.

If one has evaluated related alternatives, weighed pros and cons and settled on alpine, well, power to them.

Re:Alpine.js: A minimal framework for composing JavaScript b... by spankalee on Monday February 10, 2020 @12:24PM (#22291111)
Hi. Author ot lit-html here.

While for things like full apps, complex components, or any project leaning heavily on type-checking, I obviously mostly agree with you... I'm very intrigued by projects like this and Intercooler.js. I think there's a lot to like here.

JavaScript is a great place to merge data and DOM because data is so much easier to work with in JS than HTML, but if you're starting from HTML already and just want to incrementally add a bit of interactivity these approaches are really appealing. I think we should strive to make the complexity and learning curves as smooth as possible from static pages to full apps. These do that. lit-html and React do in some ways (you can almost copy and paste HTML into JS and have it work) so long as you're very comfortable in JS to begin with.

I've experimented with plain HTML dialects of lit-html and hope to get back to that so that we can have templates-in-HTML and templates-in-JS share semantics and implementation.

Re:Alpine.js: A minimal framework for composing JavaScript b... by jmull on Monday February 10, 2020 @10:01AM (#22289638)
No general purpose framework (or language or process or runtime model) will prevent you from making a project unmaintainable over time if you don't have a design and otherwise keep things organized.

It's inherent in the flexibility that a general-purpose framework provides.

Conversely, if you stay organized, you can maintain long lived complex systems based using frameworks of a variety of styles.

I can assure you there are plenty of old DOM-template-based systems around.