Hydrate islands inside templates #53061
luisherranz
started this conversation in
Interactivity API
Replies: 0 comments 1 reply
-
I think that by far the best solution out of the above is:
We can make it work via a MutationObserver but we should consider whether using the customized built-in elements and |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
With the current island hydration algorithm, islands inside a
<template>
tag will not work once the template tag is deleted.For example, this
wp-text
will not work.This is because the HTML that exists inside the template tags does not belong to the DOM, and
document.querySelectorAll('[data-wp-interactive]')
(code) will not return those elements.There are several solutions:
Never use
<template>
.I don't like it, since using template tags has benefits.
Search
data-wp-interactive
in the content of each template tagUsing something like this (needs to be recursive):
I don't like it either because hydrating content that is inside a template tag is doing extra work on page load. Ideally, hydration should happen when those elements appear in the DOM.
Finding a way to auto-hydrate islands when they appear in the DOM
We would have to find a way similar to the one we used with the custom elements hydration where we used the connectedCallback to initiate the hydration once those elements appear in the DOM.
Since we cannot use custom-elements (see this), I think the only way to do this would be to use
MutationObserver
.You could use customized built-in elements and use
is="wp-interactive"
, but Safari will never support them. So instead of using a polyfill, we better useMutationObserver
directly.Manually execute the hydration in directives that use template tags, such as
wp-show
.Every time a directive pulls HTML content from a template, it would have to run a "rehydration" of that content, in case there were any new islands inside. Something along the lines of:
I don't like the idea of delegating that responsibility to developers. The framework should be self-sufficient to avoid bugs.
Any other ideas?
Beta Was this translation helpful? Give feedback.
All reactions