From 7d64cc648ac7870f2f8b398bd2d79cadecbc0971 Mon Sep 17 00:00:00 2001 From: ShuPink <60080324+ShuPink@users.noreply.github.com> Date: Thu, 10 Feb 2022 14:20:18 +0800 Subject: [PATCH] Add prepend css but has unresolved FOUC issues --- .../app/entry.server.tsx | 28 ++++++------- examples/remix-with-typescript/app/root.tsx | 39 +++++++++++-------- .../app/routes/index.tsx | 13 +++++++ .../app/src/createEmotionCache.ts | 6 ++- .../app/styles/global.css | 3 ++ .../app/styles/index.css | 3 ++ 6 files changed, 61 insertions(+), 31 deletions(-) create mode 100644 examples/remix-with-typescript/app/styles/global.css create mode 100644 examples/remix-with-typescript/app/styles/index.css diff --git a/examples/remix-with-typescript/app/entry.server.tsx b/examples/remix-with-typescript/app/entry.server.tsx index c56e03758279c9..80191db72b9af9 100644 --- a/examples/remix-with-typescript/app/entry.server.tsx +++ b/examples/remix-with-typescript/app/entry.server.tsx @@ -33,22 +33,24 @@ export default function handleRequest( // Render the component to a string. const html = renderToString(); - // Grab the CSS from emotion - const { styles } = extractCriticalToChunks(html); + // // Grab the CSS from emotion + // const { styles } = extractCriticalToChunks(html); - let stylesHTML = ''; + // let stylesHTML = ''; - styles.forEach(({ key, ids, css }) => { - const emotionKey = `${key} ${ids.join(' ')}`; - const newStyleTag = ``; - stylesHTML = `${stylesHTML}${newStyleTag}`; - }); + // styles.forEach(({ key, ids, css }) => { + // const emotionKey = `${key} ${ids.join(' ')}`; + // const newStyleTag = ``; + // stylesHTML = `${stylesHTML}${newStyleTag}`; + // }); - // Add the emotion style tags after the insertion point meta tag - const markup = html.replace( - //, - `${stylesHTML}`, - ); + // // Add the emotion style tags after the insertion point meta tag + // const markup = html.replace( + // //, + // `${stylesHTML}`, + // ); + + const markup = html; responseHeaders.set('Content-Type', 'text/html'); diff --git a/examples/remix-with-typescript/app/root.tsx b/examples/remix-with-typescript/app/root.tsx index 664455c6e7eca4..decbfe9ced00a0 100644 --- a/examples/remix-with-typescript/app/root.tsx +++ b/examples/remix-with-typescript/app/root.tsx @@ -5,34 +5,40 @@ import { unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/material'; import theme from './src/theme'; import ClientStyleContext from './src/ClientStyleContext'; import Layout from './src/Layout'; +import styles from './styles/global.css'; interface DocumentProps { children: React.ReactNode; title?: string; } +export function links() { + return [{ rel: 'stylesheet', href: styles }]; +} + const Document = withEmotionCache(({ children, title }: DocumentProps, emotionCache) => { - const clientStyleData = React.useContext(ClientStyleContext); + // const clientStyleData = React.useContext(ClientStyleContext); - // Only executed on client - useEnhancedEffect(() => { - // re-link sheet container - emotionCache.sheet.container = document.head; - // re-inject tags - const tags = emotionCache.sheet.tags; - emotionCache.sheet.flush(); - tags.forEach((tag) => { - // eslint-disable-next-line no-underscore-dangle - (emotionCache.sheet as any)._insertTag(tag); - }); - // reset cache to reapply global styles - clientStyleData.reset(); - // eslint-disable-next-line react-hooks/exhaustive-deps - }, []); + // // Only executed on client + // useEnhancedEffect(() => { + // // re-link sheet container + // emotionCache.sheet.container = document.head; + // // re-inject tags + // const tags = emotionCache.sheet.tags; + // emotionCache.sheet.flush(); + // tags.forEach((tag) => { + // // eslint-disable-next-line no-underscore-dangle + // (emotionCache.sheet as any)._insertTag(tag); + // }); + // // reset cache to reapply global styles + // clientStyleData.reset(); + // // eslint-disable-next-line react-hooks/exhaustive-deps + // }, []); return ( + @@ -43,7 +49,6 @@ const Document = withEmotionCache(({ children, title }: DocumentProps, emotionCa rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" /> - {children} diff --git a/examples/remix-with-typescript/app/routes/index.tsx b/examples/remix-with-typescript/app/routes/index.tsx index f264c5b93a18e1..fdeedefa87f160 100644 --- a/examples/remix-with-typescript/app/routes/index.tsx +++ b/examples/remix-with-typescript/app/routes/index.tsx @@ -2,6 +2,12 @@ import * as React from 'react'; import type { MetaFunction } from 'remix'; import { Link } from 'remix'; import Typography from '@mui/material/Typography'; +import Button from '@mui/material/Button'; +import styles from '../styles/index.css'; + +export function links() { + return [{ rel: 'stylesheet', href: styles }]; +} // https://remix.run/api/conventions#meta export const meta: MetaFunction = () => { @@ -21,6 +27,13 @@ export default function Index() { Go to the about page +
+ Global css example. If prepend is working I should be monospace font otherwise I will be + roboto. +
+ ); } diff --git a/examples/remix-with-typescript/app/src/createEmotionCache.ts b/examples/remix-with-typescript/app/src/createEmotionCache.ts index 37fd756c3ca445..23fc9d849f00cb 100644 --- a/examples/remix-with-typescript/app/src/createEmotionCache.ts +++ b/examples/remix-with-typescript/app/src/createEmotionCache.ts @@ -1,5 +1,9 @@ import createCache from '@emotion/cache'; export default function createEmotionCache() { - return createCache({ key: 'css' }); + return createCache({ + key: 'css', + // prepend true to move added styles to top of head + prepend: true, + }); } diff --git a/examples/remix-with-typescript/app/styles/global.css b/examples/remix-with-typescript/app/styles/global.css new file mode 100644 index 00000000000000..2206be4a2c1be1 --- /dev/null +++ b/examples/remix-with-typescript/app/styles/global.css @@ -0,0 +1,3 @@ +body { + font-family: monospace; +} diff --git a/examples/remix-with-typescript/app/styles/index.css b/examples/remix-with-typescript/app/styles/index.css new file mode 100644 index 00000000000000..edb0a3cc0ad516 --- /dev/null +++ b/examples/remix-with-typescript/app/styles/index.css @@ -0,0 +1,3 @@ +.button { + background: red; +}