Skip to content

Commit

Permalink
chore(website): Do not lazy load icons through next/head
Browse files Browse the repository at this point in the history
  • Loading branch information
mlaursen committed Mar 10, 2022
1 parent 2d2d1e9 commit 47ccc1d
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions packages/documentation/src/components/Demos/DemoPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ReactElement } from "react";
import { useEffect } from "react";
import cn from "classnames";
import { defaults } from "lodash";
import Head from "next/head";

import { toId } from "utils/toTitle";

Expand Down Expand Up @@ -45,22 +45,40 @@ export default function DemoPage(props: DemoPageProps): ReactElement {
className,
fonts = EMPTY_LIST,
} = props;
useEffect(() => {
const head = document.querySelector("head");
if (!head || !fonts.length) {
return;
}

const links: HTMLLinkElement[] = [];
fonts.forEach((font) => {
let href: string;
if (font === "Font Awesome") {
href =
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css";
} else {
const name = font.replace(/\s+/g, "+");
href = `https://fonts.googleapis.com/css2?family=${name}&display=swap`;
}
const link = document.createElement("link");

link.rel = "stylesheet";
link.href = href;

head.appendChild(link);
links.push(link);
});

return () => {
links.forEach((link) => {
head.removeChild(link);
});
};
}, [fonts]);

return (
<div id="demo-page-container" className={cn(styles.container, className)}>
<Head>
{fonts.map((font) => {
let href: string;
if (font === "Font Awesome") {
href =
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css";
} else {
const name = font.replace(/\s+/g, "+");
href = `https://fonts.googleapis.com/css2?family=${name}&display=swap`;
}

return <link key={font} rel="stylesheet" href={href} />;
})}
</Head>
<DemoPageHeader packageName={packageName}>{description}</DemoPageHeader>
{demos.map((demo, index) => (
// eslint-disable-next-line react/jsx-key
Expand Down

1 comment on commit 47ccc1d

@vercel
Copy link

@vercel vercel bot commented on 47ccc1d Mar 10, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.