News for hackers. Stuff that matters.
Re:Astro 1.0 – a web framework for building fast, content-fo... by cercatrova on Tuesday August 09, 2022 @06:42PM (#32405512)
Developer experience writing React is much nicer than using other markup languages. That's why I use it at least, the fact that it spits out a fully JS-free website at the end is the icing on the top.
Re:Astro 1.0 – a web framework for building fast, content-fo... by tshaddox on Tuesday August 09, 2022 @07:46PM (#32406160)
Nothing about the developer experience of React seems better than any old HTML template language (and hardly better even than plain HTML), if you're not doing any interactivity and are just going to render once and spit out the HTML.
Re:Astro 1.0 – a web framework for building fast, content-fo... by cercatrova on Tuesday August 09, 2022 @07:49PM (#32406190)
Have you used React before? It's miles above HTML templating. I honestly don't use frameworks that have regular templating anymore, like Vue or Svelte.
Re:Astro 1.0 – a web framework for building fast, content-fo... by tshaddox on Tuesday August 09, 2022 @08:54PM (#32406658)
I have used React. I'm a huge fan and I also prefer it over its most common "competitors" like Vue and Svelte.

But React (like Vue and Svelte) are fundamentally about interactivity. If I had a project where I knew for sure that I only wanted to generate HTML sans JS on the server (or with a static build process) I wouldn't even consider using React.

It barely even makes sense. Your "React components" would just be JavaScript functions that take props and return some JSX. None of the interesting React features and hooks would even make sense, other than maybe context (and presumably most or all popular static HTML templating tools have comparable features).

Re:Astro 1.0 – a web framework for building fast, content-fo... by munbun on Wednesday August 10, 2022 @09:48AM (#32411862)
JSX is much easier to maintain than pretty much every other templating language.

If you are building a growing design system with any degree of complexity, React is one of the best tools available.

Re:Astro 1.0 – a web framework for building fast, content-fo... by dsego on Wednesday August 10, 2022 @06:37AM (#32409920)
Which looks nicer? Eta vs JSX

   <%~ includeFile('./navbar', { pages: [
     'home',
     'about',
     'users'
   ] }) %>

   <navbar pages={['home', 'about', 'users']} />


> would just be JavaScript functions that take props and return some JSX

They can also be async functions that process data independently. Whereas templates are commonly just passed in data from the controller.

https://github.com/dsego/ssr-playground/#jsx-partials

Re:Astro 1.0 – a web framework for building fast, content-fo... by acdha on Tuesday August 09, 2022 @08:20PM (#32406418)
Yes. JSX is nice for escaping by default but encourages total spaghetti coding hiding app logic in the templates and various footguns like non-standard attributes className and breaking id. Running it on the server side mitigates the React performance hit somewhat but the selling point is interactivity.
Re:Astro 1.0 – a web framework for building fast, content-fo... by alanwreath on Tuesday August 09, 2022 @08:30PM (#32406502)
What are you referring to? I am unaware of what “regular” templating you’re referring to or what react is achieving that is not available or cumbersome under react or svelte.

I mean if JSX is considered as something particularly powerful (not saying it does) doesn’t Vue actually do that https://vuejs.org/guide/extras/render-function.html ?

Re:Astro 1.0 – a web framework for building fast, content-fo... by zdragnar on Tuesday August 09, 2022 @09:52PM (#32407053)
Think of JSX as a macro, rather than "regular" templating, which is typically string substitution.

For static sites, this means that you get functions and objects the entire way through the render pipeline right up until there is a full tree built and the final output is rendered.

You still get all the separation powers of contexts, the component based reusability, etc, and it is all regular JavaScript / typescript except for the JSX macro itself and React's APIs (which are just JavaScript). Conversely, with templating engines like handlebars / erb / et al you need to learn the specific DSL of the template engine- custom loops and controls, imports for partials, and your custom helpers are limited to what they can do. Even Vue's render function has special markup for control (v-if, v-else).

Re:Astro 1.0 – a web framework for building fast, content-fo... by ricardobeat on Wednesday August 10, 2022 @12:53AM (#32407968)
Some day we have to stop repeating this nonsense. JSX requires much more learning than simple templating, there is no inherent benefit. Even a simple conditional is more complex than your average template.
Re:Astro 1.0 – a web framework for building fast, content-fo... by zdragnar on Wednesday August 10, 2022 @01:09PM (#32415341)
How so? Any expression is valid within JSX- ternaries, function calls, binary and unary operators are all valid. If you know JavaScript, you already know all of the control mechanisms that are valid.
Re:Astro 1.0 – a web framework for building fast, content-fo... by cercatrova on Wednesday August 10, 2022 @01:32AM (#32408099)
The thing is, if you know JS, then a conditional is JSX is the same as in JS. Contrast that with every new templating language under the sun which might have its own way of doing conditionals and loops and control flow. That to me is much more annoying.
Re:Astro 1.0 – a web framework for building fast, content-fo... by chrismorgan on Friday August 12, 2022 @01:33AM (#32435007)
This is a dubious claim, since JSX is limited to expressions. If you ask people for “a conditional in JS”, they’ll very probably go for `if (…) { … } else { … }` first, not `… ? … : …` (if you can even rewrite it as a ternary). Same deal with loops: you’re limited to expressions, so you can’t use the normal way of writing a loop (and this regularly leads to mild contortions as you deal with iterables of diverse types). Therefore I’d tend to (qualifiedly) describe JSX as doing its own thing too.
Re:Astro 1.0 – a web framework for building fast, content-fo... by zdragnar on Saturday August 13, 2022 @02:05AM (#32447340)
Plenty of people are taught that ternary operators are evil and should never be used.

Plenty of people were also taught that proper architecture involved an AbstractGetterVisitorFactoryFactory.

That doesn't make either of those things true.

As a side note, if you find yourself wanting a loop but using `map` isn't sufficient, you should probably be preparing the values ahead of time and still using map. It'll be more efficient, and the code easier to read.

Re:Astro 1.0 – a web framework for building fast, content-fo... by 9dev on Wednesday August 10, 2022 @05:52AM (#32409660)
But you’re aware JSX is just plain old PHP files, code intermingled with HTML? It’s the exact same thing, and it breeds the exact same bad behaviour.
Re:Astro 1.0 – a web framework for building fast, content-fo... by zdragnar on Wednesday August 10, 2022 @01:18PM (#32415463)
Separation of concerns is different from separation of languages.

PHP intermingled with HTML was bad because developers would do things like run database queries and other operations that caused side effects directly in the markup. Every templating system still has logic in it, and almost every templating system as a way to create custom helpers which are written in the host language.

Since API calls and calls to react's `setState` are asynchronous, and the rendering pipeline is synchronous, it is still obvious that you can't put code that makes an AJAX call in your html markup.

Re:Astro 1.0 – a web framework for building fast, content-fo... by 9dev on Thursday August 11, 2022 @08:26AM (#32424643)
JSX doesn't separate languages, you're mixing HTML with TypeScript. You can absolutely do database queries in your template. What is keeping you from doing `fetch('https://side-effect.ts').then(() => doSomethingHorrible())` in your template? Or invoking an `alert()`?
Re:Astro 1.0 – a web framework for building fast, content-fo... by zdragnar on Thursday August 11, 2022 @10:37AM (#32426360)
You can do those things, but you're not going to capture the effect that you want. Renders happen frequently, and the render process will complete before your `fetch` resolves. As for alert, the alert will pop up immediately before the render is applied to the DOM- and will likely pop up many times more than you thought it would, assuming you're popping it up based on some state variable.

In short, you can do those things maliciously, but if you do them naively it is immediately obvious when you run the code that it's not correct.

Compare that to the original analogy to mixing raw PHP and HTML templates. Since the rendering process is blocking, you can easily stuff form handling, remote API calls, and database calls in amongst your markup, and can make it "work" even if it's not clean.

Even Laravel's blade components let you write custom components and helpers in PHP which can do all of those things from the template- you just don't see the actual SQL mixed with the HTML in the same file. The problem is still there, just now it is harder to see.

Re:Astro 1.0 – a web framework for building fast, content-fo... by nsonha on Wednesday August 10, 2022 @09:32AM (#32411614)
> Nothing about the developer experience of React seems better than any old HTML template language

Is that a joke? You get autocomplete from typescript and props to be fully typed functions, or any kind of object for that matter. That compares to autocomplete that's just some ad-hoc, bug ridden tools that locks you into some IDE or editor, and who cares because all the attributes have the same type (string) anyway.

Re:Astro 1.0 – a web framework for building fast, content-fo... by bryanrasmussen on Wednesday August 10, 2022 @05:50AM (#32409652)
and what's your experience writing other markup languages that you base this statement on? I can definitely agree it is nicer for a data intensive sites, and with effort can be made somewhat equivalent in most document heavy sites, but there are definitely some scenarios I've encountered where the match of technology to use case was disastrously not in React's favor.

Specifically I can think of parts of the websites' functionality is editing highly technical structured data documents by highly trained domain experts.

I mean basically where years of developer productivity was thrown down a React hole when it could have been months in SGML / XML based tooling for what was required.

I can say that because I have built big solutions in both stacks, though.

Re:Astro 1.0 – a web framework for building fast, content-fo... by cercatrova on Wednesday August 10, 2022 @10:49AM (#32412933)
I've used many, Handlebars, Pug, Vue's, Svelte's, Zola's, many. They're all somewhat similar in that they try to recreate loops and conditionals, all without strong type support unlike in JSX. I've never used SGML though, another commenter told me that it is more powerful.
Re:Astro 1.0 – a web framework for building fast, content-fo... by cutler on Tuesday August 09, 2022 @08:54PM (#32406664)
You have to be joking, surely? React/Vue as a mere static template languages? Dedicated static site frameworks such as Jekyll and Middleman are a much better experience. My favourite is still Perl's Template::Toolkit.
Re:Astro 1.0 – a web framework for building fast, content-fo... by cercatrova on Tuesday August 09, 2022 @09:16PM (#32406825)
TypeScript, that's the differentiator. Having fully typed variables in the templates cannot be replicated in templating languages (well technically they might be able to but that's not as ergonomic as fact).
Re:Astro 1.0 – a web framework for building fast, content-fo... by cutler on Tuesday August 09, 2022 @09:18PM (#32406844)
Talk about sledgehammer to crack a nut - honestly, why does anyone need type safety in a simple templating operation? Dynamic languages have their uses, you know. Type safety is a tool, not an ideology.
Re:Astro 1.0 – a web framework for building fast, content-fo... by Yiin on Wednesday August 10, 2022 @12:09AM (#32407764)
because it's nice to know whether that `post` variable contains a property `createdAt` or `published` At without leaving the template. Arguing "I don't use it why should you" is unproductive.
Re:Astro 1.0 – a web framework for building fast, content-fo... by cercatrova on Wednesday August 10, 2022 @01:30AM (#32408089)
Agreed, you get all the benefits of type inference even in a template. It's a pain to debug template variables that don't exist, I've written enough Handlebars to know.
Re:Astro 1.0 – a web framework for building fast, content-fo... by cercatrova on Tuesday August 09, 2022 @10:24PM (#32407235)
I basically never use dynamic languages anymore. If there aren't algebraic data types in a language I simply don't use it. Sometimes people think I'm exaggerating but it's truly great to use a language with ADTs that you get spoiled.
Re:Astro 1.0 – a web framework for building fast, content-fo... by cutler on Wednesday August 10, 2022 @12:41AM (#32407914)
So what do you use for shell scripting?
Re:Astro 1.0 – a web framework for building fast, content-fo... by cercatrova on Wednesday August 10, 2022 @01:29AM (#32408083)
I like using Nim or Rust, or TypeScript, depends on the script. If it's just a few lines, sure I'll write it in bash, but if it gets a little larger, I convert it into a real programming language.
Re:Astro 1.0 – a web framework for building fast, content-fo... by dotancohen on Wednesday August 10, 2022 @02:09AM (#32408299)
Rust as a replacement for a bash script?

My bash scripts are often edited, I could not imagine using a compiled language where I would have to store the source as a separate file from the executable, then compile it each time.

I'd love to know your more specific use cases, if you don't mind. I'm always happy to learn something new. Could you share some Rust that most people would script in Bash as an example? What's your build and deploy (to ~/.local/bin I presume) strategy?

Re:Astro 1.0 – a web framework for building fast, content-fo... by cercatrova on Wednesday August 10, 2022 @10:46AM (#32412891)
> where I would have to store the source as a separate file from the executable, then compile it each time

> What's your build and deploy (to ~/.local/bin I presume) strategy?

You can run scripts as you would with bash, you don't have to manually build and run the executable. For example, `cargo run (inside the script source folder)` and `sh script.sh` do basically the same thing, end user wise. `nim compile --run script.nim` is similar in that the language compiler will automatically compile and run it together.

> Could you share some Rust that most people would script in Bash as an example?

I was creating dotfiles the other day and I didn't want to use some dotfile manager program as I had some specific steps I wanted to follow, so I started it as a bash script. Well, it got kind of annoying so I made it into a Rust script with some nice features like interactive prompts, text coloring, etc with libraries like `clap`. You can do this in bash of course, but the Rust version was more ergonomic. When I need to run the script, I just did `cargo run` and it worked great.

Re:Astro 1.0 – a web framework for building fast, content-fo... by dotancohen on Wednesday August 10, 2022 @11:32AM (#32413680)
I see, but then your scripts are not nice compact commands.

For instance, my most-used bash script takes a file as input and opens either VIM or Emacs depending on whether the file is a .md or .org (simplified example). I run it like so:

  $ n foo.org
I can edit ~/.local/bin/n and update the file, and use it immediately. I actually have my whole ~/.local/bin in version control. Having to type out e.g.

  $ nim compile --run n foo.org
...would make the whole experience far less fluent. I probably would never use it.

I'm asking because I'd love to rewrite this script in e.g. Rust, but I don't see any good way to deploy it to ~/.local/bin/n.

Re:Astro 1.0 – a web framework for building fast, content-fo... by firesloth on Thursday August 11, 2022 @01:02PM (#32428695)
For nim, you could use something like nimcr (https://nimble.directory/pkg/nimcr). You put a shebang in your script `#!/usr/bin/env nimcr` and then call it like a normal script.

eg:

  $ code script.nim


  #!/usr/bin/env nimcr
  echo "hello world"


  $ ./script.nim
  hello world
edit:

There's also the possibility of using nimscript, using nim e. It works similarly but you'd change the shebang line to something like

  #!/usr/bin/env nim e --hints:off
Re:Astro 1.0 – a web framework for building fast, content-fo... by firesloth on Saturday August 13, 2022 @04:34PM (#32453540)
Actually another way to do nimscript is to make a config.nims file with tasks in it. eg:

In config.nims:

  switch("hints", "off")
  task hello, "say hello world":
    echo "hello world"
In bash:

  $ nim hello
  hello world
Re:Astro 1.0 – a web framework for building fast, content-fo... by cercatrova on Wednesday August 10, 2022 @12:02PM (#32414232)
Just alias the command in your .bashrc? e.g. `alias n="nim compile --run n"`. I'm not sure what the issue is with longer commands because the benefits of writing in a strongly statically typed language definitely outweigh a few extra characters to type, which you don't even need to do with aliases.
Re:Astro 1.0 – a web framework for building fast, content-fo... by cb321 on Thursday August 11, 2022 @12:21PM (#32428030)
For what it's worth, you need only type "nim r n". If "n.nim" is marked executable and begins with "#!/usr/bin/nim r" then you also need only type "n.nim".
Re:Astro 1.0 – a web framework for building fast, content-fo... by paldepind2 on Wednesday August 10, 2022 @02:10AM (#32408307)
But TypeScript doesn’t have ADTs which you stated was a hard requirement. It has some other features that can achieve similar things though, so maybe that’s good enough?
Re:Astro 1.0 – a web framework for building fast, content-fo... by piaste on Wednesday August 10, 2022 @09:01AM (#32411188)
Kinda nitpicky. Sure ADTs are a pattern in Typescript rather than a first-class entity, but they're an extremely well-supported pattern, exhaustiveness checking and all.

    type ADT = 
        | { case : 'a', a : Number }
        | { case : 'b', b : string }

    function f (c : ADT) {
        switch(c.case) {
            case 'a' : return c.a
            case 'b' : return Number.parseInt(c.b)

            // case 'c' : return 0 // compile error

            // case 'b' : return c.a // also compile error
        }
    }
Re:Astro 1.0 – a web framework for building fast, content-fo... by mind-blight on Wednesday August 10, 2022 @04:21AM (#32409170)
Discriminated unions aren't quite as nice, but they're pretty close.
Re:Astro 1.0 – a web framework for building fast, content-fo... by wpietri on Tuesday August 09, 2022 @11:20PM (#32407537)
For sure. I'm a big fan of type safety in larger codebases with reasonably stable domains. But when a project's small or new enough to be relatively volatile, the overhead just isn't worth it for me.
Re:Astro 1.0 – a web framework for building fast, content-fo... by tannhaeuser on Wednesday August 10, 2022 @01:29AM (#32408087)
There are "templating languages" such as SGML for generating type-checked markup ie. respecting the regular content model and lexical types expressed in DTDs and other markup declarations. This results in injection-free, HTML-aware templating ie. templating engines can properly quote/escape content in attributes, CDATA sections, etc. and can enforce content model rules (that eg lists consist of nothing but list items, that script elements aren't placed where forbidden as would be required in user content such as comments provided by your web site visitors, etc.) Much more powerful than programming language types, and needed in CMSs.