๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Next.js

[๋‚ด๋ณด๋‚ด๋ฒˆ] Next.js docs - next/image (v12.1.1)

๋‚ด๋ณด๋‚ด๋ฒˆ(๋‚ด๊ฐ€ ๋ณด๋ ค๊ณ  ๋‚ด๊ฐ€ ๋ฒˆ์—ญํ•œ...) Next.js docs

2022๋…„ 4์›” 15์ผ ๊ธฐ์ค€ Next.js ๊ณต์‹ ๋ฌธ์„œ๋กœ ์—…๋ฐ์ดํŠธํ–ˆ๋‹ค. (v12.1.1)

์ด์ „ ๋ฒ„์ „ ๋ฌธ์„œ๋Š” ์•„๋ž˜์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 
2021.04.01 - [Next.js] - [๋‚ด๋ณด๋‚ด๋ฒˆ] Next.js docs - next/image

โ€ป ์˜์–ด ์ „๊ณต์ž๋„ ํ•ด์™ธ ์œ ํ•™ํŒŒ๋„ ์•„๋‹ˆ๊ธฐ์— ๋ฒˆ์—ญ์—๋Š” ์˜์—ญ, ์˜ค์—ญ, ๊ตฌ๊ธ€ ๋ฒˆ์—ญ์ด ๋ฌด์ˆ˜ํžˆ ๋งŽ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ˜ผ์ž ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•ด๊ฐ€๋ฉฐ ๋ฒˆ์—ญํ•˜๋‹ค ๋ณด๋‹ˆ ์˜คํƒ€๋„ ๋งŽ์„ ์ˆ˜ ์žˆ๋‹ค. ์ •ํ™•ํ•œ ๋‚ด์šฉ์€ ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ง์ ‘ ์‚ดํŽด๋ณด๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ์ •๋ณด๋“ค์„ ๋” ์ฐพ์•„๋ณด๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค.

(ํ•˜์ง€๋งŒ ๋Œ“๊ธ€ ํ”ผ๋“œ๋ฐฑ๋„ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜ƒ )

Next.js ๊ณต์‹๋ฌธ์„œ ํ™•์ธํ•˜๊ธฐ>>

 

next/image | Next.js

Enable Image Optimization with the built-in Image component.

nextjs.org


Note: This is API documentation for the Image Component and Image Optimization. For a feature overview and usage information for images in Next.js, please see Images.
๋…ธํŠธ: ์ด API ๋ฌธ์„œ๋Š” ์ด๋ฏธ์ง€ ์ปดํฌ๋„ŒํŠธ์™€ ์ด๋ฏธ์ง€ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค. Next.js์˜ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ๊ธฐ๋Šฅ ๊ฐœ์š”์™€ ์‚ฌ์šฉ๋ฒ•์— ๋Œ€ํ•œ ์ •๋ณด๋Š” Images ๋ฌธ์„œ๋ฅผ ์‚ดํŽด๋ณด๋ผ. 

2021.03.31 - [Next.js] - [๋‚ด๋ณด๋‚ด๋ฒˆ] Next.js docs - Image Component and Image Optimization

 

[๋‚ด๋ณด๋‚ด๋ฒˆ] Next.js docs - Image Component and Image Optimization

๋‚ด๋ณด๋‚ด๋ฒˆ( ๋‚ด๊ฐ€ ๋ณด๋ ค๊ณ  ๋‚ด๊ฐ€ ๋ฒˆ์—ญํ•œ... ) Next.js docs 2021๋…„ 3์›” 31์ผ ๊ธฐ์ค€ Next.js ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ๋ฒˆ์—ญํ–ˆ๋‹ค. โ€ป ์˜์–ด ์ „๊ณต์ž๋„ ํ•ด์™ธ ์œ ํ•™ํŒŒ๋„ ์•„๋‹ˆ๊ธฐ์— ๋ฒˆ์—ญ์—๋Š” ์˜์—ญ, ์˜ค์—ญ, ๊ตฌ๊ธ€ ๋ฒˆ์—ญ์ด ๋ฌด์ˆ˜ํžˆ ๋งŽ์„ ์ˆ˜

birdmee.tistory.com

 

Required Props 

The <Image /> component requires the following properties.

<Image /> ์ปดํฌ๋„ŒํŠธ๋Š” ์•„๋ž˜์˜ ์†์„ฑ๋“ค์ด ์š”๊ตฌ๋œ๋‹ค: 

src

Must be one of the following:

  1. statically imported image file, or
  2. A path string. This can be either an absolute external URL, or an internal path depending on the loader prop or loader configuration.

When using an external URL, you must add it to domains in next.config.js.

์•„๋ž˜์˜ ์‚ฌํ•ญ ์ค‘ ํ•œ ๊ฐ€์ง€๋Š” ๋ฐ˜๋“œ์‹œ ์š”๊ตฌ๋œ๋‹ค:

1. ์ •์ ์œผ๋กœ ๊ฐ€์ ธ์˜จ ์ด๋ฏธ์ง€ ํŒŒ์ผ์ด๋‚˜
2. ๊ฒฝ๋กœ ์ŠคํŠธ๋ง. ์ด๊ฒƒ์€ ์™ธ๋ถ€ ์ ˆ๋Œ€ URL์ด๊ฑฐ๋‚˜ loader prop์ด๋‚˜ loader ์„ค์ •์— ๋”ฐ๋ฅธ ๋‚ด๋ถ€ ๊ฒฝ๋กœ์ผ ์ˆ˜๋„ ์žˆ๋‹ค. 

์™ธ๋ถ€ URL์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ next.config.js์˜ domains์— ์ถ”๊ฐ€ํ•ด์•ผ๋งŒ ํ•œ๋‹ค.

width

The `width` property can represent either the rendered width or original width in pixels, depending on the `layout` and `sizes` properties.

When using `layout="intrinsic"`, `layout="fixed"`, or `layout="raw"` without `sizes`, the `width` property represents the rendered width in pixels, so it will affect how large the image appears.

When using `layout="responsive"`, `layout="fill"`, or `layout="raw"` with `sizes`, the `width` property represents the original width in pixels, so it will only affect the aspect ratio.

The `width` property is required, except for statically imported images, or those with layout="fill".

`width` ์†์„ฑ์€ `layout` ์ด๋‚˜ `sizes` ์†์„ฑ์— ๋”ฐ๋ผ ํ”ฝ์…€๋กœ ๋ Œ๋”๋œ ๋„ˆ๋น„ ํ˜น์€ ์›๋ณธ์˜ ๋„ˆ๋น„๋ฅผ ํ”ฝ์…€๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. 

`sizes` ์†์„ฑ์ด ์—†์ด `layout="intrinsic"`, `layout="fixed"` ํ˜น์€ `layout="raw"`๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ `width` ์†์„ฑ์€ ๋ Œ๋”๋œ ๋„ˆ๋น„๋ฅผ ํ”ฝ์…€๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฏธ์ง€๊ฐ€ ์–ผ๋งˆ๋‚˜ ํฌ๊ฒŒ ๋ณด์—ฌ์ง€๋Š”์ง€์— ์˜ํ–ฅ์„ ๋ฏธ์น  ๊ฒƒ์ด๋‹ค. 

`sizes` ์†์„ฑ๊ณผ ํ•จ๊ป˜ `layout="responsive"`, `layout="fill"` ํ˜น์€ `layout="raw"`๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ `width` ์†์„ฑ์€ ์›๋ณธ์˜ ๋„ˆ๋น„๋ฅผ ํ”ฝ์…€๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฏธ์ง€์˜ ๋น„์œจ์—๋งŒ ์˜ํ–ฅ์„ ๋ฏธ์น  ๊ฒƒ์ด๋‹ค. 

์ •์ ์œผ๋กœ ๊ฐ€์ ธ์˜ค๋Š” ์ด๋ฏธ์ง€๋‚˜ layout="fill"์ด ์•„๋‹Œ ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๊ณ  `width` ์†์„ฑ์ด ์š”๊ตฌ๋œ๋‹ค. 

height

The height property can represent either the rendered height or original height in pixels, depending on the layout and sizes properties.

When using layout="intrinsic", layout="fixed", or layout="raw" without sizes, the height property represents the rendered height in pixels, so it will affect how large the image appears.

When using layout="responsive", layout="fill", or layout="raw" with sizes, the height property represents the original height in pixels, so it will only affect the aspect ratio.

The height property is required, except for statically imported images, or those with layout="fill".

`height` ์†์„ฑ์€ `layout` ์ด๋‚˜ `sizes` ์†์„ฑ์— ๋”ฐ๋ผ ํ”ฝ์…€๋กœ ๋ Œ๋”๋œ ๋„ˆ๋น„ ํ˜น์€ ์›๋ณธ์˜ ๋„ˆ๋น„๋ฅผ ํ”ฝ์…€๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. 

`sizes` ์†์„ฑ์ด ์—†์ด `layout="intrinsic"`, `layout="fixed"` ํ˜น์€ `layout="raw"`๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ `height` ์†์„ฑ์€ ๋ Œ๋”๋œ ๋†’์ด๋ฅผ ํ”ฝ์…€๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฏธ์ง€๊ฐ€ ์–ผ๋งˆ๋‚˜ ํฌ๊ฒŒ ๋ณด์—ฌ์ง€๋Š”์ง€์— ์˜ํ–ฅ์„ ๋ฏธ์น  ๊ฒƒ์ด๋‹ค.

`sizes` ์†์„ฑ๊ณผ ํ•จ๊ป˜ `layout="responsive"`, `layout="fill"` ํ˜น์€ `layout="raw"`๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ `height` ์†์„ฑ์€ ์›๋ณธ์˜ ๋†’์ด๋ฅผ ํ”ฝ์…€๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฏธ์ง€์˜ ๋น„์œจ์—๋งŒ ์˜ํ–ฅ์„ ๋ฏธ์น  ๊ฒƒ์ด๋‹ค. 

์ •์ ์œผ๋กœ ๊ฐ€์ ธ์˜ค๋Š” ์ด๋ฏธ์ง€๋‚˜ layout="fill"์ด ์•„๋‹Œ ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๊ณ  `height` ์†์„ฑ์ด ์š”๊ตฌ๋œ๋‹ค. 

 

Optional Props

The <Image /> component accepts a number of additional properties beyond those which are required. This section describes the most commonly-used properties of the Image component. Find details about more rarely-used properties in the Advanced Props section.

<Image /> ์ปดํฌ๋„ŒํŠธ์— ํ•„์ˆ˜์ ์ธ ์†์„ฑ ์™ธ์—๋„ ๋งŽ์€ ์ถ”๊ฐ€ ์†์„ฑ๋“ค์„ ํ—ˆ์šฉํ•œ๋‹ค. ์ด ๋ถ€๋ถ„์€ ์ด๋ฏธ์ง€ ์ปดํฌ๋„ŒํŠธ์—์„œ ๊ฐ€์žฅ ํ”ํžˆ ์‚ฌ์šฉ๋˜๋Š” ์†์„ฑ๋“ค์— ๋Œ€ํ•ด ์„œ์ˆ ํ•œ๋‹ค. ํ”ํžˆ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ์†์„ฑ๋“ค์— ๋Œ€ํ•ด์„œ๋Š” Advanced Props ์„น์…˜์—์„œ ๋” ์ž์„ธํ•œ ์ ์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด๋ผ. 

layout

The layout behavior of the image as the viewport changes size. 

`layout` Behavior `srcSet` `sizes` Has wrapper and sizer
`instrinsic`
(default)
Scale down to fit width of container, up to image size `1x`, `2x` (based on imageSizes) N/A yes
`fixed` Sized to `width` and `height` exactly `1x`, `2x` (based on imageSizes) N/A yes
`responsive` Scale to fit width of container `640w`, `750w`, ... `2048w`, `3840w` (based on imageSizes and deviceSizes) `100vw` yes
`fill` Grow in both X and Y axes to fill container `640w`, `750w`, ... `2048w`, `3840w` (based on imageSizes and deviceSizes) `100vw` yes
`raw`* Insert the image element with no automatic layout behavior Behaves like responsive if the image has the sizes prop, and like fixed if it does not optional no
`layout` Behavior `srcSet` `sizes` Has wrapper and sizer
`instrinsic`
(๊ธฐ๋ณธ๊ฐ’)
์ปจํ…Œ์ด๋„ˆ์˜ ๋„ˆ๋น„์— ๋งž์ถ”๊ธฐ ์œ„ํ•ด ์ด๋ฏธ์ง€ ํฌ๊ธฐ ์ถ•์†Œ, ์ด๋ฏธ์ง€ ํฌ๊ธฐ์— ๋‹ฌ๋ ค์žˆ์Œ `1x`, `2x` (imageSizes์— ๋”ฐ๋ผ
*ํ•˜๋‹จ์˜ Image Sizes ์†Œ์ œ๋ชฉ ์ฐธ์กฐ)
N/A yes
`fixed` ์ •ํ™•ํ•œ `width`์™€ `height`์— ๋งž์ถค `1x`, `2x` (imageSizes์— ๋”ฐ๋ผ
*ํ•˜๋‹จ์˜ Image Sizes ์†Œ์ œ๋ชฉ ์ฐธ์กฐ)
N/A yes
`responsive` ์ปจํ…Œ์ด๋„ˆ์˜ ๋„ˆ๋น„์— ๋งž์ถฐ ํฌ๊ธฐ ์กฐ์ • `640w`, `750w`, ... `2048w`, `3840w` (imageSizes ์™€ deviceSizes์— ๋”ฐ๋ผ
*ํ•˜๋‹จ์˜ Image Sizes์™€ Device Sizes ์†Œ์ œ๋ชฉ ์ฐธ์กฐ)
`100vw` yes
`fill` ์ปจํ…Œ์ด๋„ˆ์— ์ฑ„์šฐ๊ธฐ ์œ„ํ•ด X, Y์ถ• ๋ชจ๋‘๋ฅผ ํ‚ค์›€ `640w`, `750w`, ... `2048w`, `3840w` (imageSizes ์™€ deviceSizes์— ๋”ฐ๋ผ
*ํ•˜๋‹จ์˜ Image Sizes์™€ Device Sizes ์†Œ์ œ๋ชฉ ์ฐธ์กฐ)
`100vw` yes
`raw`* ์ž๋™ ๋ ˆ์ด์•„์›ƒ ๋™์ž‘์—†์ด ์ด๋ฏธ์ง€ ์š”์†Œ ์ถ”๊ฐ€
์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ ๋ฐ˜์‘ํ˜•์œผ๋กœ ๋™์ž‘ํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ ๊ณ ์ •๋จ optional no
  • Demo the intrinsic layout (default)
    • When intrinsic, the image will scale the dimensions down for smaller viewports but maintain the original dimensions for larger viewports.
  • Demo the fixed layout
    • When fixed, the image dimensions will not change as the viewport changes (no responsiveness) similar to the native img element.
  • Demo the responsive layout
    • When responsive, the image will scale the dimensions down for smaller viewports and scale up for larger viewports.
    • Ensure the parent element uses display: block in their stylesheet.
  • Demo the fill layout
    • When fill, the image will stretch both width and height to the dimensions of the parent element, provided the parent element is relative.
    • This is usually paired with the objectFit property.
    • Ensure the parent element has position: relative in their stylesheet.

 

  • When raw*, the image will be rendered as a single image element with no wrappers, sizers or other responsive behavior.
    • If your image styling will change the size of a raw image, you should include the sizes property for proper image serving. Otherwise your image will receive a fixed height and width.
    • The other layout modes are optimized for performance and should cover nearly all use cases. It is recommended to try to use those modes before using raw.
  • Demo background image

 

 

๋ทฐํฌํŠธ์˜ ํฌ๊ธฐ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ ์ด๋ฏธ์ง€์˜ ๋ ˆ์ด์•„์›ƒ ๋™์ž‘์ด๋‹ค. (layout ์†์„ฑ์˜ ๋™์ž‘์„ ์˜๋ฏธํ•˜๋Š” ๋“ฏํ•จ. ) 

  • intrinsic ๋ ˆ์ด์•„์›ƒ ๋ฐ๋ชจ (๊ธฐ๋ณธ๊ฐ’)
    • ๊ฐ’์ด intrinsic์ธ ๊ฒฝ์šฐ, ์ด๋ฏธ์ง€๋Š” ์ž‘์€ ๋ทฐํฌํŠธ์—์„œ๋Š” ํฌ๊ธฐ๊ฐ€ ์ค„์–ด๋“ค์ง€๋งŒ ๋” ํฐ ๋ทฐํฌํŠธ์—์„œ๋Š” ๋ณธ๋ž˜์˜ ํฌ๊ธฐ๋ฅผ ์œ ์ง€ํ•  ๊ฒƒ์ด๋‹ค. 
  • fixed ๋ ˆ์ด์•„์›ƒ ๋ฐ๋ชจ
    • ๊ฐ’์ด fixed์ธ ๊ฒฝ์šฐ, native img ์š”์†Œ์™€ ๋น„์Šทํ•˜๊ฒŒ ๋ทฐํฌํŠธ์˜ ํฌ๊ธฐ๊ฐ€ ๋ณ€ํ•ด๋„(๋ฐ˜์‘ ์—†์Œ - ๋ฐ˜์‘ํ˜•์ด ์•„๋‹ˆ๋ผ๋Š” ์˜๋ฏธ ๊ฐ™์Œ) ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๊ฐ€ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค. 
  • responsive ๋ ˆ์ด์•„์›ƒ ๋ฐ๋ชจ
    • ๊ฐ’์ด responsive์ธ ๊ฒฝ์šฐ, ์ด๋ฏธ์ง€๊ฐ€ ์ž‘์€ ๋ทฐํฌํŠธ์—์„œ๋Š” ํฌ๊ธฐ๊ฐ€ ์ค„์–ด๋“ค๊ณ  ๋” ํฐ ๋ทฐํฌํŠธ์—์„œ๋Š” ํฌ๊ธฐ๊ฐ€ ์ปค์ง„๋‹ค. 
    • ๋ถ€๋ชจ ์š”์†Œ๊ฐ€ ์Šคํƒ€์ผ์‹œํŠธ์—์„œ `display: block`์„ ์‚ฌ์šฉํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋ผ.
  • fill ๋ ˆ์ด์•„์›ƒ ๋ฐ๋ชจ
    • ๊ฐ’์ด fill์ผ ๋•Œ, ๋ถ€๋ชจ ์š”์†Œ๊ฐ€ relative์ธ ๊ฒฝ์šฐ ๋ถ€๋ชจ ์š”์†Œ์˜ ํฌ๊ธฐ์— ๋งž์ถฐ ๋„ˆ๋น„์™€ ๋†’์ด๊ฐ€ ๋Š˜์–ด๋‚œ๋‹ค.
    • ๋ณดํ†ต์€ objectFit ์†์„ฑ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•œ๋‹ค.
    • ๋ถ€๋ชจ ์š”์†Œ๊ฐ€ ์Šคํƒ€์ผ์‹œํŠธ์—์„œ `display: relative`๋ฅผ ์‚ฌ์šฉํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋ผ.
  • raw ์ผ ๋•Œ, ์ด๋ฏธ์ง€๋Š” wrapper, sizer๋‚˜ ๋‹ค๋ฅธ ๋ฐ˜์‘ํ˜• ๋™์ž‘์—†์ด ๋‹จ์ผ ์ด๋ฏธ์ง€ ์š”์†Œ๋กœ ๋ Œ๋”๋ง ๋œ๋‹ค.
    • ์ด๋ฏธ์ง€์˜ ์Šคํƒ€์ผ์ด `raw` ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒฝ์šฐ, ์ ์ ˆํ•œ ์ด๋ฏธ์ง€ ์ œ๊ณต์„ ์œ„ํ•ด์„œ `sizes` ์†์„ฑ์„ ํฌํ•จ์‹œ์ผœ์•ผํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ด๋ฏธ์ง€์˜ ๋†’์ด์™€ ๋„ˆ๋น„๊ฐ€ ๊ณ ์ •๋œ๋‹ค.  
    • ๋‹ค๋ฅธ ๋ ˆ์ด์•„์›ƒ ๋ชจ๋“œ๋“ค์€ ์„ฑ๋Šฅ์„ ์œ„ํ•ด์„œ ์ตœ์ ํ™” ๋˜์–ด ์žˆ๊ณ  ๊ฑฐ์˜ ๋ชจ๋“  ๊ฒฝ์šฐ์— ์‚ฌ์šฉ๋˜์–ด์•ผํ•œ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— `raw`๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ๊ทธ๋Ÿฌํ•œ ๋ชจ๋“œ๋“ค์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค. 
  • ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ด๋ฏธ์ง€ ๋ฐ๋ชจ

 

loader

A custom function used to resolve URLs. Setting the loader as a prop on the Image component overrides the default loader defined in the images section of next.config.js.

A loader is a function returning a URL string for the image, given the following parameters:

Here is an example of using a custom loader with next/image:

URL์„ ํ™•์ธํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์‚ฌ์šฉ์ž ์ง€์ • ํ•จ์ˆ˜. ์ด๋ฏธ์ง€ ์ปดํฌ๋„ŒํŠธ์—์„œ loader๋ฅผ prop์œผ๋กœ ์„ค์ •ํ•˜๋ฉด next.config.js์˜ ์ด๋ฏธ์ง€ ์˜์—ญ์— ์ •์˜๋œ ๊ธฐ๋ณธ loader๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•œ๋‹ค. 

Loader๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ์ด๋ฏธ์ง€๋ฅผ ์œ„ํ•œ URL ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค: 

์•„๋ž˜๋Š” next/image์™€ ํ•จ๊ป˜ ์ปค์Šคํ…€ loader๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์ œ์ด๋‹ค: 

import Image from 'next/image'

const myLoader = ({ src, width, quality }) => {
  return `https://example.com/${src}?w=${width}&q=${quality || 75}`
}

const MyImage = (props) => {
  return (
    <Image
      loader={myLoader}
      src="me.png"
      alt="Picture of the author"
      width={500}
      height={500}
    />
  )
}

 

sizes

A string that provides information about how wide the image will be at different breakpoints. Defaults to 100vw (the full width of the screen) when using layout="responsive" or layout="fill".

If you are using layout="fill" or layout="responsive", it's important to assign sizes for any image that takes up less than the full viewport width.

For example, when the parent element will constrain the image to always be less than half the viewport width, use sizes="50vw". Without sizes, the image will be sent at twice the necessary resolution, decreasing performance.

If you are using layout="intrinsic" or layout="fixed", then sizes is not needed because the upperbound width is constrained already.

Learn more.

๋‹ค์–‘ํ•œ ์ค‘๋‹จ์ (breakpoints)์—์„œ ์ด๋ฏธ์ง€๊ฐ€ ์–ด๋Š ๋„ˆ๋น„๋ฅผ ๊ฐ€์งˆ ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ด์ฃผ๋Š” ๋ฌธ์ž์—ด์ด๋‹ค. layout="responsive"์™€ layout="fill"์„ ์‚ฌ์šฉํ•  ๋•Œ ๊ธฐ๋ณธ๊ฐ’์€ 100vw(ํ™”๋ฉด์˜ ์ „์ฒด ๋„ˆ๋น„)์ด๋‹ค.

layout="fill"์ด๋‚˜ layout="responsive"๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ, ์ „์ฒด ๋ทฐํฌํŠธ ๋„ˆ๋น„๋ณด๋‹ค ์ ๊ฒŒ ์ฐจ์ง€ํ•˜๋Š” ๋ชจ๋“  ์ด๋ฏธ์ง€์— ๋Œ€ํ•ด ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. 

์˜ˆ๋ฅผ ๋“ค๋ฉด, ๋ถ€๋ชจ ์š”์†Œ๊ฐ€ ์ด๋ฏธ์ง€๋ฅผ ํ•ญ์ƒ ๋ทฐํฌํŠธ ๋„ˆ๋น„์˜ ์ ˆ๋ฐ˜ ๋ฏธ๋งŒ์œผ๋กœ ์ œํ•œํ•œ๋‹ค๋ฉด sizes="50vw"๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. sizes๊ฐ€ ์—†์œผ๋ฉด ์ด๋ฏธ์ง€๊ฐ€ ํ•„์š”ํ•œ ํ•ด์ƒ๋„์˜ ๋‘ ๋ฐฐ๋กœ ์ „์†ก๋˜์–ด ์„ฑ๋Šฅ์ด ์ €ํ•˜๋œ๋‹ค. 

layout="intrinsic์ด๋‚˜ layout="fixed"๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ, ๋„ˆ๋น„์˜ ์ƒํ•œ์„ ์— ์ด๋ฏธ ์ œ์•ฝ์ด ๊ฑธ๋ ค์žˆ๊ธฐ ๋•Œ๋ฌธ์— sizes๋Š” ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค.

  

quality

The quality of the optimized image, an integer between 1 and 100 where 100 is the best quality. Defaults to 75.

์ตœ์ ํ™”๋œ ์ด๋ฏธ์ง€์˜ ํ’ˆ์งˆ๋กœ, 1์—์„œ 100 ์‚ฌ์ด์˜ ์ •์ˆ˜์ด๋ฉฐ 100์ด ์ตœ์ƒ์˜ ํ’ˆ์งˆ์ด๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ 75์ด๋‹ค. 

priority

When true, the image will be considered high priority and preload. Lazy loading is automatically disabled for images using priority.

You should use the priority property on any image detected as the Largest Contentful Paint (LCP) element. It may be appropriate to have multiple priority images, as different images may be the LCP element for different viewport sizes.

Should only be used when the image is visible above the fold. Defaults to false.

true์ธ ๊ฒฝ์šฐ ์ด๋ฏธ์ง€๊ฐ€ ๋†’์€ ์šฐ์„ ์ˆœ์œ„์™€ preload๋กœ ๊ฐ„์ฃผ๋œ๋‹ค. priority๋ฅผ ์‚ฌ์šฉํ•œ ์ด๋ฏธ์ง€๋“ค์€ lazy loading์ด ์ž๋™์œผ๋กœ ๋น„ํ™œ์„ฑํ™”๋œ๋‹ค. 

Largest Contentful Paint(LCP) ์š”์†Œ๋กœ ๊ฐ์ง€๋˜๋Š” ์–ด๋–ค ์ด๋ฏธ์ง€๋“  priority ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๋‹ค๋ฅธ ์ด๋ฏธ์ง€๋Š” ๋‹ค๋ฅธ ๋ทฐํฌํŠธ ํฌ๊ธฐ์— ๋Œ€ํ•œ LCP ์š”์†Œ์ผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๋Ÿฌ ์šฐ์„ ์ˆœ์œ„ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ–๋Š” ๊ฒƒ์ด ์ ์ ˆํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๋ฏธ์ง€๊ฐ€ ์›น ์‚ฌ์ดํŠธ ์ƒ๋‹จ๋ถ€*์— ๋ณด์—ฌ์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ false์ด๋‹ค. 

(*above the fold: ์›น ์‚ฌ์ดํŠธ์—์„œ ์Šคํฌ๋กค์„ ๋‚ด๋ฆฌ์ง€ ์•Š๊ณ  ์ œ์ผ ๋จผ์ € ๋ณผ ์ˆ˜ ์žˆ๋Š” ์˜์—ญ, 1๋ฉด์˜ ์ƒ๋‹จ๋ถ€ - ๋„ค์ด๋ฒ„ ์˜คํ”ˆ์‚ฌ์ „)

 

placeholder

A placeholder to use while the image is loading. Possible values are 'blur' or 'empty'. Defaults to empty.

When blur, the blurDataURL property will be used as the placeholder. If src is an object from a static import and the imported image is .jpg, .png, .webp or .avif, then blurDataURL will automatically be populated.

For dynamic images, you must provide the blurDataURL property. Solutions such as Plaiceholder can help with base64 generation.

When empty, there will be no placeholder while the image is loading, only empty space.

Placeholder๋Š” ์ด๋ฏธ์ง€๊ฐ€ ๋กœ๋“œ๋˜๋Š” ๋™์•ˆ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ฐ€๋Šฅํ•œ ๊ฐ’์€ blur๋‚˜ empty์ด๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ empty. 

blur์ผ ๋•Œ, blurDataURL ์†์„ฑ์ด placeholder๋กœ ์‚ฌ์šฉ๋  ๊ฒƒ์ด๋‹ค. ๋งŒ์ผ src๊ฐ€ ์ •์ ์œผ๋กœ ๊ฐ€์ ธ์˜ค๋Š” ๊ฐœ์ฒด์ด๊ณ  ์ด๋ฏธ์ง€์˜ ํ™•์žฅ์ž๊ฐ€ .jpg, .png,  .webp ํ˜น์€ .avif์ธ ๊ฒฝ์šฐ, blurDataURL์€ ์ž๋™์œผ๋กœ ์ฑ„์›Œ์งˆ ๊ฒƒ์ด๋‹ค. 

๋™์  ์ด๋ฏธ์ง€์˜ ๊ฒฝ์šฐ blurDataURL ์†์„ฑ์„ ์ œ๊ณตํ•ด์•ผํ•œ๋‹ค. Plaiceholder์™€ ๊ฐ™์€ ์†”๋ฃจ์…˜์ด base64 ์ƒ์„ฑ์— ๋„์›€์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

empty์ผ ๋•Œ, ์ด๋ฏธ์ง€๊ฐ€ ๋กœ๋“œ๋˜๋Š” ๋™์•ˆ ์–ด๋–ค placeholder๋„ ์—†์„ ๊ฒƒ์ด๋ฉฐ ๋นˆ ๊ณต๊ฐ„๋งŒ ๋ณด์ผ ๊ฒƒ์ด๋‹ค. 

Try it out:

 

Advanced Props

In some cases, you may need more advanced usage. The <Image /> component optionally accepts the following advanced properties.

์–ด๋–ค ๊ฒฝ์šฐ์—์„œ๋Š” ๋” ๊ณ ๊ธ‰ ์‚ฌ์šฉ๋ฒ•์ด ํ•„์š”ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. <Image /> ์ปดํฌ๋„ŒํŠธ๋Š” ์„ ํƒ์ ์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ณ ๊ธ‰ ์†์„ฑ๋“ค์„ ํ—ˆ์šฉํ•œ๋‹ค. 

style

Allows passing CSS styles to the underlying image element.

Note that all layout modes other than "raw"* apply their own styles to the image element, and these automatic styles take precedence over the style prop.

CSS ์Šคํƒ€์ผ์„ ๊ธฐ๋ณธ ์ด๋ฏธ์ง€ ์š”์†Œ์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

"raw"๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ ˆ์ด์•„์›ƒ ๋ชจ๋“œ๋“ค์€ ๊ฐ์ž์˜ ์Šคํƒ€์ผ์„ ์ด๋ฏธ์ง€ ์š”์†Œ์— ์ ์šฉ์‹œํ‚จ๋‹ค๋Š” ์ ์„ ๊ธฐ์–ตํ•˜๋ผ. ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฌํ•œ ์ž๋™ ์Šคํƒ€์ผ์€ style ์†์„ฑ๋ณด๋‹ค ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง„๋‹ค. 

 

objectFit

Defines how the image will fit into its parent container when using layout="fill".
This value is passed to the object-fit CSS property for the src image.

layout="fill" ์‚ฌ์šฉ ์‹œ ์ด๋ฏธ์ง€๊ฐ€ ๋ถ€๋ชจ ์ปจํ…Œ์ด๋„ˆ์— ๋งž๋Š”(fit) ๋ฐฉ์‹์„ ์ •์˜ํ•œ๋‹ค. 
์ด ๊ฐ’์€ ์†Œ์Šค ์ด๋ฏธ์ง€์˜ object-fit CSS ์†์„ฑ์œผ๋กœ ์ „๋‹ฌ๋œ๋‹ค. 

 

objectPosition

Defines how the image is positioned within its parent element when using layout="fill".
This value is passed to the object-position CSS property applied to the image.

layout="fill" ์‚ฌ์šฉ์‹œ ๋ถ€๋ชจ ์š”์†Œ์˜ ๋‚ด๋ถ€์—์„œ ์ด๋ฏธ์ง€ ๋ฐฐ์น˜ ๋ฐฉ์‹์„ ์ •์˜ํ•œ๋‹ค. 
์ด ๊ฐ’์€ ์ด๋ฏธ์ง€์˜ object-position CSS ์†์„ฑ์œผ๋กœ ์ „๋‹ฌ๋œ๋‹ค. 

 

onLoadingComplete

A callback function that is invoked once the image is completely loaded and the placeholder has been removed.
The onLoadingComplete function accepts one parameter, an object with the following properties:

์ด๋ฏธ์ง€๊ฐ€ ์™„์ „ํžˆ ๋กœ๋“œ๋˜๊ณ  placeholder๊ฐ€ ์‚ฌ๋ผ์ง€๋ฉด ํ˜ธ์ถœ๋˜๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜์ด๋‹ค. 
onLoadingComplete ํ•จ์ˆ˜๋Š” ์•„๋ž˜์˜ ์†์„ฑ๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ๊ฐ์ฒด๋ฅผ ํ•˜๋‚˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š”๋‹ค:

 

loading

Attention: This property is only meant for advanced usage. Switching an image to load with eager will normally hurt performance.
We recommend using the priority property instead, which properly loads the image eagerly for nearly all use cases.
์ฃผ์˜: ์ด ์†์„ฑ์€ ๊ณ ๊ธ‰ ์‚ฌ์šฉ๋ฒ•์„ ์œ„ํ•จ์ด๋‹ค. ๋Œ€๊ฐœ ์ด๋ฏธ์ง€๋ฅผ ๋กœ๋“œํ•˜๊ธฐ ์œ„ํ•œ loading ๊ฐ’์„ eagar๋กœ ๋ฐ”๊พธ๋ฉด ์„ฑ๋Šฅ ์ €ํ•˜์— ์˜ํ–ฅ์„ ๋ฏธ์นœ๋‹ค. (์•„๋ž˜์˜ ์†์„ฑ์„ ์„ค๋ช…ํ•˜๋Š” ๋ถ€๋ถ„์„ ๋จผ์ € ์ฝ๋Š” ๊ฒƒ์ด ์ฃผ์˜ ์‚ฌํ•ญ์„ ์ดํ•ดํ•˜๋Š”๋ฐ ๋„์›€์ด ๋จ.)
๋Œ€์‹  ๊ฑฐ์˜ ๋ชจ๋“  ๊ฒฝ์šฐ์— ์ด๋ฏธ์ง€๋ฅผ ์ ์ ˆํžˆ ๋กœ๋“œํ•˜๋Š” priority ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค. 

The loading behavior of the image. Defaults to lazy.

When lazy, defer loading the image until it reaches a calculated distance from the viewport.

When eager, load the image immediately.

Learn more

์ด๋ฏธ์ง€์˜ ๋กœ๋”ฉ ๋™์ž‘์— ๋Œ€ํ•œ ์†์„ฑ. ๊ธฐ๋ณธ๊ฐ’์€ lazy์ด๋‹ค. 

๊ฐ’์ด lazy์ธ ๊ฒฝ์šฐ, ๋ทฐํฌํŠธ๋กœ๋ถ€ํ„ฐ ๊ณ„์‚ฐ๋œ ๊ฑฐ๋ฆฌ์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ์ด๋ฏธ์ง€ ๋กœ๋”ฉ์„ ๋ฏธ๋ฃฌ๋‹ค. 

๊ฐ’์ด eager์ธ ๊ฒฝ์šฐ, ์ด๋ฏธ์ง€๋ฅผ ์ฆ‰์‹œ ๋กœ๋“œํ•œ๋‹ค. 

 

blurDataURL

A Data URL to be used as a placeholder image before the src image successfully loads. Only takes effect when combined with placeholder="blur".

Must be a base64-encoded image. It will be enlarged and blurred, so a very small image (10px or less) is recommended. Including larger images as placeholders may harm your application performance.

Data URL์€ src ์ด๋ฏธ์ง€๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ๋กœ๋“œ๋˜์ง€ ์ „์— placeholder ์ด๋ฏธ์ง€๋กœ ์‚ฌ์šฉ๋  ๊ฒƒ์ด๋‹ค. placeholder="blur"์™€ ๊ฒฐํ•ฉ๋œ ๊ฒฝ์šฐ์—๋งŒ ์ ์šฉ๋œ๋‹ค. 

base64-encoded ์ด๋ฏธ์ง€์—ฌ์•ผ๋งŒ ํ•œ๋‹ค. ํ™•๋Œ€๋˜์–ด ํ๋ ค์ง€๋ฏ€๋กœ ์•„์ฃผ ์ž‘์€ ์ด๋ฏธ์ง€(10ํ”ฝ์…€์ด๋‚˜ ๋” ์ž‘์€)๋ฅผ ๊ถŒ์žฅํ•œ๋‹ค. placeholder๋กœ ํฐ ์ด๋ฏธ์ง€๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฒฝ์šฐ ๋‹น์‹ ์˜ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ฑ๋Šฅ์— ํ•ด๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค. 

Try it out:

You can also generate a solid color Data URL to match the image.

์ด๋ฏธ์ง€์™€ ์ผ์น˜ํ•˜๋„๋ก ๋‹จ์ƒ‰ Data URL์„ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

lazyBoundary

A string (with similar syntax to the margin property) that acts as the bounding box used to detect the intersection of the viewport with the image and trigger lazy loading. Defaults to "200px".

์ด๋ฏธ์ง€์™€ ๋ทฐํฌํŠธ์˜ ๊ต์ฐจ๋ฅผ ๊ฐ์ง€ํ•˜๊ณ  lazy loading์„ ํŠธ๋ฆฌ๊ฑฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ๊ณ„ ์ƒ์ž ์—ญํ• ์„ ํ•˜๋Š” ๋ฌธ์ž์—ด์ด๋‹ค. (margin ์†์„ฑ๊ณผ ์œ ์‚ฌํ•œ ๊ตฌ๋ฌธ). ๊ธฐ๋ณธ๊ฐ’์€ 200px์ด๋‹ค.

Learn more

 

lazyRoot

A React Ref pointing to the scrollable parent element. Defaults to null (the document viewport).

The Ref must point to a DOM element or a React component that forwards the Ref to the underlying DOM element.

React Ref๋Š” ์Šคํฌ๋กค๊ฐ€๋Šฅํ•œ ๋ถ€๋ชจ ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ null (document ๋ทฐํฌํŠธ)์ด๋‹ค.

Ref๋Š” DOM ์š”์†Œ๋‚˜ ๊ธฐ๋ณธ DOM ์š”์†Œ์— Ref๋ฅผ ์ „๋‹ฌํ•˜๋Š” React ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฐ€๋ฆฌ์ผœ์•ผ ํ•œ๋‹ค. 

Example pointing to a DOM element - DOM ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์˜ˆ์ œ

import Image from 'next/image'
import React from 'react'

const lazyRoot = React.useRef(null)

const Example = () => (
  <div ref={lazyRoot} style={{ overflowX: 'scroll', width: '500px' }}>
    <Image lazyRoot={lazyRoot} src="/one.jpg" width="500" height="500" />
    <Image lazyRoot={lazyRoot} src="/two.jpg" width="500" height="500" />
  </div>
)

Example pointing to a React component - React ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์˜ˆ์ œ

import Image from 'next/image'
import React from 'react'

const Container = React.forwardRef((props, ref) => {
  return (
    <div ref={ref} style={{ overflowX: 'scroll', width: '500px' }}>
      {props.children}
    </div>
  )
})

const Example = () => {
  const lazyRoot = React.useRef(null)

  return (
    <Container ref={lazyRoot}>
      <Image lazyRoot={lazyRoot} src="/one.jpg" width="500" height="500" />
      <Image lazyRoot={lazyRoot} src="/two.jpg" width="500" height="500" />
    </Container>
  )
}

Learn more

 

unoptimized

When true, the source image will be served as-is instead of changing quality, size, or format. Defaults to false.

๊ฐ’์ด true์ธ ๊ฒฝ์šฐ, ์›๋ณธ ์ด๋ฏธ์ง€์˜ ํ’ˆ์งˆ, ํฌ๊ธฐ, ํฌ๋งท์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋Œ€์‹  ๊ทธ๋Œ€๋กœ ์ œ๊ณต๋  ๊ฒƒ์ด๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ false์ด๋‹ค.

 

Other Props

Other properties on the <Image /> component will be passed to the underlying img element with the exception of the following:

<Image /> ์ปดํฌ๋„ŒํŠธ์˜ ๋‹ค๋ฅธ ์†์„ฑ๋“ค์€ ์•„๋ž˜์— ๋Œ€ํ•ด์„œ๋Š” ์ œ์™ธํ•˜๊ณ  ๊ธฐ๋ณธ img ์†์„ฑ์œผ๋กœ ์ „๋‹ฌ๋  ๊ฒƒ์ด๋‹ค: 

  • srcSet. ๋Œ€์‹  Device Sizes๋ฅผ ์‚ฌ์šฉํ•˜๋ผ.
  • ref. ๋Œ€์‹  onLoadingComplete๋ฅผ ์‚ฌ์šฉํ•˜๋ผ.
  • decoding. ํ•ญ์ƒ async์ด๋‹ค. 

 

Configuration Options

Domains

To protect your application from malicious users, you must define a list of image provider domains that you want to be served from the Next.js Image Optimization API. This is configured in with the domains property in your next.config.js file, as shown below:

์•…์„ฑ ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๋‹น์‹ ์˜ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋ณดํ˜ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”, Next.js ์ด๋ฏธ์ง€ ์ตœ์ ํ™” API์— ์ œ๊ณตํ•  ์ด๋ฏธ์ง€ ์ œ๊ณต์ž ๋„๋ฉ”์ธ ๋ชฉ๋ก์„ ์ •ํ•ด์•ผ ํ•œ๋‹ค. ์•„๋ž˜์— ๋ณด์ด๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด next.config.js ํŒŒ์ผ์˜ ๋„๋ฉ”์ธ ์†์„ฑ์œผ๋กœ ์„ค์ •๋œ๋‹ค.

module.exports = {
  images: {
    domains: ['assets.acme.com'],
  },
}

 

Loader Configuration

If you want to use a cloud provider to optimize images instead of using the Next.js built-in Image Optimization API, you can configure the loader and path prefix in your next.config.js file. This allows you to use relative URLs for the Image src and automatically generate the correct absolute URL for your provider.

๋งŒ์ผ Next.js ๋‚ด์žฅ ์ด๋ฏธ์ง€ ์ตœ์ ํ™” API๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์‹  ์ด๋ฏธ์ง€๋ฅผ ์ตœ์ ํ™”ํ•˜๊ธฐ ์œ„ํ•ด ํด๋ผ์šฐ๋“œ ์ œ๊ณต์ž๋ฅผ ์‚ฌ์šฉํ•˜๊ธธ ์›ํ•œ๋‹ค๋ฉด, ์ž์‹ ์˜ next.config.js ํŒŒ์ผ์—์„œ loader์™€ ๊ฒฝ๋กœ prefix๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด์„œ ์ด๋ฏธ์ง€ ๊ฒฝ๋กœ๋ฅผ ์œ„ํ•œ ์ƒ๋Œ€ URL์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ์ œ๊ณต์ž์— ๋Œ€ํ•œ ์˜ฌ๋ฐ”๋ฅธ ์ ˆ๋Œ€ URL์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค. 

module.exports = {
  images: {
    loader: 'imgix',
    path: 'https://example.com/myaccount/',
  },
}

 

Built-in Loaders

The following Image Optimization cloud providers are included:

  • Default: Works automatically with next dev, next start, or a custom server
  • Vercel: Works automatically when you deploy on Vercel, no configuration necessary. Learn more
  • Imgix: loader: 'imgix'
  • Cloudinary: loader: 'cloudinary'
  • Akamai: loader: 'akamai'
  • Custom: loader: 'custom' use a custom cloud provider by implementing the loader prop on the next/image component

If you need a different provider, you can use the loader prop with next/image.

Images can not be optimized at build time using `next export`, only on-demand. To use `next/image` with `next export`, you will need to use a different loader than the default. Read more in the discussion.
The next/image component's default loader uses squoosh because it is quick to install and suitable for a development environment. When using next start in your production environment, it is strongly recommended that you install sharp by running yarn add sharp in your project directory. This is not necessary for Vercel deployments, as sharp is installed automatically.

๋‹ค์Œ ์ด๋ฏธ์ง€ ์ตœ์ ํ™” ํด๋ผ์šฐ๋“œ ์ œ๊ณต์ž๊ฐ€ ํฌํ•จ๋œ๋‹ค:

  • ๊ธฐ๋ณธ๊ฐ’: next dev, next start ๋‚˜ ์ปค์Šคํ…€ ์„œ๋ฒ„์™€ ์ž๋™์œผ๋กœ ์ž‘๋™
  • Vercel: Vercel์— ๋ฐฐํฌํ•˜๋ฉด ์ž๋™์œผ๋กœ ์ž‘๋™, ๊ตฌ์„ฑ ํ•„์š” ์—†์Œ. Learn more
  • Imgix: loader: 'imgix'
  • Cloudinary: loader: 'cloudinary'
  • Akamai: loader: 'akamai'
  • ์ปค์Šคํ…€: loader: 'custom' ์€ next/image ์ปดํฌ๋„ŒํŠธ์— loader prop์„ ๊ตฌํ˜„ํ•˜์—ฌ ์ปค์Šคํ…€ ํด๋ผ์šฐ๋“œ ์ œ๊ณต์ž๋ฅผ ์‚ฌ์šฉํ•จ.

๋งŒ์•ฝ ๋‹ค๋ฅธ ์ œ๊ณต์ž๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ, next/image์™€ ํ•จ๊ป˜ loader prop์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

`next export`๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋ฏธ์ง€๋Š” ๋นŒ๋“œ ์‹œ๊ฐ„์— ์ตœ์ ํ™” ๋  ์ˆ˜ ์—†๊ณ  ์š”๊ตฌ์— ๋”ฐ๋ผ์„œ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค. `next/image`๋ฅผ `next export`์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ธฐ๋ณธ ๋กœ๋”๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ๋กœ๋”๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค. ๋งํฌ์˜ discussion์—์„œ ๋” ์ฝ์–ด๋ณด๋ผ. 
next/image ์ปดํฌ๋„ŒํŠธ์˜ ๊ธฐ๋ณธ loader๋Š” squoosh๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์„ค์น˜๊ฐ€ ๋น ๋ฅด๊ณ  ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์— ์ ํ•ฉํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์šด์˜ ํ™˜๊ฒฝ์—์„œ next start๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ† ๋ฆฌ์—์„œ yarn add sharp๋ฅผ ์‹คํ–‰ํ•˜์—ฌ sharp๋ฅผ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์„ ๊ฐ•๋ ฅ ์ถ”์ฒœํ•œ๋‹ค. Vercel์— ๋ฐฐํฌํ•˜๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด ์ž๋™์œผ๋กœ sharp๊ฐ€ ์„ค์น˜๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ถˆํ•„์š”ํ•˜๋‹ค. 

 

Advanced

The following configuration is for advanced use cases and is usually not necessary. If you choose to configure the properties below, you will override any changes to the Next.js defaults in future updates.

์•„๋ž˜์˜ ๊ตฌ์„ฑ์€ ๊ณ ๊ธ‰ ์‚ฌ์šฉ์„ ์œ„ํ•œ ๊ฒฝ์šฐ์ด๋ฉฐ ๋Œ€๊ฐœ๋Š” ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค. ๋‹น์‹ ์ด ์•„๋ž˜์˜ ์†์„ฑ๋“ค์— ๋Œ€ํ•ด ๊ตฌ์„ฑํ•˜๊ธธ ์„ ํƒํ•œ๋‹ค๋ฉด, ์•ž์œผ๋กœ ์žˆ์„ ์—…๋ฐ์ดํŠธ์—์„œ์˜ Next.js ๊ธฐ๋ณธ๊ฐ’์— ๋Œ€ํ•œ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•  ๊ฒƒ์ด๋‹ค. 

Device Sizes

If you know the expected device widths of your users, you can specify a list of device width breakpoints using the deviceSizes property in next.config.js. These widths are used when the next/image component uses layout="responsive" or layout="fill" to ensure the correct image is served for user's device.

If no configuration is provided, the default below is used.

์‚ฌ์šฉ์ž์˜ ์˜ˆ์ƒ ๊ธฐ๊ธฐ ๋„ˆ๋น„๋ฅผ ์•Œ๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ, ๊ธฐ๊ธฐ ๋„ˆ๋น„ ์ค‘๋‹จ์  ๋ชฉ๋ก์„ next.config.js์˜ deviceSizes ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ช…์‹œํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๋„ˆ๋น„๋“ค์€ next/image ์ปดํฌ๋„ŒํŠธ๊ฐ€ layout="responsive"๋‚˜ layout="fill"์ธ ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž์˜ ๊ธฐ๊ธฐ์— ์˜ฌ๋ฐ”๋ฅธ ์ด๋ฏธ์ง€ ์ œ๊ณต์„ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค. 

์–ด๋– ํ•œ ๊ตฌ์„ฑ์ด ์ œ๊ณต๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์•„๋ž˜์˜ ๊ธฐ๋ณธ๊ฐ’์ด ์‚ฌ์šฉ๋œ๋‹ค. 

module.exports = {
  images: {
    deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
  },
}

 

Image Sizes

You can specify a list of image widths using the images.imageSizes property in your next.config.js file. These widths are concatenated with the array of device sizes to form the full array of sizes used to generate image srcsets.

The reason there are two separate lists is that imageSizes is only used for images which provide a sizes prop, which indicates that the image is less than the full width of the screen. Therefore, the sizes in imageSizes should all be smaller than the smallest size in deviceSizes.

If no configuration is provided, the default below is used.

next.config.js ํŒŒ์ผ์˜ images.imageSizes ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€ ๋„ˆ๋น„ ๋ชฉ๋ก์„ ๋ช…์‹œํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๋„ˆ๋น„๋“ค์€ ๊ธฐ๊ธฐ ๋„ˆ๋น„(device sizes) ๋ฐฐ์—ด๊ณผ ์—ฐ๊ฒฐ๋˜์–ด ์ด๋ฏธ์ง€ srcset์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ „์ฒด ํฌ๊ธฐ ๋ฐฐ์—ด์„ ํ˜•์„ฑํ•œ๋‹ค. 

์—ฌ๊ธฐ์— ๋‘ ๊ฐ€์ง€ ๋ถ„๋ฆฌ๋œ ๋ชฉ๋ก์ด ์žˆ๋Š” ์ด์œ ๋Š” imageSizes๊ฐ€ sizes prop์„ ์ œ๊ณตํ•˜๋Š” ์ด๋ฏธ์ง€์—๋งŒ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด๋Š” ์ด๋ฏธ์ง€๊ฐ€ ํ™”๋ฉด์˜ ์ „์ฒด ๋„ˆ๋น„๋ณด๋‹ค ์ž‘๋‹ค๋Š” ๊ฒƒ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ, imageSizes์˜ sizes ๋ชจ๋‘๋Š” deviceSizes์˜ ๊ฐ€์žฅ ์ž‘์€ ํฌ๊ธฐ๋ณด๋‹ค ์ž‘์•„์•ผ๋งŒ ํ•œ๋‹ค. 

์–ด๋– ํ•œ ๊ตฌ์„ฑ์ด ์ œ๊ณต๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์•„๋ž˜์˜ ๊ธฐ๋ณธ๊ฐ’์ด ์‚ฌ์šฉ๋œ๋‹ค.

module.exports = {
  images: {
    imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
  },
}

 

Acceptable Formats

The default Image Optimization API will automatically detect the browser's supported image formats via the request's Accept header.

If the Accept head matches more than one of the configured formats, the first match in the array is used. Therefore, the array order matters. If there is no match, the Image Optimization API will fallback to the original image's format.

If no configuration is provided, the default below is used.

๊ธฐ๋ณธ ์ด๋ฏธ์ง€ ์ตœ์ ํ™” API๋Š” ์š”์ฒญ Accept ํ—ค๋”๋ฅผ ํ†ตํ•ด์„œ ๋ธŒ๋ผ์šฐ์ €์˜ ์ง€์› ๊ฐ€๋Šฅ ์ด๋ฏธ์ง€ ํฌ๋งท์„ ์ž๋™์œผ๋กœ ๊ฐ์ง€ํ•  ๊ฒƒ์ด๋‹ค. 

Accept ํ—ค๋”๊ฐ€ ๊ตฌ์„ฑ๋œ ํฌ๋งท ์ค‘ ํ•˜๋‚˜ ์ด์ƒ๊ณผ ์ผ์น˜๋˜๋Š” ๊ฒฝ์šฐ, ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๋งค์น˜๋œ ๊ฒƒ์„ ์‚ฌ์šฉํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋ฐฐ์—ด์˜ ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•˜๋‹ค. ์ผ์น˜ํ•˜๋Š” ๊ฒƒ์ด ์—†์œผ๋ฉด, ์ด๋ฏธ์ง€ ์ตœ์ ํ™” API๋Š” ์›๋ณธ ์ด๋ฏธ์ง€์˜ ํฌ๋งท์œผ๋กœ ๋Œ€์ฒด๋œ๋‹ค. 

์–ด๋– ํ•œ ๊ตฌ์„ฑ์ด ์ œ๊ณต๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์•„๋ž˜์˜ ๊ธฐ๋ณธ๊ฐ’์ด ์‚ฌ์šฉ๋œ๋‹ค.

module.exports = {
  images: {
    formats: ['image/webp'],
  },
}

You can enable AVIF support with the following configuration.
์•„๋ž˜์˜ ๊ตฌ์„ฑ์„ ํ†ตํ•ด AVIF ์ง€์›์„ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. 

module.exports = {
  images: {
    formats: ['image/avif', 'image/webp'],
  },
}
Note: AVIF generally takes 20% longer to encode but it compresses 20% smaller compared to WebP. This means that the first time an image is requested, it will typically be slower and then subsequent requests that are cached will be faster.
๋…ธํŠธ: AVIF๋Š” ๋Œ€๊ฐœ ์ธ์ฝ”๋”ฉํ•˜๋Š”๋ฐ 20% ๋‚˜ ๋” ์˜ค๋ž˜ ๊ฑธ๋ฆฌ์ง€๋งŒ WebP์™€ ๋น„๊ตํ•  ๋•Œ 20% ๋” ์ž‘๊ฒŒ ์••์ถ•ํ•œ๋‹ค. ์ด ๋ง์˜ ์˜๋ฏธ๋Š” ์ด๋ฏธ์ง€๊ฐ€ ์ฒ˜์Œ ์š”์ฒญ๋˜์—ˆ์„ ๋•Œ ๋ณดํ†ต ๋” ๋Š๋ฆฌ๊ณ  ์บ์‹œ๋œ ๊ทธ ๋‹ค์Œ์˜ ์š”์ฒญ๋ถ€ํ„ฐ๋Š” ๋” ๋น ๋ฅด๋‹ค. 

 

Caching Behavior

The following describes the caching algorithm for the default loader. For all other loaders, please refer to your cloud provider's documentation.

Images are optimized dynamically upon request and stored in the <distDir>/cache/images directory. The optimized image file will be served for subsequent requests until the expiration is reached. When a request is made that matches a cached but expired file, the expired image is served stale immediately. Then the image is optimized again in the background (also called revalidation) and saved to the cache with the new expiration date.

The cache status of an image can be determined by reading the value of the `x-nextjs-cache` response header. The possible values are the following:

  • MISS - the path is not in the cache (occurs at most once, on the first visit)
  • STALE - the path is in the cache but exceeded the revalidate time so it will be updated in the background
  • HIT - the path is in the cache and has not exceeded the revalidate time

The expiration (or rather Max Age) is defined by either the minimumCacheTTL configuration or the upstream server's `Cache-Control` header, whichever is larger. Specifically, the `max-age` value of the `Cache-Control` header is used. If both `s-maxage` and `max-age` are found, then `s-maxage` is preferred.

  • You can configure minimumCacheTTL to increase the cache duration when the upstream image does not include `Cache-Control` header or the value is very low.
  • You can configure deviceSizes and imageSizes to reduce the total number of possible generated images.
  • You can configure formats to disable multiple formats in favor of a single image format.

์•„๋ž˜์˜ ์„ค๋ช…์€ ๊ธฐ๋ณธ loader์˜ ์บ์‹ฑ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•œ ๊ฒƒ์ด๋‹ค. ๋‹ค๋ฅธ loader์— ๋Œ€ํ•ด์„œ๋Š” ๋‹น์‹ ์˜ ํด๋ผ์šฐ๋“œ ์ œ๊ณต์ž์˜ ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜๊ธธ ๋ฐ”๋ž€๋‹ค. 

์ด๋ฏธ์ง€๋Š” ์š”์ฒญ์— ๋”ฐ๋ผ์„œ ๋™์ ์œผ๋กœ ์ตœ์ ํ™”๋˜๊ณ  <distDir>/cache/images ๋””๋ ‰ํ† ๋ฆฌ์— ์ €์žฅ๋œ๋‹ค. ์ตœ์ ํ™”๋œ ์ด๋ฏธ์ง€ ํŒŒ์ผ์€ ๋งŒ๋ฃŒ๊ธฐ๊ฐ„ ์ „๊นŒ์ง€ ํ›„์† ์š”์ฒญ์— ๋Œ€ํ•ด์„œ ์ œ๊ณต๋  ๊ฒƒ์ด๋‹ค. ์บ์‹œ๊ฐ€ ๋˜์–ด ์žˆ์ง€๋งŒ ๋งŒ๋ฃŒ๋œ ํŒŒ์ผ์— ๋Œ€ํ•œ ์š”์ฒญ์ด ์ด๋ฃจ์–ด์ง€๋ฉด, ๋งŒ๋ฃŒ๋œ ์ด๋ฏธ์ง€๋Š” ์ฆ‰์‹œ ์˜ค๋ž˜๋œ ์ด๋ฏธ์ง€๋กœ ์ œ๊ณต๋œ๋‹ค. ๊ทธ ์ด๋ฏธ์ง€๋Š” ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ๋‹ค์‹œ ์ตœ์ ํ™”๋˜๊ณ (์žฌ๊ฒ€์ฆ์ด๋ผ๊ณ  ๋ถˆ๋ฆผ) ์ƒˆ๋กœ์šด ๋งŒ๋ฃŒ์ผ์ž๋กœ ์บ์‹œ๋˜์–ด ์ €์žฅ๋œ๋‹ค. 

์ด๋ฏธ์ง€์˜ ์บ์‹œ ์ƒํƒœ๋Š” `x-nextjs-cache` ์‘๋‹ต ํ—ค๋”์˜ ๊ฐ’์„ ์ฝ์–ด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ€๋Šฅํ•œ ๊ฐ’๋“ค์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • MISS - ํ•ด๋‹น ๊ฒฝ๋กœ๊ฐ€ ์บ์‹œ์— ์—†์Œ(์ฒซ ๋ฐฉ๋ฌธ์‹œ์— ์ตœ๋Œ€ ํ•œ ๋ฒˆ ๋ฐœ์ƒํ•จ)
  • STALE - ํ•ด๋‹น ๊ฒฝ๋กœ๊ฐ€ ์บ์‹œ์— ์žˆ์ง€๋งŒ ์žฌ๊ฒ€์ฆ ์‹œ๊ฐ„์ด ์ดˆ๊ณผ๋˜์–ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์—…๋ฐ์ดํŠธ ๋  ๊ฒƒ์ž„
  • HIT - ํ•ด๋‹น ๊ฒฝ๋กœ๊ฐ€ ์บ์‹œ์— ์žˆ์œผ๋ฉฐ ์žฌ๊ฒ€์ฆ ์‹œ๊ฐ„์„ ์ดˆ๊ณผํ•˜์ง€ ์•Š์Œ

๋งŒ๋ฃŒ๊ธฐ๊ฐ„(ํ˜น์€ Max Age)์€ minimumCacheTTL ๊ตฌ์„ฑ์ด๋‚˜ upstream ์„œ๋ฒ„์˜ Cache-Control ํ—ค๋” ์ค‘ ๋” ํฐ ๊ฐ’์œผ๋กœ ์ •์˜๋œ๋‹ค. ๊ตฌ์ฒด์ ์œผ๋กœ ๋งํ•˜๋ฉด Cache-Control ํ—ค๋”์˜ max-age ๊ฐ’์ด ์‚ฌ์šฉ๋œ๋‹ค. ๋งŒ์ผ s-maxage์™€ max-age ๋ชจ๋‘ ์žˆ๋Š” ๊ฒฝ์šฐ, s-maxage๊ฐ€ ์„ ํ˜ธ๋œ๋‹ค. 

  • upstream ์ด๋ฏธ์ง€๊ฐ€ Cache-Control ํ—ค๋”๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ๊ทธ ๊ฐ’์ด ๋„ˆ๋ฌด ์งง์€ ๊ฒฝ์šฐ ์บ์‹œ ๊ธฐ๊ฐ„์„ ๋Š˜๋ฆฌ๊ธฐ ์œ„ํ•ด์„œ minimumCacheTTL์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. 
  • ๊ฐ€๋Šฅํ•œ ์ƒ์„ฑ ์ด๋ฏธ์ง€ ๊ฐฏ์ˆ˜๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด deviceSizes์™€ imageSizes๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋‹จ์ผ ์ด๋ฏธ์ง€ ํ˜•์‹์„ ์œ„ํ•ด ์—ฌ๋Ÿฌ ํ˜•์‹์„ ๋น„ํ™œ์„ฑํ™”ํ•˜๋„๋ก ํ˜•์‹์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Minimum Cache TTL

You can configure the Time to Live (TTL) in seconds for cached optimized images. In many cases, it's better to use a Static Image Import which will automatically hash the file contents and cache the image forever with a Cache-Control header of `immutable`.

์บ์‹œ๋œ ์ตœ์ ํ™” ์ด๋ฏธ์ง€์— ๋Œ€ํ•ด TTL(Time to Live)์„ ์ดˆ ๋‹จ์œ„๋กœ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŽ์€ ๊ฒฝ์šฐ ์ž๋™์œผ๋กœ ํŒŒ์ผ์„ ํ•ด์‹œํ•˜์—ฌ `immutable`์˜ `Cache-Control` ํ—ค๋”๋กœ ์˜๊ตฌํžˆ ์บ์‹œํ•˜๋Š” Static Image Import๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ๋‚ซ๋‹ค. 

module.exports = {
  images: {
    minimumCacheTTL: 60,
  },
}

If you need to add a Cache-Control header for the browser (not recommended), you can configure headers on the upstream image e.g. /some-asset.jpg not /_next/image itself.

๋ธŒ๋ผ์šฐ์ €์— Cache-Control์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค๋ฉด (๊ถŒ์žฅ๋˜์ง€ ์•Š์Œ), ์—…์ŠคํŠธ๋ฆผ ์ด๋ฏธ์ง€์˜ ํ—ค๋”๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ์‹œ. /_next/image ์ž์‹ ์ด ์•„๋‹Œ /some-asset.jpg 

 

Disable Static Imports

The default behavior allows you to import static files such as import icon from './icon.png and then pass that to the src property.

In some cases, you may wish to disable this feature if it conflicts with other plugins that expect the import to behave differently.

You can disable static image imports inside your next.config.js:

๊ธฐ๋ณธ์ ์ธ ๋™์ž‘์€ `import icon from './icon.png'`๊ฐ™์€ ์ •์  ํŒŒ์ผ์„ ๊ฐ€์ ธ์™€์„œ src ์†์„ฑ์— ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•œ๋‹ค. 

๊ฒฝ์šฐ์— ๋”ฐ๋ผ, ๊ฐ€์ ธ์˜ค๊ธฐ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•˜๋Š” ๋‹ค๋ฅธ ํ”Œ๋Ÿฌ๊ทธ์ธ๊ณผ ์ถฉ๋Œ์ด ์ผ์–ด๋‚˜๋Š” ๊ฒฝ์šฐ ์ด ๊ธฐ๋Šฅ์„ ๋น„ํ™œ์„ฑํ™”ํ•˜๊ณ  ์‹ถ์„ ์ˆ˜ ์žˆ๋‹ค. 

next.config.js์—์„œ ์ •์  ์ด๋ฏธ์ง€ ๊ฐ€์ ธ์˜ค๊ธฐ๋ฅผ ๋น„ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. 

module.exports = {
  images: {
    disableStaticImages: true,
  },
}

 

Dangerously Allow SVG

The default loader does not optimize SVG images for a few reasons. First, SVG is a vector format meaning it can be resized losslessly. Second, SVG has many of the same features as HTML/CSS, which can lead to vulnerabilities without proper Content Security Policy (CSP) headers.

If you need to serve SVG images with the default Image Optimization API, you can set dangerouslyAllowSVG and contentSecurityPolicy inside your next.config.js:

๊ธฐ๋ณธ ๋กœ๋”๋Š” ๋ช‡ ๊ฐ€์ง€ ์ด์œ ๋กœ SVG ์ด๋ฏธ์ง€๋ฅผ ์ตœ์ ํ™”ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ฒซ ๋ฒˆ์งธ๋Š”, SVG๋Š” ๋ฒกํ„ฐ ํฌ๋งท์œผ๋กœ ์†์‹ค์—†์ด ์‚ฌ์ด์ฆˆ๋ฅผ ์กฐ์ ˆํ•  ์ˆ˜ ์žˆ์Œ์„ ์˜๋ฏธํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ๋Š”, SVG์—๋Š” HTML/CSS์™€ ๋™์ผํ•œ ๊ธฐ๋Šฅ์ด ๋งŽ์ด ์žˆ๋Š”๋ฐ, ์ ์ ˆํ•œ ์ปจํ…์ธ  ๋ณด์•ˆ ์ •์ฑ…(CSP) ํ—ค๋” ์—†์ด๋Š” ์ทจ์•ฝ์ ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. 

SVG ์ด๋ฏธ์ง€๋ฅผ ๊ธฐ๋ณธ ์ด๋ฏธ์ง€ ์ตœ์ ํ™” API๋กœ ์ œ๊ณตํ•˜๊ธฐ๋ฅผ ์›ํ•œ๋‹ค๋ฉด, next.config.js์—์„œ dangerouslyAllowSVG์™€ contentSecurityPolicy๋ฅผ ์„ค์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค. 

module.exports = {
  images: {
    dangerouslyAllowSVG: true,
    contentSecurityPolicy: "default-src 'self'; script-src 'none'; sandbox;",
  },
}

 

Experimental "raw" layout mode

The image component currently supports an additional `layout="raw"` mode, which renders the image without wrappers or styling. This layout mode is currently an experimental feature, while user feedback is gathered. As there is the possibility of breaking changes to the layout="raw" interface, the feature is locked behind an experimental feature flag. If you would like to use the `raw` layout mode, you must add the following to your next.config.js:

์ด๋ฏธ์ง€ ์ปดํฌ๋„ŒํŠธ๋Š” ํ˜„์žฌ ์ถ”๊ฐ€์ ์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ wrapper๋‚˜ ์Šคํƒ€์ผ๋ง์—†์ด ๋ Œ๋”ํ•˜๋Š” `layout="raw"` ๋ชจ๋“œ๋ฅผ ์ง€์›ํ•œ๋‹ค. ์ด ๋ ˆ์ด์•„์›ƒ ๋ชจ๋“œ๋Š” ์‹คํ—˜์ ์ธ ๊ธฐ๋Šฅ์œผ๋กœ ์‚ฌ์šฉ์ž ํ”ผ๋“œ๋ฐฑ์„ ๋ชจ์œผ๊ณ  ์žˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— layout="raw" ์ธํ„ฐํŽ˜์ด์Šค์— ํฐ ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์€ ์‹คํ—˜์ ์ธ ๊ธฐ๋Šฅ ํ”Œ๋ž˜๊ทธ ๋’ค์— ์ž ๊ฒจ์žˆ๋‹ค. `raw` ๋ ˆ์ด์•„์›ƒ ๋ชจ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ next.config.js์— ์ถ”๊ฐ€ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

module.exports = {
  experimental: {
    images: {
      layoutRaw: true,
    },
  },
}
Note on CLS with `layout="raw"`: It is possible to cause layout shift with the image component in `raw` mode. If you include a `sizes` property, the image component will not pass `height` and `width` attributes to the image, to allow you to apply your own responsive sizing. An aspect-ratio style property is automatically applied to prevent layout shift, but this won't apply on older browsers.
 `layout="raw"`์˜ CLS(Cumulative Layout Shift) ์ฐธ๊ณ ์‚ฌํ•ญ: `raw` ๋ชจ๋“œ์ธ ์ด๋ฏธ์ง€ ์ปดํฌ๋„Œํ„ฐ๋Š” ๋ ˆ์ด์•„์›ƒ ์ด๋™์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. `sizes` ์†์„ฑ์„ ํฌํ•จ์‹œํ‚จ๋‹ค๋ฉด, ์ด๋ฏธ์ง€ ์ปดํฌ๋„ŒํŠธ๋Š” ๋‹น์‹ ๋งŒ์˜ ๋ฐ˜์‘ํ˜• ์‚ฌ์ด์ฆˆ๋ฅผ ์ ์šฉ์‹œํ‚ค๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ `height`์™€ `width` ์†์„ฑ์„ ์ด๋ฏธ์ง€์— ๋„˜๊ฒจ์ฃผ์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. ๊ฐ€๋กœ ์„ธ๋กœ ๋น„์œจ ์Šคํƒ€์ผ ์†์„ฑ์€ ๋ ˆ์ด์•„์›ƒ ์ด๋™์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์ž๋™์œผ๋กœ ์ ์šฉ๋˜์ง€๋งŒ ์˜ค๋ž˜๋œ ๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.

 

Animated Images

The default loader will automatically bypass Image Optimization for animated images and serve the image as-is.

Auto-detection for animated files is best-effort and supports GIF, APNG, and WebP. If you want to explicitly bypass Image Optimization for a given animated image, use the unoptimized prop.

๊ธฐ๋ณธ ๋กœ๋”๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ์ด๋ฏธ์ง€ ์ตœ์ ํ™”๋ฅผ ์ž๋™์œผ๋กœ ์šฐํšŒํ•˜๊ณ  ์ด๋ฏธ์ง€ ๊ทธ๋Œ€๋กœ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. 

์• ๋‹ˆ๋ฉ”์ด์…˜ ํŒŒ์ผ์— ๋Œ€ํ•œ ์ž๋™ ๊ฐ์ง€๋Š” ์ตœ์„ ์ด๋ฉฐ GIF, APNG, WebP๋ฅผ ์ง€์›ํ•œ๋‹ค. ํŠน์ •ํ•œ ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ด๋ฏธ์ง€์— ๋Œ€ํ•ด ๋ช…์‹œ์ ์œผ๋กœ ์ด๋ฏธ์ง€ ์ตœ์ ํ™”๋ฅผ ์šฐํšŒํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด unoptimized ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋ผ.