-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[examples] Fix CSS modules integration #30381
[examples] Fix CSS modules integration #30381
Conversation
3b645e9
to
ffe52eb
Compare
ffe52eb
to
88a00a6
Compare
795c5ac
to
c2854e0
Compare
@@ -11,7 +11,7 @@ import { useUserLanguage } from 'docs/src/modules/utils/i18n'; | |||
* | |||
* - /docs/src/modules/components/Link.tsx | |||
* - /examples/nextjs/src/Link.tsx | |||
* - /examples/nextjs-with-typescript/src/components/Link.tsx | |||
* - /examples/nextjs-with-typescript/src/Link.tsx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flatten the src
folder to make it easier to discover for new users (less overwhelming).
<!-- #default-branch-switch --> | ||
|
||
[![Edit on StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/mui-org/material-ui/tree/master/examples/create-react-app-with-typescript) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative
or: | ||
|
||
<!-- #default-branch-switch --> | ||
|
||
[![Edit on CodeSandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/mui-org/material-ui/tree/master/examples/gatsby) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not working and Gatsby's popularity is low, trending downward, so probably not worth trying to fix this.
import * as React from 'react'; | ||
import { CacheProvider } from '@emotion/react'; | ||
import createEmotionServer from '@emotion/server/create-instance'; | ||
import { renderToString } from 'react-dom/server'; | ||
import getEmotionCache from './getEmotionCache'; | ||
|
||
// Inject MUI styles first to match with the prepend: true configuration. | ||
export const onPreRenderHTML = ({ getHeadComponents, replaceHeadComponents }) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixes the initial HTML output (before JS hydration), #30358 was not enough.
{ | ||
"container": { | ||
"node": "12" | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Legacy file for an old bug with Codesandbox
@@ -8,7 +8,6 @@ | |||
"@emotion/styled": "latest", | |||
"@emotion/server": "latest", | |||
"@mui/material": "latest", | |||
"clsx": "latest", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer needed with the styled() API
module.exports = { | ||
reactStrictMode: true, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A healthy default for any new project. It was added in #29974 for TypeScript but we forgot about the JS version.
@@ -21,7 +21,6 @@ export default function Head(props) { | |||
const preview = card.startsWith('http') ? card : `${HOST}${card}`; | |||
return ( | |||
<NextHead> | |||
<meta name="viewport" content="initial-scale=1, width=device-width" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's stable, no need to set it on each page. _app should do it.
@@ -6,7 +6,8 @@ | |||
"dev": "next dev", | |||
"build": "next build", | |||
"start": "next start", | |||
"lint": "next lint" | |||
"lint": "next lint", | |||
"post-update": "echo \"codesandbox preview only, need an update\" && yarn upgrade --latest" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix regression, it was removed in #29974.
@@ -19,7 +19,6 @@ export default function MyApp(props: MyAppProps) { | |||
return ( | |||
<CacheProvider value={emotionCache}> | |||
<Head> | |||
<title>Change title in _app.tsx</title> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be set on each page, not once for all.
or: | ||
|
||
<!-- #default-branch-switch --> | ||
|
||
[![Edit on CodeSandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/mui-org/material-ui/tree/master/examples/ssr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not working.
noLinkStyle?: boolean; | ||
} & Omit<NextLinkComposedProps, 'to' | 'linkAs' | 'href'> & | ||
Omit<MuiLinkProps, 'href'>; | ||
|
||
// A styled version of the Next.js Link component: | ||
// https://nextjs.org/docs/#with-link | ||
// https://nextjs.org/docs/api-reference/next/link |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regression introduced in #29974
c2854e0
to
c0dbba9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Great improvement, especially for the CSS modules integration!
I tried the latest Since the title of this PR is |
@RemyMachado Do you have a reproduction I can look at? |
@oliviertassinari I just used the latest nextjs-typescript example available on mui repository and added an index.module.css When inspecting the network devtool. I can see that the server is only responsding with the emotion styling. Completely ignoring the css modules. |
Is the FOUC issue present in your production build as well? I tried running your sandbox in prod and I could see the css sheets show up in the |
Indeed, these links are showing up only on production: <link rel="preload" href="/_next/static/css/b8bc79d340957044.css" as="style"/>
<link rel="stylesheet" href="/_next/static/css/b8bc79d340957044.css" data-n-p=""/> I can't tell if it's the right file |
@RemyMachado Ok thanks for the extra details, This is not related to MUI AFAIK. It's specific to Next.js. I have taken your reproduction and removed MUI to prove it: https://github.com/oliviertassinari/fooc-nextjs-css-modules. Now, AFAIK, there is no FOUC regardless of the content of the HTML output in dev mode. Why? Next.js loads the CSS using a JS script that is blocking until the first pain. Still, feel free to report the pain point to Next.js. I was also confused to not see the CSS loading with JavaScript disabled in dev mode at first. |
<head>
element. This was leading to flickering.<style>
is added in the right location in the<head>
relative to the<link>
used to host the CSS modules styles.This is also important when using Tailwind CSS