Live data from Hacker News

Web Components could replace frontend frameworks?

dannymoerkerke.com

221–230 of 248 posts

Re: Web Components could replace frontend frameworks?

#221
post #117

Earlier quoted context omitted.

Never use .innerHTML! Instead use .appendChild, .removeChild, and .innerText. Using .innerHTML is like having a program patch itself during runtime.

While I know what you're getting at, the word "never" really isn't helpful or useful. There are certain times where innerHTML(or insertAdjacentHTML) is the best approach. It's really not that slow that it can't be the most clear and concise solution.

Its not about performance. innerHTML is actually fast. Its about security and maintainability. InnerHTML makes it easy not only for the developer but also an hacker to inject code into a web page/app. innerHTML is like a global missile satellite network that can fire anywhere at any time. Instead you want to design like a plumber, where data flows via a pipe to the database, then back again in another pipe which change the state of the view. You should not have a single HTML tag in your single page application!

Re: Web Components could replace frontend frameworks?

#222
post #212

Earlier quoted context omitted.

Server-side rendering and a rich frontend are orthogonal. And yes, JavaScript is absolutely not the best way to actually make use of the server rack. https://www.techempower.com/benchmarks/

They are, unless you want to avoid duplicating logic, like I said. That's where most of the benefit of server-side rendering via JS comes from: you can render the initial page on the server, but then have the client-side framework take over on load, all from the same framework and with very minimal code duplication. Otherwise you have to write most of your rendering logic twice, in two different languages/frameworks.…

I rather go with rendering performance of HTML, CSS and minimal JS.

Re: Web Components could replace frontend frameworks?

#223

Earlier quoted context omitted.

Types are cool, TypeScript is cool, and types with JSDocs are cool, https://dev.to/dakmor/type-safe-web-components-with-jsdoc-4i... and what's more, they're all cool with web components.

I'm confused, I scrolled down and still saw this: return html` ${this.format(this.title)} `; That is not type checked. I can change any of the HTML tags in there, any of the attributes on the HTML tags, I can make the template ill-formed HTML, and nothing in my editor or build system will check that. const el = /** @type {TitleBar} */ (document.querySelector('title-bar')); Why would I do that instead of this, if I'm…

There exists plugin for VSCode which checks for attributes inside template literal https://marketplace.visualstudio.com/items?itemName=runem.li...

Re: Web Components could replace frontend frameworks?

#224

Earlier quoted context omitted.

There is lit-html and lit-element, though you can get tripped up by the difference between props and attributes coming from React JSX where everything is props because it’s all native JS until the DOM render... It’s tricky to recommend them because I’m using web components at work and find the transition from React especially rocky. But they are native and standard, and other frameworks are slowly moving in that dire…

You can absolutely use web components with react... you just have to use ` ` for webcomponents, since a capital first character will trigger JSX transforms. So you can use, for example this set of components with a react application and get some of the best of both worlds. The only down side is attributes in web components aren't passable via the markup, only via DOM reference. I happen to like react as-is, but Vue i…

Vue already offers compiling to web components.

https://cli.vuejs.org/guide/build-targets.html#vue-vs-js-ts-...

Re: Web Components could replace frontend frameworks?

#225
post #66

I argued for a similar case last year[1], however the missing piece right now is updating state in a clean way. Web Components definitely won't replace current front-end frameworks, but combined with ES Modules the web is getting to a good place for resuable primitives. :) [1] https://andrewrabon.com/the-case-for-react-like-web-componen...

Be sure to checkout lit-html and LitElement ( https://lit-html.polymer-project.org & https://lit-element.polymer-project.org )...what you've written about here already exists!

Also give StencilJS a look. It's a compiler instead of a framework or library. The end result is pure vanilla components https://stenciljs.com/

Re: Web Components could replace frontend frameworks?

#226

Web components are awesome I've been using them in production using StencilJS for a pretty large app for about a year now. Even though browser support is widely available there are still a few gotchas with web components, specially Shadow DOM. Not all community tools have caught up as well. For example you can't use Stripe v3 with Shadow DOM. Most analytics tools also cannot access the Shadow DOM. We had to remove Sh…

You might be interested in https://github.com/bennypowers/stripe-elements Stripe v3 with Shadow DOM.

Thanks I had actually seen this while looking into a workaround for us. However I thought that LitHTML elements only worked with other LitHTML elements since you need the correct bundler for it. StencilJS projects don't have any bundling system.

Re: Web Components could replace frontend frameworks?

#227

Earlier quoted context omitted.

It’s still opaque string blobs no matter how you try to dress them up. —- I should probably edit this: It’s still string blobs that are totally opaque to the browser, and that are parsed with regular expressions[1] at runtime[2] and dumped into DOM via innerHtml[3]. [1] https://github.com/Polymer/lit-html/blob/61c08a615abadbe58bc... [2] http://exploringjs.com/es6/ch_template-literals.html [3] https://github.com/Polym…

Yeah, lit-html scans template strings to determine what kind of markers to join them with - comments for text expressions, {{}} for attributes. This is before handing to the browsers HTML parser. So what? You always pop up to complain about this, like it should be some obvious fatal flaw, but you never explain why it bothers you personally so much. Does it make lit-html slower? No. lit-html scanning + native HTML par…

> So what? You always pop up to complain about this, like it should be some obvious fatal flaw, but you never explain why it bothers you personally so much.

I pop up to complain about this because I refuse to give in to mass delusion about what tagged template literals are. Others have said it much better than I have [1]:

"...we’ve learned not to write code in strings. You’d think this would be a fundamental law of software engineering... Writing code, not strings, means you can do everything to it that you can do to code. You can type check code. You can lint it. You can optimise it, compile it, validate it, syntax highlight it, format it with tools like Prettier, tree shake it"

Edit. Found another great quote [1.1]:

"The benefits of the component as plain, non-stringified data are not the avoidance of closing tags, but what you can do with those components computationally. E.g. instrumentation, behavior/attribute injections, configuration, filtering etc. An approach based on string interpolation can only go this far, whereas having everyting as arrays, objects, iterators your possibilities of composition & transformation are almost endless..."

> Does it make lit-html slower?

The problem isn't performance, is it.

> Are lit-html templates "opaque"? That's arguable.

It's not arguable. It's a simple fact of life.

Not a single piece of the entire tech stack involved in working with them knows what they contain: not the editor/IDE, not the bundler, not the JavaScript VM, not the browser. In case of lit-html they only stop being opaque when they get dumped into the DOM via innerHtml, and when the browser attempts to parse that string as HTML.

> I'd say it's no more opaque than compiled JSX: the browser has no way of knowing that those nested function calls represent HTML, so they're "opaque".

The browser has no idea that those blobs of string are HTML until you dump them into the DOM.

However, the browser knows a great deal more about the Javascript code that gets executed in it's VM.

> I'm going to continue to shrug every time you complain about this until you explain why it's even bad.

Above are some quotes from other users that express these concerns.

I can also quote myself from elsewhere [2]:

In a language that’s already a laughing stock for its insane dynamic type system, we said: “It’s all right, we’ll have all our code in strings now, thank you very much, and we’ll make sure we parse it at runtime because what can possibly go wrong”.

The only reason projects like lit-html, HTM and some others can boast about their “accomplishments” building fast, effecient libraries is because string handling has been insanely optimised by the modern Javascript VMs. And that .innerHTML is no longer the slowest operation on the DOM.

...

Why is this bad again?

Because this isn’t code. This is literally taking a bunch of opaque string blobs, parsing them at runtime, and producing some result. All of programming has been busy moving away from coding in strings and parsing stuff at runtime. For the past few years Javascript has been happily re-introducing the worst programming practices. And devs get away with it, too, just because modern Javascript VMs and browser DOM are optimised way more than they have any right to.

...

Since these are just arbitrary strings, no common tools will be able to lint them, analyse them, optimise them unless you write a very specific tool for this particular very specific string structure. And yes, that includes JS VMs.

...

You wanted macros? Here, have run-time string concatenation and regexp parsing, and stringly-typed everything.

[1] https://news.ycombinator.com/item?id=18511943

[1.1] https://news.ycombinator.com/item?id=18052072

[2] https://dmitriid.com/blog/2019/03/tagged-template-literals/

Re: Web Components could replace frontend frameworks?

#228
I think this is an excellent thorough presentation of WebComponents. One think I didn't quite understand was the first example about which it is said:

"I have created a web component which lazily loads an image once it is fully visible in the browser's viewport"

Once the image is "fully visible", what's the point of "loading it"? Doesn't it go the other way first you load it then it becomes visible?

Re: Web Components could replace frontend frameworks?

#229

I wonder why HTML never got includes. It would be so nice to just write: On Hacker News, for the first pageview, this would load the whole "Guidelines | FAQ | Support ..." section. On all other pageloads, the browser would already have it in the cache. So it does not have to be loaded again. Building complex websites would become so much nicer. Because each part of a page would be an include.

Frameset / iframe?

You are right. IFrame creates an almost but not totally isolated page-environment. So in a sense IFrames are 'web-components".

So what can Web-components offer over that? A simpler way of including them on a page with a single tag. Well, you must load the js-file too but a single js-file could contain multiple web-components I assume.

The whole (?) point of components is isolating the interface from implementation, encapsulation, which WebComponents does nicely by providing a standard way of writing methods of the underlying ES6 class.

We could say that WebComponents are smarter IFrames with a cleaner way to declare their interface.

Another great thing about WebComponents is extending existing tags which makes it easy to prototype improvements to existing tags. For instance I wonder if IFrame could be made better by creating a web-component that extends them?

Re: Web Components could replace frontend frameworks?

#230

I don't get the hate for web components. I think they are amazing. A number of times I just wanted a damn date picker, not a framework that changes how I'm making my website! Web components provide exactly that.

I agree. But there's always "hatred" for anything new which requires people yet again to learn new ways. I understand that. But there's nothing more difficult to stop than an idea whose time has come. I think that was from a Funkadelic LP-cover :-)
Post reply on HN