next.js์์๋ next/image๋ก ์์ฒด์ ์ธ ์ด๋ฏธ์ง ์ปดํฌ๋ํธ๋ฅผ ์ ๊ณตํ๋ค. ์ผ๋ฐ์ ์ธ img ํ๊ทธ์๋ ๋ฌ๋ฆฌ ์ฌ๋ฌ ์ฑ๋ฅ ์ต์ ํ๊ธฐ๋ฒ์ด ๋ด์ฅ๋์ด์๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ๋ค์๊ณผ ๊ฐ์ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
- ์ฑ๋ฅ ํฅ์ : ๊ฐ ๋๋ฐ์ด์ค์ ์ ํํ ๋ง๋ ์ด๋ฏธ์ง๋ฅผ ๋ชจ๋ ์น ํฌ๋งท์ ๋ง๊ฒ ์ ๊ณตํ๋ค.
- Visual Stability : Cumulative Layout Shift๋ฅผ ์๋์ผ๋ก ์๋ฐฉํ๋ค.
- Cumulative Layout Shift : ์น ํ๋ฉด์ ๋ ์ด์์์ด ๊ฐ์๊ธฐ ๋ฐ๋๋ ๊ฒ. ์ฌ์ฉ์๊ฐ ์๋์น์์ ๋์์ ์คํํ ์ ์๋ค.
- Faster Page Loads : ๋ทฐํฌํธ ์์ ๋ค์ด์์ผ๋ง ์ด๋ฏธ์ง๊ฐ ๋ก๋๋๊ฒ ํ๋ค. ์ ํ์ ์ผ๋ก ๋ธ๋ฌ์ฒ๋ฆฌ๋ placeholder๋ฅผ ๋ ์ ์๋ค.
- Asset Flexibility : ์ด๋ฏธ์ง๊ฐ ๋ฆฌ๋ชจํธ ์๋ฒ์ ์ ์ฅ๋์ด์์ด๋, on-demand image resizing์ ์ง์ํ๋ค.
next/image ์ปดํฌ๋ํธ ์ฌ์ฉ๋ฒ
์ปดํฌ๋ํธ import
import Image from "next/image"image ๋ถ๋ฌ์ค๊ธฐ
Local Image
import profilePic from '../public/me.png'
function Home() {
return (
<Image
src={profilePic}
alt="Picture of the author"
// width={500} automatically provided
// height={500} automatically provided
// blurDataURL="data:..." automatically provided
// placeholder="blur" // Optional blur-up while loading
/>
)
}import๋ฌธ์ด ๋น๋์ ๋ถ์๋์ด์ผํ๊ธฐ ๋๋ฌธ์ Dynamicawait import()๋๋require()๋ ์ง์๋์ง ์๋๋ค.width,height๋ next.js์์ ์๋์ผ๋ก ๊ณ์ฐํ๋ค. ์ด๋ Cumulative Layout Shift๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด ์ฐ์ธ๋ค.blurDataURL๋ ์๋์ผ๋ก ์ง์๋๋ค.
Remote image
import Image from 'next/image'
export default function Home() {
return (
<Image
src="/me.png"
alt="Picture of the author"
width={500}
height={500}
/>
)
}- ์ ๋์ฃผ์ ๋๋ ์๋์ฃผ์๋ก ์ด๋ฏธ์ง์ ์์น๋ฅผ ๋ช
์ํ๋ค. ์ด๋ ์๋์ฃผ์๋
publicํด๋ ์ดํ์ ๊ฒฝ๋ก์ด๋ค. - ๋น๋์ remote image์ ์ ๊ทผํ์ง ์๊ธฐ์,
width,height๋ฅผ ๋ช ์ํด์ฃผ์ด์ผํ๋ค. blurDataURL๋ํ ์ง์ ์ ๋ ฅํด์ผํ๋ค.
Loaders
Remote image๋ฅผ ๋ถ๋ฌ์ค๋ ์์ ์์ src ๊ฐ์ /me.png์ ๊ฐ์ ์๋๊ฒฝ๋ก๋ก ์
๋ ฅํ์๋๋ฐ, ์ด๊ฒ์ด ๊ฐ๋ฅํ ์ด์ ๋ Loader๊ฐ ์๊ธฐ ๋๋ฌธ์ด๋ค.
loader๋ ์ด๋ฏธ์ง์ URL์ ์์ฑํ๋ ํจ์์ด๋ค. src๋ฅผ ๋ณ๊ฒฝํ์ฌ ๋ค์ํ ์ฌ์ด์ฆ์ ์ด๋ฏธ์ง๋ฅผ ์์ฒญํ๋ ์ฌ๋ฌ URL์ ์์ฑํ๋ค. ์ด๋ฌํ ์ฌ๋ฌ URL์ ์๋์ผ๋ก srcset์ ์์ฑํ๊ณ , ์ฌ์ดํธ์ ๋ฐฉ๋ฌธํ ์ ์ ๊ฐ ์ ์ ํ ์ฌ์ด์ฆ์ ์ด๋ฏธ์ง๋ฅผ ๋ค์ด ๋ฐ๋๋กํ๋ค.
default loader๋ ์ด๋ฏธ์ง๋ฅผ ๊ฐ์ ธ์ ์ต์ ํํ ๋ค์์ next.js ์น ์๋ฒ์์ ์ ๊ณตํ๋ค. ๋ง์ฝ CDN์ด๋ image server์์ ์ง์ ์ด๋ฏธ์ง๋ฅผ ๊ฐ์ ธ๊ฐ๋๋ก ํ๊ณ ์ถ๋ค๋ฉด, ์ง์ loader ํจ์๋ฅผ ์์ฑํ ์ ์๋ค. next/image์ปดํฌ๋ํธ๋ง๋ค loader ์์ฑ์ ์ง์ ํ ์ ์์ผ๋ฉฐ, ์๋๋ฉด ์ดํ๋ฆฌ์ผ์ด์
๋ ๋ฒจ๋ก next.config.js์์ loader๋ฅผ ์ง์ ํ ์ ์๋ค.
Domains
remote image๋ฅผ ์ต์ ํํ๊ณ ์ถ์ ๋, loader ๋ ๋ํดํธ ์ค์ ์ผ๋ก ๋๊ณ , next/image ์ปดํฌ๋ํธ์ src ์๋ ์ ๋๊ฒฝ๋ก๋ฅผ ์
๋ ฅํ๋ค. ์
์์ ์ธ ๊ณต๊ฒฉ์ ๋ง๊ธฐ ์ํด remote hostname์ ๋ฐ๋์ ์ ์ํด์ผํ๋ค. ๋ค์๊ณผ ๊ฐ์ด next.config.js์์ ์ ์ํ ์ ์๋ค.
module.exports = {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 'example.com',
port: '',
pathname: '/account123/**',
},
{
protocol: 'https',
hostname: '**.example.com',
},
],
},
}- ์์ผ๋์นด๋
*๋hostname๊ณผpathname์ ๋ชจ๋ ์ฐ์ผ ์ ์๋ค.*: ํ๋์ path segment ๋๋ subdomain๊ณผ ๋งค์นญ๋๋ค.**; ์ฌ๋ฌ path segment ๋๋ subdomain๊ณผ ๋งค์นญ๋๋ค.www.**.com๊ณผ ๊ฐ์ด ์ค๊ฐ ๋ถ๋ถ์์ ์ ์ฉ๋์ง ์๋๋ค.
์ข ๋ ๊ฐ๋จํ๊ฒ domain๋ง ๋ช
์ํ ์๋ ์๋ค. ํ์ง๋ง domain์ ์์ผ๋์นด๋๋ฅผ ์ง์ํ์ง ์๊ณ , protocol์ ์ง์ ํ ์ ์๊ธฐ์ remotePatterns๊ฐ ๊ถ์ฅ๋๋ค.
module.exports = {
images: {
domains: ['assets.acme.com'],
},
}remotePatterns์ ๊ดํ ์์ธํ ๋ด์ฉ
Priority
priority์์ฑ์ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง ๋ก๋ฉ์ ์ฐ์ ์์๋ฅผ ๋ ์ ์๋ค. LCP ์๋ฆฌ๋จผํธ์ ์ฐ์ผ ์ ์์ผ๋ฉฐ, ์ด๋ฏธ์ง๊ฐ ํ์ด์ง์ ์ ์ํ์๋ง์ ๋ณด์ฌ์ผํ ๋๋ง ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค,
Image Sizing
์ด๋ฏธ์ง๊ฐ ํผํฌ๋จผ์ค์ ์ ์ํฅ์ ๋ฏธ์น๋ ๊ฐ์ฅ ํฐ ์์ธ์ cumulative layout shift์ด๋ค . ์ด๋ฏธ์ง๊ฐ ๋ฆ๊ฒ ๋ก๋ฉ๋์ด ๋ค๋ฅธ ์๋ฆฌ๋จผํธ๋ฅผ ๋ฐ์ด๋ด๋ ๊ฒ์ ๋งํ๋ค. ์ ์ ์ ์ ์ฅ์์ ๋งค์ฐ ์ง์ฆ๋๋ ์ด์์ด๋ค.
์ด ์ด์๋ฅผ ๋ง๊ธฐ ์ํด์๋ ์ด๋ฏธ์ง์ ์ฌ์ด์ฆ๋ฅผ ๊ธฐ์ ํด์ผํ๋ค. ์ด๋ฏธ์ง์ ์ฌ์ด์ฆ๋ฅผ ๊ธฐ์ ํ๋ฉด, ๋ธ๋ผ์ฐ์ ๋ ์ด๋ฏธ์ง๊ฐ ๋ก๋๋๊ธฐ ์ ์ ์ด๋ฏธ์ง๋ฅผ ์ํ ๊ณต๊ฐ์ ๋ฏธ๋ฆฌ ์ก์๋๋ค.
next/image์์๋ ๋ค์ ์ธ๊ฐ์ง ๋ฐฉ๋ฒ์ค ํ๋๋ฅผ ์ฌ์ฉํ๋ค.
-
static import๋ฅผ ํตํด ๋ก์ปฌ์์ ์ด๋ฏธ์ง๋ฅผ ๊ฐ์ ธ์ค๋ฉด ์๋์ผ๋ก ๊ณ์ฐํ๋ค. -
๋ช ์์ ์ผ๋ก
width,height์์ฑ์ ๊ธฐ์ ํ๋ค. -
fill์์ฑ์ ๋ถ๋ชจ ์์๋ฅผ ์ฑ์ธ ๋ ์ฌ์ฉํ๋ฉฐ, ์ด ์์ฑ์ ์ฌ์ฉํ๋ฉด,width,height๋ฅผ ๊ธฐ์ ํ์ง ์์๋ ์๋์ผ๋ก ๊ณ์ฐ๋๋ค. ๋ค๋ง ๋ถ๋ชจ ์์์ position์ดrelative,absolute,fixed์ค ํ๋์ฌ์ผํ๋ค.
Styling
๊ธฐ๋ณธ์ ์ผ๋ก๋ <img> ํ๊ทธ๋ฅผ ์ฌ์ฉํ ๋์ ๊ฐ์ง๋ง ์๋ ์ฃผ์์ฌํญ์ด ์๋ค.
๊ธ๋ก๋ฒ์ด ์๋ styled-jsx ๋์ ์ className ๋๋ style์ ์ฌ์ฉํ๋ค.
styled-jsx๋ ๊ธ๋ก๋ฒ๋ก ์ฌ์ฉํ์ง ์๋ํ ํ์ฌ ์ปดํฌ๋ํธ์์๋ง ์ ์ฉ๋๊ธฐ์ ์ฌ์ฉํ ์ ์๋ค.
fill ์์ฑ ์ฌ์ฉ์, ๋ถ๋ชจ ์์๋ position:relative์ฌ์ผํ๋ค.
์ ์ ํ ์ฌ์ด์ฆ๋ฅผ ๊ณ์ฐํ๊ธฐ ์ํด ํ์ํ๋ค.
fill ์์ฑ ์ฌ์ฉ์, ๋ถ๋ชจ์์๋ display: block์ฌ์ผํ๋ค.
<div> ์๋ฆฌ๋จผํธ์ผ ๊ฒฝ์ฐ์ ์ด๋ฏธ ์ ์ฉ๋์ด์์ง๋ง, ๋ค๋ฅธ ์๋ฆฌ๋จผํธ๋ผ๋ฉด ๋ช
์์ ์ผ๋ก ์ค์ ํด์ผํ๋ค.
์ค์ฌ์ฉ
๋ณดํต Remote ์์ ์ด๋ฏธ์ง๋ฅผ ๊ฐ์ ธ์ค๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ผ๋ฏ๋ก Remote ๊ธฐ์ค์ผ๋ก ์คํํจ
- width, height ๊ฐ ์ด๋ฏธ์ง ํฌ๊ธฐ์ ์ํฅ์ ์ฃผ๋ : css ์ค์ ์ด ๋์ด์๋ค๋ฉด css๊ฐ ์ฐ์ ์ผ๋ก ์ ์ฉ๋จ. ํฌ๊ฒ ๊ฑฑ์ ํ์ง ์์๋ ๋ ๋ฏ
- lazy ๋ก๋ฉ ์ ์ฉ๋จ (img ํ๊ทธ์
loading="lazy") - ์ฃผ์๊ฐ next.js ์๋ฒ ์ฃผ์๋ก ๋ณ๊ฒฝ๋จ. ์ด๋ฏธ์ง๋ฅผ ๊ฐ์ ธ์์ next.js ์๋ฒ์์ webp ๋ฑ์ผ๋ก ๊ฐ๊ณตํ ํ ๋ด๋ณด๋
- โRemote โ CDNโ vs โRemote โ next.js ์๋ฒโ ์ค ์ด๋๊ฒ ๋น ๋ฅผ์ง๋ฅผ ํ์ธํด๋ด์ผํ ๋ฏ. ํ์์ ๊ฒฝ์ฐ webp๋ก ๊ฐ๊ณตํ๊ธฐ์ ์ ๋์ ์ธ ์ด๋ฏธ์ง ์ฉ๋์ด ์ ์ง๋ง ํ์ธ์ ํ์
- ๊ฐ๊ณตํ ์ด๋ฏธ์ง๋
/.next/cache/images์๋์ ์ ์ฅ๋์ด ์ฌ์ฌ์ฉํ๋ค. vercel ์ ๋ฐฐํฌํ ๊ฒฝ์ฐ ์ต๋ 31์ผ๊น์ง ์ ์ฅํด๋ .
์ถ์ฒ
next/image ๊ฐ์ด๋ : https://nextjs.org/docs/basic-features/image-optimization
next/image ๋ ํผ๋ฐ์ค : https://nextjs.org/docs/api-reference/next/image
https://oliveyoung.tech/2023-06-09/nextjs-image-optimization/ https://kyoung-jnn.com/posts/nextjs-image-optimization