Why Preact instead of Alpine? #53022
Replies: 0 comments 8 replies
-
Hey @domtra, thanks for stopping by 🙂 The Directives Hydration (and WP Directives plugin) approach explored in this repository is what you mention: a system of directives. It's similar to Alpine, but with a few, and very important distinctions:
The fact that it is powered by Preact is more of an implementation detail, because most people won't even know about it. It gives us a lot, though:
All in all, we're relying on Preact to do the heavy lifting for us, delegating most of the complexity to it. It's the only framework small enough, performant enough, extensible enough, and that has all the features we need to build something like this on top of it. If you look at our code, it is small and simple, around 2Kbs. The rest (another 8Kbs) is Preact. Thanks to Preact, we can focus and spend more time building something that adds real value to WordPress devs and less time implementing low-level code and fixing bugs. |
Beta Was this translation helpful? Give feedback.
-
Hey @luisherranz, thanks a lot for your in depth replies. I understand that you are at an early stage with this and things haven't been polished for communication, yet. That is why I appreciate even more you taking time to answer my questions. This is really a lot to grasp and the more I dig in the more questions I have. I totally understand the usage of preact, esp. for the exploration. As you said, it is more of an implementation detail. A nice thing about preact is of course, that it has potentially full compat with react. Nonetheless, I could imagine that you run into issues with the whole vDOM topic. You said, that
but from my experience, there is still overhead and additional complexity involved, compared to non-vDOM approaches. Mostly because without a vDOM, you do not have to serialize and deserialize / hydrate an entire component, but only the parts that are interactive. Qwik is also going in such a fine grained direction with a vDOM, but its approach is probably even harder to achieve with a PHP backend. I am esp a fan of solidjs as an alternative to a vDOM based approach. Speaking of solidjs, I totally agree that signals are an awesome concept. I also saw, that you are using your own class for deepsignals, as preact propably will not implement something like that soon. I think the work that solidjs is doing in this area, creating a bunch of primitives that can be used to build bigger systems is really great. I do not know if you have seen this, but maybe that would also be an alternative approach. The whole SSR approach I probably understand least, so far. This seems to be limited to the given directives, right? It is not some PHP rendering of all block attributes. For me, one of the biggest issues with non-dynamic blocks is, that if you change the of a block, it will be come invalid, and you have to manually update the parent post that uses this block, in order for these changes to be seen in the frontend. Will your SSR functionality also tackle this issue, or is it only that changes in the directives will not make those blocks invalid? Regarding the client-side navigations I am always a bit skeptical. I have yet to see a turbo(links)/pjax site that actually does feel like a native app. Especially, from a developer point of view, developing components for a long living site requires a different mental model than a site with traditional navigation. This should not be underestimated. I have seen tons of, also experienced, developers that have a hard time thinking about cleaning up dependencies, etc. when a component is destroyed. This goes from cleaning up global event listeners, timeouts/interval or external dependencies. WordPress websites are usually not complex web apps where developers deal with these kind of issues on a regular basis. For this to go smoothly, I expect a lot of education needing to be done. Thanks again for your effort. |
Beta Was this translation helpful? Give feedback.
-
Hi there. It is awesome to see that you are working on this topic. I also spend a lot of time on this and have quite a few things to ask and maybe also to contribute.
I just stumbled upon this repo today, so excuse me if I was not able to gather all information that is out there.
The first thing I want to ask I about the client side integration of the custom element and the directives for hydration: Are you planning for this to be a p/react thing?
While I understand your choice of using react for everything inside wp-admin, I do not think it would be a good idea to introduce a hard frontend dependency for building interactive blocks the WordPress way.
I see a lot of parallels to how WordPress used jQuery in the past, but today, the framework landscape is a lot more fragmented and I would really like to see WordPress not making a decision for me in this area.
If I get this correctly, your main goal is to improve UX and DX for interactive blocks. And you are kind of competing or at least being compared to full stack javascript meta/frameworks. This is somewhat unfair, of course, but the goal remains the same. And while WordPress with the block editor kind of uses react as SSR inside of the browser of the editor, there is still a large PHP system powering everything.
This is why for my exploration of the general topic of hydration (for a WordPress starter theme called Flynt) I have also tried to take a lot of inspiration from non full stack solutions. The most popular one being alpine.js.
It seems that with your directives approach you are moving into this direction, as well. I am wondering if WordPress should have such full featured functionality included? alpine.js also does not fit everyone. But maybe I am making wrong assumptions.
What happens currently when you stick alpine components into the save function of a block? Does this get in the way of block validation aso?
My personal opinion is that it would probably be enough to integrate something like catalyst or 11ty/is-land, ie a thin layer as a custom element that is just responsible for loading the javascript of a block when certain conditions are met. catalyst adds a bit more functionality that might not be needed either.
For the next release of Flynt we implemented something similar here, here, and here. However, we are not using the block editor, yet.
There are a lot more things that would be interesting for me to discuss. But that should be enough for now. I hope you found this useful. Good luck with all future work. Keep it up.
Beta Was this translation helpful? Give feedback.
All reactions