Skip to content

Commit

Permalink
perf: avoid unnecessary re-rendering for demos (#1700)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachScript committed May 30, 2023
1 parent 84d3bdf commit bc79d2c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 34 deletions.
66 changes: 36 additions & 30 deletions src/client/theme-api/DumiDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,43 @@ const DemoErrorBoundary: FC<{ children: ReactNode }> = (props) => (
</ErrorBoundary>
);

export const DumiDemo: FC<IDumiDemoProps> = (props) => {
const { demos, historyType } = useSiteData();
const { basename } = useAppData();
const { component, asset } = demos[props.demo.id];
export const DumiDemo: FC<IDumiDemoProps> = React.memo(
(props) => {
const { demos, historyType } = useSiteData();
const { basename } = useAppData();
const { component, asset } = demos[props.demo.id];

// hide debug demo in production
if (process.env.NODE_ENV === 'production' && props.previewerProps.debug)
return null;
// hide debug demo in production
if (process.env.NODE_ENV === 'production' && props.previewerProps.debug)
return null;

if (props.demo.inline) {
return <DemoErrorBoundary>{createElement(component)}</DemoErrorBoundary>;
}
if (props.demo.inline) {
return <DemoErrorBoundary>{createElement(component)}</DemoErrorBoundary>;
}

const isHashRoute = historyType === 'hash';
const isHashRoute = historyType === 'hash';

return (
<Previewer
asset={asset}
demoUrl={
// allow user override demoUrl by frontmatter
props.previewerProps.demoUrl ||
// when use hash route, browser can automatically handle relative paths starting with #
`${isHashRoute ? `#` : ''}${basename}${SP_ROUTE_PREFIX}demos/${
props.demo.id
}`
}
{...props.previewerProps}
>
{props.previewerProps.iframe ? null : (
<DemoErrorBoundary>{createElement(component)}</DemoErrorBoundary>
)}
</Previewer>
);
};
return (
<Previewer
asset={asset}
demoUrl={
// allow user override demoUrl by frontmatter
props.previewerProps.demoUrl ||
// when use hash route, browser can automatically handle relative paths starting with #
`${isHashRoute ? `#` : ''}${basename}${SP_ROUTE_PREFIX}demos/${
props.demo.id
}`
}
{...props.previewerProps}
>
{props.previewerProps.iframe ? null : (
<DemoErrorBoundary>{createElement(component)}</DemoErrorBoundary>
)}
</Previewer>
);
},
(prev, next) => {
// compare length for performance
return JSON.stringify(prev).length === JSON.stringify(next).length;
},
);
4 changes: 2 additions & 2 deletions src/loaders/markdown/transformer/rehypeDemo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,9 @@ export default function rehypeDemo(
localId,
vFile.data.frontmatter!.atomId,
);
component = `React.lazy(() => import( /* webpackChunkName: "${chunkName}" */ '${winPath(
component = `React.memo(React.lazy(() => import( /* webpackChunkName: "${chunkName}" */ '${winPath(
parseOpts.fileAbsPath,
)}?techStack=${techStack.name}'))`;
)}?techStack=${techStack.name}')))`;
// use code value as title
// TODO: force checking
if (codeValue) codeNode.properties!.title = codeValue;
Expand Down
4 changes: 2 additions & 2 deletions src/techStacks/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export default class ReactTechStack implements IDumiTechStack {
},
});

return `React.lazy(async () => {
return `React.memo(React.lazy(async () => {
${code}
})`;
}))`;
}

return raw;
Expand Down

0 comments on commit bc79d2c

Please sign in to comment.