Skip to content

Commit 7d64b67

Browse files
authored
Merge pull request #2250 from dxc-technology/jialecl-emotion
Emotion cache added
2 parents e35a79c + 0d0ed56 commit 7d64b67

File tree

5 files changed

+233
-5
lines changed

5 files changed

+233
-5
lines changed

apps/website/next.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ module.exports = {
2525
"@cloudscape-design/components",
2626
"@cloudscape-design/component-toolkit",
2727
"@cloudscape-design/theming-runtime",
28+
"react-data-grid",
2829
],
2930
};

apps/website/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
"dependencies": {
1111
"@cloudscape-design/components": "^3.0.706",
1212
"@dxc-technology/halstack-react": "*",
13+
"@emotion/cache": "^11.14.0",
1314
"@emotion/react": "^11.14.0",
15+
"@emotion/server": "^11.11.0",
1416
"@emotion/styled": "^11.14.0",
1517
"@radix-ui/react-popover": "^1.0.7",
1618
"cross-env": "^7.0.3",

apps/website/pages/_app.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { LinksSectionDetails, LinksSections } from "@/common/pagesList";
1010
import Link from "next/link";
1111
import StatusBadge from "@/common/StatusBadge";
1212
import "../global-styles.css";
13+
import createCache from "@emotion/cache";
14+
import { CacheProvider } from "@emotion/react";
1315

1416
type NextPageWithLayout = NextPage & {
1517
getLayout?: (_page: ReactElement) => ReactNode;
@@ -18,6 +20,8 @@ type AppPropsWithLayout = AppProps & {
1820
Component: NextPageWithLayout;
1921
};
2022

23+
const clientSideEmotionCache = createCache({ key: "css", prepend: true });
24+
2125
export default function App({ Component, pageProps }: AppPropsWithLayout) {
2226
const getLayout = Component.getLayout || ((page) => page);
2327
const componentWithLayout = getLayout(<Component {...pageProps} />);
@@ -43,7 +47,7 @@ export default function App({ Component, pageProps }: AppPropsWithLayout) {
4347
};
4448

4549
return (
46-
<>
50+
<CacheProvider value={clientSideEmotionCache}>
4751
<Head>
4852
<link rel="icon" type="image/png" sizes="32x32" href="/favicon.png" />
4953
</Head>
@@ -96,6 +100,6 @@ export default function App({ Component, pageProps }: AppPropsWithLayout) {
96100
</DxcToastsQueue>
97101
</DxcApplicationLayout.Main>
98102
</DxcApplicationLayout>
99-
</>
103+
</CacheProvider>
100104
);
101105
}

apps/website/pages/_document.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
import Document, { Head, Html, Main, NextScript } from "next/document";
2+
import createEmotionServer from "@emotion/server/create-instance";
3+
import React from "react";
4+
import createCache from "@emotion/cache";
25

36
export default class MyDocument extends Document {
7+
static async getInitialProps(ctx: any) {
8+
const originalRenderPage = ctx.renderPage;
9+
10+
const cache = createCache({ key: "css", prepend: true });
11+
const { extractCriticalToChunks } = createEmotionServer(cache);
12+
13+
ctx.renderPage = () =>
14+
originalRenderPage({
15+
enhanceApp: (App: any) =>
16+
function EnhanceApp(props: any) {
17+
return <App emotionCache={cache} {...props} />;
18+
},
19+
});
20+
21+
const initialProps = await Document.getInitialProps(ctx);
22+
const emotionStyles = extractCriticalToChunks(initialProps.html);
23+
const emotionStyleTags = emotionStyles.styles.map((style) => (
24+
<style
25+
data-emotion={`${style.key} ${style.ids.join(" ")}`}
26+
key={style.key}
27+
dangerouslySetInnerHTML={{ __html: style.css }}
28+
/>
29+
));
30+
31+
return {
32+
...initialProps,
33+
styles: [...React.Children.toArray(initialProps.styles), ...emotionStyleTags],
34+
};
35+
}
436
render() {
537
return (
638
<Html lang="en">

package-lock.json

Lines changed: 192 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)