Nextjs-Optimizing(1) - Font
๐์์ํ๋ฉฐ
ํด๊ทผ ์ , ๋ค๋ฅธ ํ๋ก ํธ์๋ ๊ฐ๋ฐ์ ๋ถ๊ณผ, ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฒ๋ค ์ฌ์ด์ฆ์ ๋ํด ์ด์ผ๊ธฐ๋ฅผ ๋๋์๋ค. ์ต์ ํ์ ์ค์์ฑ์ ๋๋ฌด ๋น์ฐํ ๊ฒ์ด๊ธฐ๋ ํด์, ๋ฒ๋ค ์ฌ์ด์ฆ๋ฅผ ์ค์ด๋ ๊ฒ์ด๋, ์ต์ ํ ์งํ์ ๋ํด ๊ณต๊ฐํ๊ณ , ๊ธฐ๋ณธ์ ์ผ๋ก ์๊ณ ์๋ ๋ด์ฉ์ ๋ํด Next js๊ฐ ์ ๊ณตํ๋ ์ต์ ํ ๋ฐฉ์๋ ์์๋๋ฉด ์ข์ ๊ฒ ๊ฐ์์, ํ๋์ Next js์ ๊ณต์๋ฌธ์์ Optimizing ๋ถ๋ถ์ ํํค์ณ ๋ณผ ์๊ฐ์ด๋ค.๐
๋จผ์ ์ค๋์ ๊ฐ๋ณ๊ฒ(?) ๊ธฐ์กด์ ์์ฃผ ์ฌ์ฉํ๋ Next/font
์ ๊ดํ ์ด์ผ๊ธฐ๋ค.
์๋ ๋ด์ฉ์ ๊ณต์๋ฌธ์๋ฅผ ๋ฒ์ญํ๊ฑฐ๋, ์ง์ ์ฌ์ฉํด๋ณด๊ณ ์ถ๊ฐํ ๋ด์ฉ์ด ๋ด๊ฒจ์๋ค.
๐นGoogle Fonts
next/font/google
๋ฅผ ์ฌ์ฉํ๋ฉด, ์ ํ๋ฆฌ์ผ์ด์
์ ๋น๋ ๊ณผ์ ์์ ๊ตฌ๊ธ ํฐํธ๋ฅผ ๋ค์ด๋ก๋ํ์ฌ ๋ก์ปฌ๋ก ํฌํจ์ํค๊ธฐ ๋๋ฌธ์, ํฐํธ ํ์ผ์ ๋น๋ ๊ฒฐ๊ณผ๋ฌผ์ ํฌํจ๋๋ค. ๋ฐ๋ผ์, ์ฌ์ฉ์๊ฐ ์น์ฌ์ดํธ๋ฅผ ๋ฐฉ๋ฌธํ ๋๋ง๋ค ๊ตฌ๊ธ ์๋ฒ์ ์์ฒญ์ ๋ณด๋ด์ง ์๊ณ , ๋ก์ปฌ ์๋ฒ์์ ํด๋น ํฐํธ๊ฐ ์ ๊ณต๋๋ฏ๋ก ๊ฐ์ธ์ ๋ณด ๋ณดํธ์ ์ฑ๋ฅ์ ์ด์ ์ด ์๋ค.
์ฌ์ฉ๋ฐฉ์์ ๋ค์๊ณผ ๊ฐ๋ค!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import "./globals.css"
import Provider from "./Provider"
import Header from "@/components/Header"
import { Noto_Sans_KR } from "next/font/google"
// ๋ค์ํ weight์ ๊ธ๊ผด(Variable font)์ ๋ก๋ํ๋ ๊ฒฝ์ฐ ๋ฐ๋ก weight๋ฅผ ์ง์ ํ ํ์ ์์!
const noto = Noto_Sans_KR({
subsets: ["latin"],
display: "swap",
})
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="ko" className={noto.className}>
<body>
<Provider>
<Header />
{children}
</Provider>
</body>
</html>
)
}
๋ง์ฝ ํน์ weight์ ๊ธ๊ผด๋ง ํ์ํ๋ค๋ฉด, ์๋์ ๊ฐ์ด ์์ฑํด์ค๋ค. ์ด ๊ฒฝ์ฐ์, ์ง์ CSS๋ก font-weight๋ฅผ ์กฐ์ ํ๋ ค๊ณ ํ๋๋ผ๋, bold
์ border
์ต์
์ธ์ weight๊ฐ ๋ณํ์ง ์๋๋ค.
1
2
3
4
5
const noto = Noto_Sans_KR({
weight: "400",
subsets: ["latin"],
display: "swap",
})
1
2
3
4
5
const noto = Noto_Sans_KR({
weight: ["400", "700"],
subsets: ["latin"],
display: "swap",
})
๐นTailwind์ ํจ๊ป ์ฌ์ฉํ๊ธฐ
์ฌ์ฉํ ํฐํธ๊ฐ ๋ง์ผ๋ฉด, tailwind์ ํฐํธ๋ฅผ ์ ์ํด๋๊ณ ์ฌ์ฉํ๋ ๊ฒ์ด ํธ๋ฆฌํ๋ ํจ๊ป ์ฌ์ฉํด๋ณด์ ๋จผ์ ํฐํธ ๋ด์ฉ์ ์ ์ํด์ฃผ์๋ค. ์ฌ๊ธฐ์ ์ค์ํ๊ฑด variable ๋ก ๊ฐ ํฐํธ๋ณ๋ก ์ด๋ฆ์ ์ง์ด์ค๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// styles/font.ts
import { Noto_Sans_KR, Roboto, Source_Sans_3 } from "next/font/google"
import localFont from "next/font/local"
// ๋ณ์ ํฐํธ ์ ์
const noto = Noto_Sans_KR({
variable: "--font-noto",
subsets: ["latin"],
})
const roboto = Roboto({
weight: "400",
subsets: ["latin"],
variable: "--font-roboto",
})
// ํน์ weight์ ๊ฐ์ง ํฐํธ ์ ์
const sourceCodePro400 = Source_Sans_3({
weight: "400",
variable: "--font-source-400",
subsets: ["latin"],
})
const sourceCodePro700 = Source_Sans_3({
weight: "700",
variable: "--font-source-700",
subsets: ["latin"],
})
// ๋ก์ปฌ ํฐํธ ์ ์
const pretendard = localFont({
src: "../app/font/Pretendard-Regular.otf",
variable: "--font-pretendard",
})
export { noto, roboto, sourceCodePro400, sourceCodePro700, pretendard }
๊ทธ ๋ค์ ์ต์์ layout.tsx์ className์ ์์์ return ํด์ค ๊ฐ๋ค์ ๋ฃ์ด์ฃผ์๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// app/layout.tsx
import "./globals.css"
import Provider from "./Provider"
import Header from "@/components/Header"
import { Noto_Sans_KR } from "next/font/google"
import {
noto,
roboto,
sourceCodePro400,
sourceCodePro700,
pretendard,
} from "@/styles/font"
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html
lang="ko"
className={`${noto.variable} ${roboto.variable} ${sourceCodePro400.variable} ${sourceCodePro700.variable} ${pretendard.variable}`}
>
<body>
<Provider>
<Header />
{children}
</Provider>
</body>
</html>
)
}
๋ง์ง๋ง์ผ๋ก tailwind.config.ts
์ ์์ฑํด์ฃผ์. ์ฐธ๊ณ ๋ก ํด๋น ๋ด์ฉ์ tailwind 3๋ฒ์ ๊ธฐ์ค์ผ๋ก ์์ฑ๋์๋๋ฐ, ํ๋์ 3๋ฒ์ ์ ์ฌ์ฉํ ์์ ์ด๊ธด ํ์ง๋ง, 4๋ฒ์ ๋์
์ ์ด๋ป๊ฒ ์์ฑํ๋ฉด ์ข์์ง๋ 4๋ฒ์ ์ ์ฌ์ฉํ๋ ๋ ํฌ์งํ ๋ฆฌ์์ ํ์ธํด๋ณด๋๊ฒ ์ข์ ๋ฏ ํ๋ค.๐ค
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./pages/**/*.{js,ts,jsx,tsx,mdx}",
"./components/**/*.{js,ts,jsx,tsx,mdx}",
"./app/**/*.{js,ts,jsx,tsx,mdx}",
"./app/(root)/**/*.{js,ts,jsx,tsx,mdx}",
],
theme: {
extend: {
fontFamily: {
noto: ["var(--font-noto)"],
roboto: ["var(--font-roboto)"],
sourceCodePro: ["var(--font-source-400)"],
sourceCodeProBold: ["var(--font-source-700)"],
pretendard: ["var(--font-pretendard)"],
},
},
},
plugins: [],
}
์ค์ ์ฌ์ฉ์ ๋ค์๊ณผ ๊ฐ์ด ์์์ ์ ์ํด์ค ์ด๋ฆ์ ๋ฐํ์ผ๋ก font-์ด๋ฆ
์ผ๋ก ์์ฑํด์ฃผ๋ฉด ๋๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
export default function Page() {
return (
<>
<p className="font-noto">
ํฐํธ๊ฐ ์ ์ฉ๋์๋์ง ์ดํด๋ณผ๊น์?
<br />
Let's check if the font has been applied properly!
</p>
<p className="font-roboto">
ํฐํธ๊ฐ ์ ์ฉ๋์๋์ง ์ดํด๋ณผ๊น์?
<br />
Let's check if the font has been applied properly!
</p>
</>
)
}
๐นCSS์ ํจ๊ป ์ฌ์ฉํ๊ธฐ
๋ง์ฝ Tailiwnd๊ฐ ์๋๋ผ CSSํ์ผ์์ ์ฌ์ฉํด์ผ ํ๋ค๋ฉด, styles/font.ts
์ app/layout.tsx
๊น์ง๋ ์์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์์ฑํ๊ณ , global.css์ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ๋ค.
1
2
3
4
5
6
7
html {
font-family: var(--font-noto);
}
h1 {
font-family: var(--font-roboto);
}
๐Font Module
Next js์ API Reference๋ฅผ ํตํด ์ด๋ป๊ฒ next/font/google
๊ณผ next/font/local
์ ์ฌ์ฉํ๋์ง ์์ธํ ์์๋ณด์.
Key | font/google | font/local | Type | Required |
---|---|---|---|---|
src | โ | โ | ๋ฌธ์์ด or ๊ฐ์ฒด ๋ฐฐ์ด | ํ์ |
weight | โ | โ | ๋ฌธ์์ด or ๋ฐฐ์ด | ํ์/์ ํ |
style | โ | โ | ๋ฌธ์์ด or ๋ฐฐ์ด | - |
subsets | โ | โ | ๋ฌธ์์ด ๋ฐฐ์ด | - |
axes | โ | โ | ๋ฌธ์์ด ๋ฐฐ์ด | - |
display | โ | โ | ๋ฌธ์์ด | - |
preload | โ | โ | ๋ถ๋ฆฌ์ธ | - |
fallback | โ | โ | ๋ฌธ์์ด ๋ฐฐ์ด | - |
adjustFontFallback | โ | โ | ๋ถ๋ฆฌ์ธ or ๋ฌธ์์ด | - |
variable | โ | โ | ๋ฌธ์์ด | - |
declarations | โ | โ | ๊ฐ์ฒด ๋ฐฐ์ด | - |
๐src
ํฐํธ ํ์ผ์ ๊ฒฝ๋ก๋ฅผ ๋ฌธ์์ด ๋๋ ๊ฐ์ฒด ๋ฐฐ์ด๋ก ์ง์ ํ๋ค.
๐weight
ํฐํธ์ ๋๊ป๋ฅผ ์ง์ ํ๋ค.
๐style
๐subsets
subsets: ์ ์ฒด ๊ธ๊ผด ํ์ผ์์ ํน์ ๋ฌธ์๋ ๊ธฐํธ๋ง์ ํฌํจํ๋ ๊ฒฝ๋ํ๋ ํฐํธ ํ์ผ
next/font/google
๋ฅผ ์ฌ์ฉํ ๋, subsets
์ต์
์ ํตํด ๋ฏธ๋ฆฌ ๋ก๋ํ๊ณ ์ถ์ ๊ธ๊ผด ์๋ธ์
์ ์ ์ํ ์ ์๋ค. preload ์ต์
์ด true๋ก ์ค์ ๋์ด ์๋ค๋ฉด(default๊ฐ์ true), ์ด ์๋ธ์
์ <link rel="preload"
> ํ๊ทธ๊ฐ <head>
์ ์๋์ผ๋ก ์ฝ์
๋๋ค!
ํฐํธ์ ๋ฐ๋ผ ์ง์๋๋ subset์ด ๋ฌ๋ผ์ง๊ธฐ ๋๋ฌธ์, Google Fonts์ ์ฌ์ฉ ์ค์ธ ๊ธ๊ผด ํ์ด์ง์์ ํ์ธํ ์ ์์ผ๋ฉฐ, ๋ชจ๋ ๊ธ๊ผด์ด ํฌํจ๋ ํ์ผ์ด ์๋๋ผ, ํ์ํ ์๋ธ์ ๋ง ๋ก๋ํด ํฐํธ ์ต์ ํ๊ฐ ๊ฐ๋ฅํ๋ค.
์์ฌ์ด ์ ์ ํ๊ธ ์๋ธ์ ์ ์๋ ๊ฒ์ผ๋ก ๋ณด์ธ๋ค๋ ๊ฒ.. ๐
Noto_Sans_KR์ด ์ง์ํ๋ ์๋ธ์ ๋ชฉ๋ก์ ๋ค์๊ณผ ๊ฐ๋ค.
Available subsets:
cyrillic
,latin
,latin-ext
,vietnamese
์๋ธ์ ์ข ๋ฅ | ์ค๋ช |
---|---|
cyrillic | ๋ฌ์์์ด, ์ฐํฌ๋ผ์ด๋์ด, ๋ถ๊ฐ๋ฆฌ์ด ๋ฑโ |
latin | ์์ด, ํ๋์ค์ด, ๋ ์ผ์ด, ์คํ์ธ์ด ๋ฑ |
latin-ext | latin์ ํ์ฅ ๋ฒ์ , ์ฃผ๋ก ๋์ ๋ฝ ์ธ์ด๋ก ํด๋๋์ด, ์ฒดํฌ์ด, ํ๊ฐ๋ฆฌ์ด ๋ฑ |
vietnamese | ๋ฒ ํธ๋จ์ด |
๐axes
๐display
๊ธ๊ผด์ ๋ก๋ํ ๋ ๋ธ๋ผ์ฐ์ ๊ฐ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ์ง๋ฅผ ์ ํด์ค ์ ์๋ค.
์ ๋ต | ์ฐจ๋จ ๊ธฐ๊ฐ (Blocking Period) | ๊ต์ฒด ๊ธฐ๊ฐ (Swap Period) | ์ค๋ช |
---|---|---|---|
auto | ๋ธ๋ผ์ฐ์ ๊ธฐ๋ณธ ์ค์ ์ ๋ฐ๋ฆ | ๋ธ๋ผ์ฐ์ ๊ธฐ๋ณธ ์ค์ ์ ๋ฐ๋ฆ | ๋ธ๋ผ์ฐ์ ์ ๊ธฐ๋ณธ ๋์์ ๋ฐ๋ฆ |
block | ๊ธด ์ฐจ๋จ ๊ธฐ๊ฐ | ๋ฌด์ ํ ๊ต์ฒด | ๊ธ๊ผด์ด ๋ก๋๋ ๋๊น์ง ํ ์คํธ๋ฅผ ํ์ํ์ง ์์. ๋ก๋๋ ํ ๊ธ๊ผด ๊ต์ฒด๋จ |
swap | ๋งค์ฐ ์งง์ ์ฐจ๋จ ๊ธฐ๊ฐ | ๋ฌด์ ํ ๊ต์ฒด | ํ ์คํธ๋ ๊ธฐ๋ณธ ๊ธ๊ผด๋ก ํ์๋๋ฉฐ, ๊ธ๊ผด์ด ๋ก๋๋๋ฉด ๋น ๋ฅด๊ฒ ๊ต์ฒด |
fallback | ๋งค์ฐ ์งง์ ์ฐจ๋จ ๊ธฐ๊ฐ | ์งง์ ๊ต์ฒด ๊ธฐ๊ฐ | ํ ์คํธ๋ ๊ธฐ๋ณธ ๊ธ๊ผด๋ก ํ์๋๋ฉฐ, ๊ธ๊ผด์ด ๋ก๋๋๋ฉด ๋น ๋ฅด๊ฒ ๊ต์ฒด |
optional | ๋งค์ฐ ์งง์ ์ฐจ๋จ ๊ธฐ๊ฐ | ์งง์ ๊ต์ฒด ๊ธฐ๊ฐ | ๊ธ๊ผด์ด ๋ก๋๋์ง ์์ผ๋ฉด ๊ธฐ๋ณธ ๊ธ๊ผด๋ก ๊ณ์ ํ์๋จ (์ฑ๋ฅ ์ต์ ํ) |
๐preload
๐fallback
๐adjustFontFallback
๐variable
๐declarations
๐ํฐํธ ๊ฒฝ๋ก ํ์ผ ์ฌ์ฉํ๊ธฐ
localFont๋ googleFont๋ฅผ ์ฌ์ฉํ ๋ ๊ทธ ํฐํธ๋ ํ๋์ ์ธ์คํด์ค๋ก ํธ์ถ๋๋ค! ๋ฐ๋ผ์, ๋งค๋ฒ ํฐํธ๋ฅผ ์์ฒญํ๋ ๊ฒ์ด ์๋๋ผ, ํ ํ์ผ์ ๋ค์ํ ํฐํธ๋ฅผ ์์ฒญํ๊ณ , ๊ทธ ๋ชจ์๋์ ํ์ผ์ importํด์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค.