To style math equations, I need to load the Katex CSS. The pages that require this styling are not predetermined, as it depends on which users include math in their posts. However, I do know which routes might potentially need it.
For a better understanding, you can visit a page with math here, and one without math here.
Currently, I am loading the CSS using a <link>
tag in the header of my root layout, as shown below:
// src/app/layout.jsx
export default async function RootLayout({ children }) {
return (
<html suppressHydrationWarning lang="en">
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fd969c899885bdcdd3cccbd3c5">[email protected]</a>/dist/katex.min.css" />
</head>
<body>
<main>
{ children }
</main>
</body>
</html>
)
}
This approach leads to it being categorized as a "render blocking resource," as indicated by my lighthouse report.
https://i.sstatic.net/6KnatOBM.png
If feasible, I would prefer to lazily and dynamically load the CSS based on whether the current page contains math that requires styling.
Despite reviewing the documentation on CSS and lazy loading, such as CSS and lazy loading, I am still uncertain how to achieve this.