Skip to content
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

Dashboard Performance: HtmlContent improvements #4726

Merged
merged 2 commits into from
Mar 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions client/app/components/HtmlContent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import React from "react";
import PropTypes from "prop-types";
import { sanitize } from "dompurify";

export default function HtmlContent({ children, ...props }) {
const HtmlContent = React.memo(function HtmlContent({ children, ...props }) {
return (
<div
{...props}
dangerouslySetInnerHTML={{ __html: sanitize(children) }} // eslint-disable-line react/no-danger
/>
);
}
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried to use useMemo instead of React.memo? React.memo changes behavior of component (due to shallow comparison of props, component may not properly react to props changes):

const html = useMemo(() => ({ __html: sanitize(children) }), [children]);

return <div {...props} dangerouslySetInnerHTML={html} />

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, useMemo should have the same performance in concept, but not re-rendering the component at all looked better. For the HtmlContent component it should be totally dependent on its props. I quickly checked its usages, it has only an string for className (basic variable types don't create issues with shallow comparison) and the children node didn't seem a problem as well.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explanation! While I personally prefer useMemo, React.memo looks good as well 👍


HtmlContent.propTypes = {
children: PropTypes.string,
Expand All @@ -18,3 +18,5 @@ HtmlContent.propTypes = {
HtmlContent.defaultProps = {
children: "",
};

export default HtmlContent;
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ function VisualizationWidgetHeader({ widget, refreshStartedAt, parameters, onPar
<p>
<QueryLink query={widget.getQuery()} visualization={widget.visualization} readOnly={!canViewQuery} />
</p>
<HtmlContent className="text-muted markdown query--description">
{markdown.toHTML(widget.getQuery().description || "")}
</HtmlContent>
{!isEmpty(widget.getQuery().description) && (
<HtmlContent className="text-muted markdown query--description">
{markdown.toHTML(widget.getQuery().description || "")}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about markdown.toHTML performance? Is it fast enough, or maybe also needs some caching?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't cache it with useMemo because it runs widget.getQuery() currently, it would need to have the widget as a dependency. Since that component is basically re-rendered when widgetchanges, that didn't seem that relevant 🤔. Also now it only runs that function when the query has a description.

</HtmlContent>
)}
</div>
</div>
{!isEmpty(parameters) && (
Expand Down