-
Notifications
You must be signed in to change notification settings - Fork 4
feat: support custom rehydratable query selectors #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: chrisvxd/remove-markup-containers
Are you sure you want to change the base?
feat: support custom rehydratable query selectors #49
Conversation
const rehydratorName = el.getAttribute("data-rehydratable"); | ||
const rehydratorSelector = Object.keys(options.allSelectors).find(selector => | ||
el.matches(selector) | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performance consideration: This introduces a find
loop on each call of rehydratableToReactElement
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ( | ||
node.nodeType === Node.ELEMENT_NODE && | ||
(node as Element).hasAttribute("data-rehydratable") | ||
(node as Element).matches(options.compoundSelector) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performance consideration: How does .matches
compare to hasAttribute
?
); | ||
|
||
const compoundSelector = Object.keys(allSelectors).reduce( | ||
(acc: string, selector: string) => `${acc ? `${acc}, ` : ""}${selector}`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performance consideration: Additional loop on each hydrate
, but only called once per hydrate.
27b8f22
to
b7ffe51
Compare
Blocked by #48, and have based this on that.
What?
Enables custom query selectors, which can eliminate any custom markup.
Instead of this
You can now have this
By configuring your rehydrate method with the new
getQuerySelector()
methodHow?
This PR extends replaces reading of the
data-rehydratable
attribute with a query selector. By default, this query selector will check fordata-rehydratable
, but this can be overridden using thegetQuerySelector
param.You can get quite creative with your selectors, targeting whatever you want.
Breaking changes
This PR contains no breaking changes
Considerations
There is a slight performance penalty to doing this instead of reading the
data-rehydratable
attribute directly, in particular due to the introduction of some new loops. I'll do my best to point them out via PR line comments.TODO