-
Notifications
You must be signed in to change notification settings - Fork 0
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
[web] Integrate Next.js with MUI v5 #58
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8d0d777
chore: Integrate Next.js with MUI v5
ipf-yychoi 68ac4fb
fix: Fix cannot find name error
ipf-yychoi 7226953
feat: Set prepend to true to move MUI styles to the top of the <head>
ipf-yychoi a3a7832
feat: Convert functional component to class component
ipf-yychoi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as React from 'react' | ||
import Document, { Html, Head, Main, NextScript } from 'next/document' | ||
import createEmotionServer from '@emotion/server/create-instance' | ||
import createEmotionCache from '@web/utils/createEmotionCache' | ||
|
||
export default class MyDocument extends Document { | ||
render() { | ||
return ( | ||
<Html lang="en"> | ||
<Head>{(this.props as any).emotionStyleTags}</Head> | ||
<body> | ||
<Main /> | ||
<NextScript /> | ||
</body> | ||
</Html> | ||
) | ||
} | ||
} | ||
|
||
MyDocument.getInitialProps = async (ctx) => { | ||
const originalRenderPage = ctx.renderPage | ||
|
||
const cache = createEmotionCache() | ||
const { extractCriticalToChunks } = createEmotionServer(cache) | ||
|
||
ctx.renderPage = () => | ||
originalRenderPage({ | ||
enhanceApp: (App: any) => | ||
function EnhanceApp(props) { | ||
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, | ||
emotionStyleTags, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import createCache from '@emotion/cache' | ||
|
||
const createEmotionCache = () => createCache({ key: 'css', prepend: true }) | ||
|
||
export default createEmotionCache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { grey } from '@mui/material/colors' | ||
import { createTheme, responsiveFontSizes } from '@mui/material/styles' | ||
|
||
let theme = createTheme({ | ||
palette: { | ||
primary: { | ||
main: grey[500], | ||
}, | ||
}, | ||
}) | ||
|
||
theme = responsiveFontSizes(theme) | ||
|
||
export default theme |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -945,6 +945,16 @@ | |
"@emotion/utils" "^1.0.0" | ||
csstype "^3.0.2" | ||
|
||
"@emotion/server@^11.4.0": | ||
version "11.4.0" | ||
resolved "https://registry.yarnpkg.com/@emotion/server/-/server-11.4.0.tgz#3ae1d74cb31c7d013c3c76e88c0c4439076e9f66" | ||
integrity sha512-IHovdWA3V0DokzxLtUNDx4+hQI82zUXqQFcVz/om2t44O0YSc+NHB+qifnyAOoQwt3SXcBTgaSntobwUI9gnfA== | ||
dependencies: | ||
"@emotion/utils" "^1.0.0" | ||
html-tokenize "^2.0.0" | ||
multipipe "^1.0.2" | ||
through "^2.3.8" | ||
|
||
"@emotion/sheet@^1.1.0": | ||
version "1.1.0" | ||
resolved "https://registry.yarnpkg.com/@emotion/sheet/-/sheet-1.1.0.tgz#56d99c41f0a1cda2726a05aa6a20afd4c63e58d2" | ||
|
@@ -3746,6 +3756,11 @@ buffer-from@^1.0.0: | |
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.2.tgz#2b146a6fd72e80b4f55d255f35ed59a3a9a41bd5" | ||
integrity sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ== | ||
|
||
buffer-from@~0.1.1: | ||
version "0.1.2" | ||
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-0.1.2.tgz#15f4b9bcef012044df31142c14333caf6e0260d0" | ||
integrity sha512-RiWIenusJsmI2KcvqQABB83tLxCByE3upSP8QU3rJDMVFGPWLvPQJt/O1Su9moRWeH7d+Q2HYb68f6+v+tw2vg== | ||
|
||
buffer@^5.5.0, buffer@^5.6.0, buffer@^5.7.0: | ||
version "5.7.1" | ||
resolved "https://registry.yarnpkg.com/buffer/-/buffer-5.7.1.tgz#ba62e7c13133053582197160851a8f648e99eed0" | ||
|
@@ -4794,6 +4809,13 @@ dotenv@^8.2.0: | |
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.6.0.tgz#061af664d19f7f4d8fc6e4ff9b584ce237adcb8b" | ||
integrity sha512-IrPdXQsk2BbzvCBGBOTmmSH5SodmqZNt4ERAZDmW4CT+tL8VtvinqywuANaFu4bOMWki16nqf0e4oC0QIaDr/g== | ||
|
||
duplexer2@^0.1.2: | ||
version "0.1.4" | ||
resolved "https://registry.yarnpkg.com/duplexer2/-/duplexer2-0.1.4.tgz#8b12dab878c0d69e3e7891051662a32fc6bddcc1" | ||
integrity sha1-ixLauHjA1p4+eJEFFmKjL8a93ME= | ||
dependencies: | ||
readable-stream "^2.0.2" | ||
|
||
duplexer3@^0.1.4: | ||
version "0.1.4" | ||
resolved "https://registry.yarnpkg.com/duplexer3/-/duplexer3-0.1.4.tgz#ee01dd1cac0ed3cbc7fdbea37dc0a8f1ce002ce2" | ||
|
@@ -6154,6 +6176,17 @@ html-tags@^3.1.0: | |
resolved "https://registry.yarnpkg.com/html-tags/-/html-tags-3.1.0.tgz#7b5e6f7e665e9fb41f30007ed9e0d41e97fb2140" | ||
integrity sha512-1qYz89hW3lFDEazhjW0yVAV87lw8lVkrJocr72XmBkMKsoSVJCQx3W8BXsC7hO2qAt8BoVjYjtAcZ9perqGnNg== | ||
|
||
html-tokenize@^2.0.0: | ||
version "2.0.1" | ||
resolved "https://registry.yarnpkg.com/html-tokenize/-/html-tokenize-2.0.1.tgz#c3b2ea6e2837d4f8c06693393e9d2a12c960be5f" | ||
integrity sha512-QY6S+hZ0f5m1WT8WffYN+Hg+xm/w5I8XeUcAq/ZYP5wVC8xbKi4Whhru3FtrAebD5EhBW8rmFzkDI6eCAuFe2w== | ||
dependencies: | ||
buffer-from "~0.1.1" | ||
inherits "~2.0.1" | ||
minimist "~1.2.5" | ||
readable-stream "~1.0.27-1" | ||
through2 "~0.4.1" | ||
|
||
http-cache-semantics@^4.0.0: | ||
version "4.1.0" | ||
resolved "https://registry.yarnpkg.com/http-cache-semantics/-/http-cache-semantics-4.1.0.tgz#49e91c5cbf36c9b94bcfcd71c23d5249ec74e390" | ||
|
@@ -8401,7 +8434,7 @@ [email protected]: | |
is-plain-obj "^1.1.0" | ||
kind-of "^6.0.3" | ||
|
||
[email protected], minimist@^1.1.1, minimist@^1.2.0, minimist@^1.2.5: | ||
[email protected], minimist@^1.1.1, minimist@^1.2.0, minimist@^1.2.5, minimist@~1.2.5: | ||
version "1.2.5" | ||
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602" | ||
integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw== | ||
|
@@ -8501,6 +8534,14 @@ [email protected]: | |
type-is "^1.6.4" | ||
xtend "^4.0.0" | ||
|
||
multipipe@^1.0.2: | ||
version "1.0.2" | ||
resolved "https://registry.yarnpkg.com/multipipe/-/multipipe-1.0.2.tgz#cc13efd833c9cda99f224f868461b8e1a3fd939d" | ||
integrity sha1-zBPv2DPJzamfIk+GhGG44aP9k50= | ||
dependencies: | ||
duplexer2 "^0.1.2" | ||
object-assign "^4.1.0" | ||
|
||
[email protected]: | ||
version "0.0.8" | ||
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.8.tgz#1630c42b2251ff81e2a283de96a5497ea92e5e0d" | ||
|
@@ -8740,6 +8781,11 @@ object-keys@^1.0.12, object-keys@^1.1.1: | |
resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-1.1.1.tgz#1c47f272df277f3b1daf061677d9c82e2322c60e" | ||
integrity sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA== | ||
|
||
object-keys@~0.4.0: | ||
version "0.4.0" | ||
resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-0.4.0.tgz#28a6aae7428dd2c3a92f3d95f21335dd204e0336" | ||
integrity sha1-KKaq50KN0sOpLz2V8hM13SBOAzY= | ||
|
||
object-visit@^1.0.0: | ||
version "1.0.1" | ||
resolved "https://registry.yarnpkg.com/object-visit/-/object-visit-1.0.1.tgz#f79c4493af0c5377b59fe39d395e41042dd045bb" | ||
|
@@ -9529,7 +9575,7 @@ readable-stream@3, readable-stream@^3.0.0, readable-stream@^3.4.0, readable-stre | |
string_decoder "^1.1.1" | ||
util-deprecate "^1.0.1" | ||
|
||
readable-stream@^2.2.2: | ||
readable-stream@^2.0.2, readable-stream@^2.2.2: | ||
version "2.3.7" | ||
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.7.tgz#1eca1cf711aef814c04f62252a36a62f6cb23b57" | ||
integrity sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw== | ||
|
@@ -9542,6 +9588,16 @@ readable-stream@^2.2.2: | |
string_decoder "~1.1.1" | ||
util-deprecate "~1.0.1" | ||
|
||
readable-stream@~1.0.17, readable-stream@~1.0.27-1: | ||
version "1.0.34" | ||
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.0.34.tgz#125820e34bc842d2f2aaafafe4c2916ee32c157c" | ||
integrity sha1-Elgg40vIQtLyqq+v5MKRbuMsFXw= | ||
dependencies: | ||
core-util-is "~1.0.0" | ||
inherits "~2.0.1" | ||
isarray "0.0.1" | ||
string_decoder "~0.10.x" | ||
|
||
readdirp@~3.6.0: | ||
version "3.6.0" | ||
resolved "https://registry.yarnpkg.com/readdirp/-/readdirp-3.6.0.tgz#74a370bd857116e245b29cc97340cd431a02a6c7" | ||
|
@@ -10850,6 +10906,14 @@ through2@^4.0.0: | |
dependencies: | ||
readable-stream "3" | ||
|
||
through2@~0.4.1: | ||
version "0.4.2" | ||
resolved "https://registry.yarnpkg.com/through2/-/through2-0.4.2.tgz#dbf5866031151ec8352bb6c4db64a2292a840b9b" | ||
integrity sha1-2/WGYDEVHsg1K7bE22SiKSqEC5s= | ||
dependencies: | ||
readable-stream "~1.0.17" | ||
xtend "~2.1.1" | ||
|
||
"through@>=2.2.7 <3", through@^2.3.6, through@^2.3.8: | ||
version "2.3.8" | ||
resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5" | ||
|
@@ -11644,6 +11708,13 @@ xtend@^4.0.0: | |
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54" | ||
integrity sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ== | ||
|
||
xtend@~2.1.1: | ||
version "2.1.2" | ||
resolved "https://registry.yarnpkg.com/xtend/-/xtend-2.1.2.tgz#6efecc2a4dad8e6962c4901b337ce7ba87b5d28b" | ||
integrity sha1-bv7MKk2tjmlixJAbM3znuoe10os= | ||
dependencies: | ||
object-keys "~0.4.0" | ||
|
||
y18n@^4.0.0: | ||
version "4.0.3" | ||
resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.3.tgz#b5f259c82cd6e336921efd7bfd8bf560de9eeedf" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@jordan-choi 님
@emotion/cache
랑@emotion/server
는 무슨 역할이에요?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.
@emotion/server
의 경우,<head>
안에 critical CSS를 넣기 위해서 사용합니다.@emotion/server
의createEmotionServer
메서드로 커스텀 서버를 만들고 criticalCSS를 추출한 후(extractCriticalToChunks
)<style>
태그로 만들어 initialProps에 포함시킵니다.CacheProvider
에서 (default 캐시가 아닌) custom cache를 제공하는데요. 이 때 공유할 custom cache를 만들 때@emotion/cache
의createCache
메서드를 사용합니다.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.
@emotion/server
는 js로 다뤄지는 css는 런타임에서 인라인으로 스타일을 공급하는데. 그렇게 하면 초기 렌더링시 레이아웃에 영향을 미치는 중요한(아마도 이게 critical css) 스타일들은 js페칭이 될 때까지 적용이 안될테니. 그걸 ssr 에서 head에 넣기 위해 제공되나 보군요.cache는 왜 필요한지 아직 몰라서 써봐야 알겠네요. 자세한 답변감사합니다~!