News for hackers. Stuff that matters.
Re:Reimagining front-end web development with htmx and hyper... by eurasiantiger on Saturday July 02, 2022 @12:44AM (#31956320)
You don’t need bundlers or build systems. ES modules work in all modern browsers without any build tooling, imports and all.

HTTP/2 mitigates the TCP overhead.

You can have an unbundled, unminified web app load quicker than the average bundled and minified React app.

Best of all, you can’t use NPM modules so your codebase can stay ultra-clean!

Re:Reimagining front-end web development with htmx and hyper... by josephg on Saturday July 02, 2022 @01:52AM (#31956614)
Sure, but I can’t run the same code in nodejs for server side rendering.

And wasn’t server push for HTTP/2 removed by web browsers? If the browser needs to do a round-trip to the server to fetch every individual javascript file (and it doesn’t even know which files it needs up front) then the result will be way slower than a bundler. I would expect performance to get worse, not better using this approach.

Re:Reimagining front-end web development with htmx and hyper... by cityzen on Saturday July 02, 2022 @02:29AM (#31956758)
I don’t get the need to use js for SSR? Why not PHP/Go/Python/Ruby… ?
Re:Reimagining front-end web development with htmx and hyper... by danielheath on Saturday July 02, 2022 @03:04AM (#31956874)
If you use react to create the DOM, you can do SSR in nodejs to re-use the same code.

Not clear to me that the added complexity is worth it personally.

Re:Reimagining front-end web development with htmx and hyper... by zelphirkalt on Sunday July 03, 2022 @04:28AM (#31966107)
Also this re-use tricks people into tightly coupling frontend view logic with backend logic, making a mess of everything.
Re:Reimagining front-end web development with htmx and hyper... by eurasiantiger on Sunday July 03, 2022 @06:14AM (#31966553)
No it doesn’t.

SSR is typically just a way to pre-render the client app into (critical) static assets that can be delivered to the browser quickly, so it can display at least some content before the client app loads, runs and ”hydrates” the server-generated HTML back into the exactly same interactive app (e.g. a React app) that was used to generate the SSR assets.

Re:Reimagining front-end web development with htmx and hyper... by zelphirkalt on Sunday July 03, 2022 @09:09AM (#31967437)
I know what SSR is ... been using it for a decade or so, before it became hip again in form of JS frameworks.

That does not change, that in practice people will write their web components all in one file and render a template, which contains <MyFancyNamedTag>{js logic here that should not be written here}</MyFancyNamedTag>. The mere ability to have arbitrary js logic in there and the fact, that rendering MyFancyNamedTag actually renders a component, which itself can contain arbitrary js logic, instead of outputting a real tag named MyFancyNamedTag, leads to people coupling everything tightly together. Traditional server side template rendering encourages people to make up their data beforehand and then render the template.

Maybe you don't do it that way. Maybe you are smarter than the average frontend/js-is all-I-know-because-I-heard-its-all-I-will-ever-need developer. Not saying you specifically make these mistakes it.

Edit: With regards to being able to contain arbitrary js code in braces in the call to render a component, this actually inches close to the mess many people make when using PHP. There is was: open php "tag", php logic, output HTML, treating it as a string, inside HTML open PHP again, output some JS, JS logic contains code for manipulating HTML ...

Re:Reimagining front-end web development with htmx and hyper... by eurasiantiger on Sunday July 03, 2022 @10:48AM (#31968100)
Well, the way I see it (and the canonical way React sees it), a render function f producing a view V from some data D is defined as

    V = f(D)
… which is an oversimplification, since UI commonly has local state, which definitely doesn’t need to be elevated all the way to the master data. Any interactive UI thus certainly uses several sources of truth.

Let’s add some to the previous. Let our previous data D now be D_master since it is our master business data (which we may not change in any way, shape, or form). Then, add state of app routing D_router, user auth state D_auth, and local widget state D_widget.

    V = f(D_master, D_router, D_auth, D_widget)
We might have all kinds of access checks regarding the three first ones, but here we are only interested in what this means for rendering a view.

To use all our data, let’s imagine a form where a dropdown select widget is placed inside a tabbed container. The user needs to have a particular user role in their auth data to see this widget. There are many similar widgets in this form, all with fine-grained controls.

- The options for the list come from D_master.

- The state of the tabbed container lives in the URL so that it isn’t lost on refresh, so it is accessed through D_router.

- The user role comes from D_auth.

- And finally, whether the dropdown is open or not is stored in the component’s local state, D_widget.

This is not a far-fetched example, such things are common in more complicated applications.

In any case, it does make intuitive sense to me to allow logic in the render function, as long as that logic operates directly on the render function parameters and not on some on-the-fly transformed intermediate.

Rules of refactoring also apply.

Without logic in the render function, the coupling between frontend and backend would get insane, as every component fetching data would require custom on-the-fly denormalisation for that data in the backend. In our example, we would be pre-generating data for the entire form for a particular user, probably even for unused features.

It would be like generating snippets of HTML in response to AJAX requests, but without the prerendering to HTML.

Re:Reimagining front-end web development with htmx and hyper... by josephg on Sunday July 03, 2022 @08:14PM (#31972164)
> Not clear to me that the added complexity is worth it personally.

It used to be pretty easy to set this up. Modern bundlers, jsx, es modules, typescript, webassembly and modern web frameworks which need their own compilers have all made this much more complex. I’m not sure the complexity is worth it any more.

Re:Reimagining front-end web development with htmx and hyper... by ttty on Saturday July 02, 2022 @05:19AM (#31957473)
So you rather write your app twice: once in react and once in go? And keep it in sync, including css and styles and dom… ? For real?
Re:Reimagining front-end web development with htmx and hyper... by chrismorgan on Saturday July 02, 2022 @02:54AM (#31956837)
You should almost always use a bundler. ES modules work, yes, but using them naively subjects you to a waterfall (each module file may trigger the loading of others, though you could circumvent this with a sufficiently advanced server that recursively traversed modules in order to collect the whole set and send rel=preload links for each with the initial response), slow loading everywhere and exceedingly slow loading in higher latency, and the downloading and execution of much unnecessary code. For many completely realistic designs that would have been a hundred kilobytes in a one or two files, you’ll end up with it spread over several hundred files, reaching ten or twenty levels deep, so that it downloads and executes multiple megabytes of code, a lot of which is not even used, so that for someone on the other side of the world, what would with proper bundling have taken less than two seconds to load (including initial TLS handshake) will instead take ten or twenty seconds if everything goes smoothly. And I’m not exaggerating in the slightest here.

The situation would certainly be worse in HTTP/1 because of the lack of pipelining and the six connections limit, but HTTP/2 does not make not-bundling in any way reasonable.

Re:Reimagining front-end web development with htmx and hyper... by mattlondon on Saturday July 02, 2022 @03:09AM (#31956901)
The import statement returns a promise, so you can lazily load modules as required (i.e. don't load everything up front, only load on-demand as users are using your app).

You don't need to complicate things - just use vanilla JavaScript features as they were intended.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guid...

Re:Reimagining front-end web development with htmx and hyper... by chrismorgan on Saturday July 02, 2022 @03:25AM (#31956965)
In practice, you are likely to have to contort your code very significantly to use dynamic imports pervasively. And pervasive usage such as you seem to be describing is emphatically not how dynamic imports were intended to be used, and wouldn’t speed things up much in general anyway (because you still can’t do anything until your dependencies have loaded), and stands a good chance of being slower.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... expresses it well:

> Use dynamic import only when necessary. The static form is preferable for loading initial dependencies, and can benefit more readily from static analysis tools and tree shaking.

(Also, a quibble over your wording: dynamic imports aren’t a statement, but a call, though not a function call.)

Re:Reimagining front-end web development with htmx and hyper... by mattlondon on Saturday July 02, 2022 @03:43AM (#31957043)
You need to develop with this in mind from the start. I am not suggesting refactoring an existing application to use it for every single dependency as yes that would be a pain.

Instead import your initial generic dependencies up-front as usual, but then notionally break your application up by CUJ and load the CUJ-specific code dynamically as needed.

E.g. for a hypothetical email client you would import the initial "inbox view" code upfront (along with generic common dependencies), but you might not import the code needed for a rich text editor until the user clicks on the "write email" button, saving that network load and code execution for when - or indeed if - the user actually needs it.

Re:Reimagining front-end web development with htmx and hyper... by chrismorgan on Saturday July 02, 2022 @03:58AM (#31957103)
Given the context (waterfall stuff), I was assuming you were describing pervasive dynamic imports, since the selective use of dynamic imports for code splitting is fairly standard practice. (And indeed, I know from working on it a few years back and doing most of the porting from a custom module system to ECMAScript modules, Fastmail’s webmail works in this way, though my recollection is that compose specifically is preloaded once the rest of the mail stuff is done, so that when you hit that compose button it’s almost certainly ready to go common fault of code splitting is deferring loading too long, so that you end up slowing users down.)

When I spoke of unused code, I was meaning things like a file containing a bunch of functions, of which you only use one, and so there are the other functions and perhaps deeper imports that weren’t actually necessary; a bundler would remove all the unused stuff. The “solution” in such a case would be to do something like splitting each function into its own file, but that’s worse than before because now your import chains are probably even longer, and even more files have to be loaded (and there is still some filewise overhead, even if HTTP/2 improves it drastically).

I’ll temper my expressed position slightly by saying that bundling is not so significantly advantageous if you have written all of the code (you’re not importing third-party libraries), and keep your file count fairly low and import depth very low. But at that point, perhaps you should have just dropped it all in one file.

Re:Reimagining front-end web development with htmx and hyper... by eurasiantiger on Saturday July 02, 2022 @03:05AM (#31956882)
Don’t use external dependencies (:D), don’t write code you don’t use, encapsulate imports in custom elements (this is a joke).

Without any runtime boilerplate like module bundling, corejs polyfills and the usual frontend/css-in-js frameworks, all code that gets downloaded is your code, so make it count. 640 kilobytes should be enough for everyone.

Re:Reimagining front-end web development with htmx and hyper... by chrismorgan on Saturday July 02, 2022 @03:38AM (#31957014)
For runtime boilerplate: yeah, don’t use webpack, it does indeed add silly boilerplate (and structures things in a way that almost requires source map tooling to understand!); use Rollup instead, which pretty much writes just what you would have written if you had put everything in one file, with no overhead whatsoever. (If you happen not to be familiar with it, take a look at the examples in https://www.rollupjs.org/repl/.) Also ditch core-js as it’s almost certainly unnecessary if ES modules are your baseline.

But even if you’re not using external dependencies, I maintain: by not bundling, you’re slowing things down, and it’s generally the people on the other side of the world from your servers and the disadvantaged with less-powerful computers and more expensive data that will suffer for your whim.

Re:Reimagining front-end web development with htmx and hyper... by eurasiantiger on Saturday July 02, 2022 @06:28AM (#31957778)
From what I’ve seen, many built-and-bundled apps have run into security issues in dependencies and have been forced to switch to ESM versions which are highly incompatible with CJS-targeting builds and so require significant retooling just to fix trivial security issues.

It’s not npm’s fault for sure, but the ecosystem is fundamentally divergent on this issue and there doesn’t seem to be a universal solution.

Re:Reimagining front-end web development with htmx and hyper... by chrismorgan on Saturday July 02, 2022 @08:05AM (#31958339)
I’m confused by your comment, because it doesn’t seem relevant and I don’t understand what you’re saying. My first impression is that you seem to be implying that the security issues in dependencies are in some way linked to the module format (ES/CommonJS), which is not the case. I can’t see why such issues would force a switch in either direction for anyone, given especially that ESM bundlers can generally slurp ECMAScript Modules and CommonJS alike (e.g. Rollup via @rollup/plugin-commonjs) with only rare compatibility issues after at most a little configuration, generally associated with very-badly-written code. Given how much of the npm ecosystem is still on CommonJS modules, there’d be much more outcry if you could’t intermingle the two in a bundler. So if what you had in mind was something like “version x was CommonJS and has a bug, version y is ESM and has fixed the bug”, well, you can probably update to y easily (at least as far as the CommonJS/ESM divide is concerned), and if you were forking or anything, if the bug is clearly identified then patching it onto x is easy too.
Re:Reimagining front-end web development with htmx and hyper... by eurasiantiger on Saturday July 02, 2022 @08:10AM (#31958365)
In practice, there are packages which have moved on to ESM and which no longer maintain their CJS version (at least as actively), making life difficult for maintainers of projects dependent on those CJS packages with published vulnerabilities.

A CJS-targeting build can’t slurp up ESM dependencies. Not without dubious hacks, at least.

See e.g. https://stackoverflow.com/questions/70545129/compile-a-packa...