Unverified Commit 1bf2b74f authored by lijianan's avatar lijianan Committed by GitHub

examples: avoid duplicate css insert for ant design (#57947)

Co-authored-by: 's avatarLee Robinson <me@leerob.io>
parent fd90690a
...@@ -5,18 +5,22 @@ import { useServerInsertedHTML } from "next/navigation"; ...@@ -5,18 +5,22 @@ import { useServerInsertedHTML } from "next/navigation";
import { StyleProvider, createCache, extractStyle } from "@ant-design/cssinjs"; import { StyleProvider, createCache, extractStyle } from "@ant-design/cssinjs";
import type Entity from "@ant-design/cssinjs/es/Cache"; import type Entity from "@ant-design/cssinjs/es/Cache";
interface StyledRegistryProps { const StyledComponentsRegistry = ({ children }: React.PropsWithChildren) => {
children: React.ReactNode;
}
const StyledComponentsRegistry = ({ children }: StyledRegistryProps) => {
const cache = React.useMemo<Entity>(() => createCache(), []); const cache = React.useMemo<Entity>(() => createCache(), []);
useServerInsertedHTML(() => ( const isServerInserted = React.useRef<boolean>(false);
<style useServerInsertedHTML(() => {
id="antd" // avoid duplicate css insert
dangerouslySetInnerHTML={{ __html: extractStyle(cache, true) }} if (isServerInserted.current) {
/> return;
)); }
isServerInserted.current = true;
return (
<style
id="antd"
dangerouslySetInnerHTML={{ __html: extractStyle(cache, true) }}
/>
);
});
return <StyleProvider cache={cache}>{children}</StyleProvider>; return <StyleProvider cache={cache}>{children}</StyleProvider>;
}; };
......
...@@ -11,11 +11,7 @@ export const metadata: Metadata = { ...@@ -11,11 +11,7 @@ export const metadata: Metadata = {
description: "Generated by create next app", description: "Generated by create next app",
}; };
interface RootLayoutProps { function RootLayout({ children }: React.PropsWithChildren) {
children: React.ReactNode;
}
function RootLayout({ children }: RootLayoutProps) {
return ( return (
<html lang="en"> <html lang="en">
<body className={inter.className}> <body className={inter.className}>
......
...@@ -10,8 +10,8 @@ ...@@ -10,8 +10,8 @@
"@ant-design/icons": "^5.1.4", "@ant-design/icons": "^5.1.4",
"antd": "^5.0.0", "antd": "^5.0.0",
"next": "latest", "next": "latest",
"react": "18.2.0", "react": "^18.2.0",
"react-dom": "18.2.0" "react-dom": "^18.2.0"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^20.4.5", "@types/node": "^20.4.5",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment