Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions apps/website/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { LinksSectionDetails, LinksSections } from "@/common/pagesList";
import Link from "next/link";
import StatusBadge from "@/common/StatusBadge";
import "../global-styles.css";
import createCache from "@emotion/cache";
import { CacheProvider } from "@emotion/react";
import createEmotionCache from "./createEmotionCache";

type NextPageWithLayout = NextPage & {
getLayout?: (_page: ReactElement) => ReactNode;
Expand All @@ -20,8 +20,9 @@ type AppPropsWithLayout = AppProps & {
Component: NextPageWithLayout;
};

const clientSideEmotionCache = createCache({ key: "css", prepend: true });

export default function App({ Component, pageProps }: AppPropsWithLayout) {
const clientSideEmotionCache = createEmotionCache();
const getLayout = Component.getLayout || ((page) => page);
const componentWithLayout = getLayout(<Component {...pageProps} />);
const [renderContent, setRenderContent] = useState(false);
Expand Down
64 changes: 32 additions & 32 deletions apps/website/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
import Document, { Html, Head, Main, NextScript } from "next/document";
import Document, { Head, Html, Main, NextScript } from "next/document";
import createEmotionServer from "@emotion/server/create-instance";
import createEmotionCache from "./createEmotionCache";
import * as React from "react";
import React from "react";
import createCache from "@emotion/cache";

export default class MyDocument extends Document {
static async getInitialProps(ctx: any) {
const originalRenderPage = ctx.renderPage;

const cache = createCache({ key: "css", prepend: true });
const { extractCriticalToChunks } = createEmotionServer(cache);

ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App: any) =>
function EnhanceApp(props: any) {
return <App emotionCache={cache} {...props} />;
},
});

const initialProps = await Document.getInitialProps(ctx);
const emotionStyles = extractCriticalToChunks(initialProps.html);
const emotionStyleTags = emotionStyles.styles.map((style) => (
<style
data-emotion={`${style.key} ${style.ids.join(" ")}`}
key={style.key}
dangerouslySetInnerHTML={{ __html: style.css }}
/>
));

return {
...initialProps,
styles: [...React.Children.toArray(initialProps.styles), ...emotionStyleTags],
};
}
render() {
return (
<Html lang="en">
Expand All @@ -16,32 +45,3 @@ export default class MyDocument extends Document {
);
}
}

MyDocument.getInitialProps = async (ctx) => {
const originalRenderPage = ctx.renderPage;

const cache = createEmotionCache();
const { extractCriticalToChunks, constructStyleTagsFromChunks } = createEmotionServer(cache);

ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App: any) =>
function EnhanceApp(props) {
return <App emotionCache={cache} {...props} />;
},
});

const initialProps = await Document.getInitialProps(ctx);
const emotionChunks = extractCriticalToChunks(initialProps.html);
const emotionStyleTags = constructStyleTagsFromChunks(emotionChunks);

return {
...initialProps,
styles: (
<>
{initialProps.styles}
<style data-emotion={`css`} dangerouslySetInnerHTML={{ __html: emotionStyleTags }} />
</>
),
};
};
6 changes: 0 additions & 6 deletions apps/website/pages/createEmotionCache.ts

This file was deleted.