Skip to content

Commit 4f3148a

Browse files
authored
Merge pull request #2252 from dxc-technology/Mil4n0r/fix-fouc-cache
Added fix for FOUC problem
2 parents 15df837 + 78329ec commit 4f3148a

File tree

3 files changed

+40
-35
lines changed

3 files changed

+40
-35
lines changed

apps/website/pages/_app.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +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";
1413
import { CacheProvider } from "@emotion/react";
14+
import createEmotionCache from "./createEmotionCache";
1515

1616
type NextPageWithLayout = NextPage & {
1717
getLayout?: (_page: ReactElement) => ReactNode;
@@ -20,9 +20,8 @@ type AppPropsWithLayout = AppProps & {
2020
Component: NextPageWithLayout;
2121
};
2222

23-
const clientSideEmotionCache = createCache({ key: "css", prepend: true });
24-
2523
export default function App({ Component, pageProps }: AppPropsWithLayout) {
24+
const clientSideEmotionCache = createEmotionCache();
2625
const getLayout = Component.getLayout || ((page) => page);
2726
const componentWithLayout = getLayout(<Component {...pageProps} />);
2827
const [renderContent, setRenderContent] = useState(false);

apps/website/pages/_document.tsx

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,9 @@
1-
import Document, { Head, Html, Main, NextScript } from "next/document";
1+
import Document, { Html, Head, Main, NextScript } from "next/document";
22
import createEmotionServer from "@emotion/server/create-instance";
3-
import React from "react";
4-
import createCache from "@emotion/cache";
3+
import createEmotionCache from "./createEmotionCache";
4+
import * as React from "react";
55

66
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-
}
367
render() {
378
return (
389
<Html lang="en">
@@ -45,3 +16,32 @@ export default class MyDocument extends Document {
4516
);
4617
}
4718
}
19+
20+
MyDocument.getInitialProps = async (ctx) => {
21+
const originalRenderPage = ctx.renderPage;
22+
23+
const cache = createEmotionCache();
24+
const { extractCriticalToChunks, constructStyleTagsFromChunks } = createEmotionServer(cache);
25+
26+
ctx.renderPage = () =>
27+
originalRenderPage({
28+
enhanceApp: (App: any) =>
29+
function EnhanceApp(props) {
30+
return <App emotionCache={cache} {...props} />;
31+
},
32+
});
33+
34+
const initialProps = await Document.getInitialProps(ctx);
35+
const emotionChunks = extractCriticalToChunks(initialProps.html);
36+
const emotionStyleTags = constructStyleTagsFromChunks(emotionChunks);
37+
38+
return {
39+
...initialProps,
40+
styles: (
41+
<>
42+
{initialProps.styles}
43+
<style data-emotion={`css`} dangerouslySetInnerHTML={{ __html: emotionStyleTags }} />
44+
</>
45+
),
46+
};
47+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import createCache from "@emotion/cache";
2+
import type { EmotionCache } from "@emotion/utils";
3+
4+
export default function createEmotionCache(): EmotionCache {
5+
return createCache({ key: "css", prepend: true });
6+
}

0 commit comments

Comments
 (0)