Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[example] Avoid double rendering in the Remix example #30366

Merged
merged 3 commits into from
Jan 3, 2022
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
26 changes: 14 additions & 12 deletions examples/remix-with-typescript/app/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type { EntryContext } from 'remix';

import createEmotionCache from './src/createEmotionCache';
import theme from './src/theme';
import StylesContext from './src/StylesContext';

import CssBaseline from '@mui/material/CssBaseline';
import { ThemeProvider } from '@mui/material/styles';
Expand All @@ -32,20 +31,23 @@ export default function handleRequest(
);

// Render the component to a string.
const html = renderToString(
<StylesContext.Provider value={null}>
<MuiRemixServer />
</StylesContext.Provider>,
);
const html = renderToString(<MuiRemixServer />);

// Grab the CSS from emotion
const emotionChunks = extractCriticalToChunks(html);
const { styles } = extractCriticalToChunks(html);

let stylesHTML = '';

styles.forEach(({ key, ids, css }) => {
const emotionKey = `${key} ${ids.join(' ')}`;
const newStyleTag = `<style data-emotion="${emotionKey}">${css}</style>`;
stylesHTML = `${stylesHTML}${newStyleTag}`;
});

// Re-render including the extracted css.
const markup = renderToString(
<StylesContext.Provider value={emotionChunks.styles}>
<MuiRemixServer />
</StylesContext.Provider>,
// Add the emotion style tags after the insertion point meta tag
const markup = html.replace(
/<meta(\s)*name="emotion-insertion-point"(\s)*content="emotion-insertion-point"(\s)*\/>/,
`<meta name="emotion-insertion-point" content="emotion-insertion-point"/>${stylesHTML}`,
);

responseHeaders.set('Content-Type', 'text/html');
Expand Down
13 changes: 1 addition & 12 deletions examples/remix-with-typescript/app/root.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import * as React from 'react';
import { Links, LiveReload, Meta, Outlet, Scripts, ScrollRestoration, useCatch } from 'remix';
import { useContext } from 'react';
import StylesContext from './src/StylesContext';
import theme from './src/theme';

import Layout from './src/Layout';

function Document({ children, title }: { children: React.ReactNode; title?: string }) {
const styleData = useContext(StylesContext);

return (
<html lang="en">
<head>
Expand All @@ -22,14 +18,7 @@ function Document({ children, title }: { children: React.ReactNode; title?: stri
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
/>
{styleData?.map(({ key, ids, css }) => (
<style
key={key}
data-emotion={`${key} ${ids.join(' ')}`}
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{ __html: css }}
/>
))}
<meta name="emotion-insertion-point" content="emotion-insertion-point" />
</head>
<body>
{children}
Expand Down
9 changes: 0 additions & 9 deletions examples/remix-with-typescript/app/src/StylesContext.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion examples/remix-with-typescript/remix.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
module.exports = {
appDirectory: 'app',
assetsBuildDirectory: 'public/build',
browserBuildDirectory: 'public/build',
publicPath: '/build/',
serverBuildDirectory: 'build',
devServerPort: 8002,
Expand Down