Skip to content

Commit 47ccc1d

Browse files
committed
chore(website): Do not lazy load icons through next/head
1 parent 2d2d1e9 commit 47ccc1d

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

packages/documentation/src/components/Demos/DemoPage.tsx

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ReactElement } from "react";
2+
import { useEffect } from "react";
23
import cn from "classnames";
34
import { defaults } from "lodash";
4-
import Head from "next/head";
55

66
import { toId } from "utils/toTitle";
77

@@ -45,22 +45,40 @@ export default function DemoPage(props: DemoPageProps): ReactElement {
4545
className,
4646
fonts = EMPTY_LIST,
4747
} = props;
48+
useEffect(() => {
49+
const head = document.querySelector("head");
50+
if (!head || !fonts.length) {
51+
return;
52+
}
53+
54+
const links: HTMLLinkElement[] = [];
55+
fonts.forEach((font) => {
56+
let href: string;
57+
if (font === "Font Awesome") {
58+
href =
59+
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css";
60+
} else {
61+
const name = font.replace(/\s+/g, "+");
62+
href = `https://fonts.googleapis.com/css2?family=${name}&display=swap`;
63+
}
64+
const link = document.createElement("link");
65+
66+
link.rel = "stylesheet";
67+
link.href = href;
68+
69+
head.appendChild(link);
70+
links.push(link);
71+
});
72+
73+
return () => {
74+
links.forEach((link) => {
75+
head.removeChild(link);
76+
});
77+
};
78+
}, [fonts]);
79+
4880
return (
4981
<div id="demo-page-container" className={cn(styles.container, className)}>
50-
<Head>
51-
{fonts.map((font) => {
52-
let href: string;
53-
if (font === "Font Awesome") {
54-
href =
55-
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css";
56-
} else {
57-
const name = font.replace(/\s+/g, "+");
58-
href = `https://fonts.googleapis.com/css2?family=${name}&display=swap`;
59-
}
60-
61-
return <link key={font} rel="stylesheet" href={href} />;
62-
})}
63-
</Head>
6482
<DemoPageHeader packageName={packageName}>{description}</DemoPageHeader>
6583
{demos.map((demo, index) => (
6684
// eslint-disable-next-line react/jsx-key

0 commit comments

Comments
 (0)