Mechanism to detect blocks compatible with the Interactivity API #54194
Replies: 5 comments 13 replies
-
Not a fan of calling these "Legacy" since they don't represent an older generation of interactivity and in fact may be new and continue to live side-by-side with WordPress' interactive blocks. They could be part of broader component systems on large sites and the Interactivity API might simply not be a viable option for them. That aside, is the problem that we are proposing to wipe out those blocks' rendered HTML from the server and try to render/hydrate them in the browser? I'm assuming those blocks would have no directives on them, so from our perspective, could it be that they are simply non-interactive? What would break if we don't make this distinction? |
Beta Was this translation helpful? Give feedback.
-
Thanks for the writeup, Luis. I was just talking with Pablo, and I pretty much agree with the way of declaring compatibility; it's the most obvious and direct one.
However, I found this confusing at first and asked myself whether this should be On the other hand, non-interactive blocks will indeed be compatible and should specify |
Beta Was this translation helpful? Give feedback.
-
Thanks for the detailed explanation, Luis 🙂 I'm not a big fan of declaring compatibility in the non-interactive blocks, but I cannot think of a better system either. Regarding the syntax, would it make sense to address the task to "Automatically detect interactive blocks" in the roadmap, and just have |
Beta Was this translation helpful? Give feedback.
-
Another idea, more explicit, could be to use the term client-side navigation: "this block supports client-side navigation". {
"supports": {
"clientSideNavigation": true
}
} Blocks made with the Interactivity API will be compatible by default. I'm not sure if it's a good idea or not yet. It largely depends on what other things will need compatibility, and whether those things will follow the same rules as client-side navigation, or different rules. I'll try to think about it. |
Beta Was this translation helpful? Give feedback.
-
We're finally going with This is the related PR: |
Beta Was this translation helpful? Give feedback.
-
Sometimes, we are going to need to know which blocks are compatible with the Interactivity API to make sure that the specific features that require all the blocks to be compatible, can be activated safely. For that, we need to figure out a mechanism to detect if a block is compatible with the Interactivity API.
Regarding this, there are three types of blocks:
To simplify talking about each block type, let's call them:
Only the Non-interactive and Interactivity API blocks are compatible.
We can detect Interactive API blocks because they are marked with
supports.interactivity: true
in theblock.json
.So we only need a way to distinguish Non-interactive blocks from Legacy Interactive blocks.
The main difference between them is that the Legacy Interactive blocks enqueue one or more JavaScript files that add interactivity in the frontend. The problem is that there is no way to detect that enqueue because it can happen practically at any moment and with any handle. It's true that the majority of these blocks will have a
viewScript
entry in theirblock.json
, but that's not guaranteed.If we can't reliably identify the Legacy Interactive blocks automatically, the only solution is to ask developers to declare something in their
block.json
. There are two options:If we ask developers to declare blocks that are not compatible, we need to assume that all blocks that are not declared, are compatible. This obviously doesn't work because it will lead to false positives when a Legacy Interactive block is not declared yet.
So the only viable option is to declare blocks that are compatible: the Non-interactive blocks. That way, if a block compatibility is undeclared, we can treat it as not compatible. This will lead to false negatives, but that's ok because we are sure that no Legacy Interactive block will slip through.
At this moment I don't have any good ideas about how to do this declaration. The most obvious one would be to just set
"supports.interactivity: false
but it doesn't convince me, so please share your ideas about this.If we do this, we would end up with:
There's an edge case of a block that is not compatible with some features like region-based client-side navigation: a block that conditionally enqueues different CSS files depending on a certain server condition, so that should also be a requirement for compatibility.
If someone wants one of their Non-interactive blocks to work, they just need to declare it. And if someone wants one of their Legacy Interactive blocks to work, they just need to migrate it to the Interactivity API and then declare it. Blocks that are not registered on the server would also be treated as unknown. If someone wants them to work, they have to create a
block.json
first.Now, for example, when someone tries to activate the enhanced pagination of the Query block, if all blocks that are inside the Post Template are compatible (they have
supports.interactivity: true/false
), the feature is activated without issues. If there's an unknown block, the user is notified that some specific block/s are incompatible, and the feature cannot be activated. If the feature is activated and the user introduces an unknown block in the Post Template, a message pops up saying that this block is not compatible and the feature has been deactivated.If someone wants to create a block that always uses one of these features they can prevent users from adding not-compatible inner blocks in the Editor. That way, they will ensure that the features will always work and don't have to circumvent activating/deactivating the feature when a not-compatible block is added.
If we adopt this system, we could start declaring all the Core blocks, which means that all Core blocks would be safe to use in all these scenarios from the beginning, and in the vast majority of cases, blocks like the Query block will only contain Core blocks, so they will work fine. Then, we can start educating block developers so they start declaring their compatibility as well.
All in all, I'm not crazy about the idea that everyone has to state that their blocks are non-interactive/compatible in their
block.json
, but I can't think of another way of making all this automatic and we should prioritize the UX over the DX.Please, let me know what you think.
cc: @mtias, @youknowriad, @oandregal, @mcsf, @gziolo, @jsnajdr, @DAreRodz, @SantosGuillamot, @dmsnell, @juanmaguitar, @ryanwelcher (feel free to ping other people)
Beta Was this translation helpful? Give feedback.
All reactions