ç¾ä»£ã®ãã¸ã¿ã«ä¸çã§ã¯ãã¦ã§ããã¼ã¸ãã¢ããªã«ããã¦ç»åã¯æ¬ ãããªãè¦ç´ ã¨ãªãã¾ããã
ç»åã使ç¨ãããã¨ã§ãæ å ±ãè¿ éã«ä¼ãããã¨ãã§ããã¦ã¼ã¶ã¼ã¨ã³ã²ã¼ã¸ã¡ã³ãã¨ä½é¨ãåä¸ããããã¨ãã§ãã¾ãã
ãã ããåã«ç»åã追å ããã ãã§ã¯ååã§ã¯ããã¾ãããç»åãæé©åãããã¨ãéè¦ã§ãã
ãªããªãã大ããªãµã¤ãºã®æé©åããã¦ããªãç»åã¯ãã¦ã§ããµã¤ãã®èªã¿è¾¼ã¿é度ãé ãããã¦ã¼ã¶ã¼ã¨ã¯ã¹ããªã¨ã³ã¹ãã³ã³ãã¼ã¸ã§ã³çã«æªå½±é¿ãåã¼ãå¯è½æ§ãããããã§ãã
ãã®ãããåãããç»åæé©åã«æ³¨åãããã¨ãä¸å¯æ¬ ã§ããç¹ã«Next.jsã使ç¨ãã¦ããå ´åã¯ãImageã³ã³ãã¼ãã³ããå©ç¨ãã¦å¹ççãªNext.jsç»åæé©åãå®ç¾ãããã¨ãã§ãã¾ãã
ãããããªããNext.jsãå©ç¨ãã¦ããã¢ããªãéçºãã¦ããå ´åãæ¬è¨äºã§ã¯å¹æçãªNext.jsç»åæé©åã®æ¹æ³ã«ã¤ãã¦è§£èª¬ãã¾ãã
ãã£ããã¨æé©åãè¡ããã¦ã§ããµã¤ããç°ãªãããã¤ã¹ï¼PCãã©ããããããã¹ãã¼ããã©ã³ãªã©ï¼ã§ãå¿çæ§ãä¿ã¤ãã¨ãã§ããããã«ãã¾ãããã
èªã¿é²ãããã¨ã§ãå¹ççãªç»åæé©åã®ææ³ãå¦ã¶ãã¨ãã§ãã¾ãã
åå¿è åãã«åºæ¬çãªç»å表示æ¹æ³ãããç»åæé©åã«é¢ããããã¤ãã®ãåé¡ãæ¤è¨ããããã¦åç´ãªImageã³ã³ãã¼ãã³ãã使ç¨ãã¦ãããã®åé¡ã解決ãã¦ãããã¹ããã©ã¯ãã£ã¹ã説æè´ãã¾ãã
æé©åãããç»åå¦ç
Vercelã®Next.jsãã¼ã ã¯ã2020å¹´ã«æ°ããImageã³ã³ãã¼ãã³ããå°å ¥ãã¾ããã
Imageã³ã³ãã¼ãã³ãã¯ãNext.jsã«çµã¿è¾¼ã¾ãã¦ããæ¹è¯çã®imgã¿ã°ã§ãããããã¤ãã®ä¾¿å©ãªæ©è½ã¨ããã©ã¼ãã³ã¹ä¸ã®å©ç¹ãæä¾ãã¦ãã¾ãã
ããã«ã¯ã次ã®ãããªãSUPERPOWERSããããã¾ãã
ã¾ããImageã³ã³ãã¼ãã³ãã¯ç»åã®æé©åãèªåçã«è¡ãã¾ããç»åãµã¤ãºãé©åã«æé©åãããã¨ã§ãããã©ã¼ãã³ã¹ãåä¸ãã¾ãã
ã¾ãããã¾ãã¾ãªããã¤ã¹ãç»é¢ãµã¤ãºã«åããã¦ãå¿ è¦ãªè§£å度ã®ç»åãèªåçã«æä¾ãã¾ãã
次ã«ãImageã³ã³ãã¼ãã³ãã¯ã¬ã¤ã¸ã¼ãã¼ãã£ã³ã°ï¼é 延èªã¿è¾¼ã¿ï¼ããµãã¼ããã¦ãã¾ãã
ç»åã¯ãç»é¢ä¸ã«è¡¨ç¤ºãããç´åã¾ã§èªã¿è¾¼ã¾ãããã¹ã¯ãã¼ã«ããã©ã¼ãã³ã¹ãåä¸ãã¾ããããã«ããããã¼ã¸ã®èªã¿è¾¼ã¿é度ãé«éåããã¾ãã
ããã«ãImageã³ã³ãã¼ãã³ãã¯ãã©ã¦ã¶ã®ãã£ãã·ã¥ãæ´»ç¨ãã¾ãã
çæãããåç»åãµã¤ãºã¯ããã©ã¦ã¶ã®ãã£ãã·ã¥ã«ä¿åããã¾ãã
次å以éãåããµã¤ãºã®ç»åãè¦æ±ãããå ´åã«ã¯ããµã¼ãã¼ã«ãªã¯ã¨ã¹ããéããã¨ãªããã©ã¦ã¶ããç´æ¥èªã¿è¾¼ããã¨ãã§ãã¾ãã
ã¾ããImageã³ã³ãã¼ãã³ãã¯ãã¬ã¼ã¹ãã«ãã¼ã®ãµãã¼ããæä¾ãã¦ãã¾ãã
ç»åãèªã¿è¾¼ã¾ããã¾ã§ã®éããã¬ã¼ã¹ãã«ãã¼ã表示ããã¾ããããã«ãããç»åã®èªã¿è¾¼ã¿ä¸ã«ã¦ã¼ã¶ã¼ã¨ã¯ã¹ããªã¨ã³ã¹ãåä¸ããããã¨ãã§ãã¾ãã
ãããã®æ©è½ã¯ãããã©ã¼ãã³ã¹ã®æé©åãã¦ã¼ã¶ã¼ã¨ã¯ã¹ããªã¨ã³ã¹ã®åä¸ã«å½¹ç«ã¡ã¾ãã
Imageã³ã³ãã¼ãã³ãã¯Next.jsã®ä¸é¨ã§ãããNext.jsããã¸ã§ã¯ãã§ç°¡åã«å©ç¨ãããã¨ãã§ãã¾ãã
ç»åã®è¡¨ç¤ºæ¹æ³
Next.jsã§ã¯ãç»åã表示ããããã«<Image>
ã³ã³ãã¼ãã³ãã使ç¨ãã¾ãããã®ã³ã³ãã¼ãã³ãã¯ãç»åã®æé©åã¨é
延èªã¿è¾¼ã¿ãèªåçã«å¦çãã¾ãã
Next 13ã§ã¯ãImageã³ã³ãã¼ãã³ãã«å¤ãã®æ°æ©è½ã追å ããã¦ããã以åã®ãã¼ã¸ã§ã³ã¨å¤å°å¤æ´ç¹ãããã¾ãã
以åã§ã¯ãç»åã表示ãããNext.jsã®ãã¼ã¸ãã³ã³ãã¼ãã³ãã§ãnext/image
ã¢ã¸ã¥ã¼ã«ãã¤ã³ãã¼ããã¾ããããnext/imageã§ã¯ãªããnext/legacy-image
ã使ç¨ããå¿
è¦ãããã¾ãã
import Image from 'next/legacy-image'
ã¾ããnext/legacy-image
ã使ç¨ããå ´åãæ°ããæ©è½ï¼ä¾ãã°ãpriority propãlayout propï¼ã¯ä½¿ç¨ã§ããªãããã注æãå¿
è¦ã§ãã
next/legacy-image
ã¯ãæ°ããæ©è½ããµãã¼ããã¦ããªããããæ°ããImageã³ãã¼ãã³ãã«ç§»è¡ããå¿
è¦ãããã¾ãã
ãã®ãããNext.jsã«ã¯2ã¤ã®ã³ã¼ãã¢ãããç¨æããã¦ãã¾ãã
next-image-to-legacy-image
ãã®ã³ã¼ãã¢ããã¯ãnext/imageã®ã¤ã³ãã¼ããèªåçã«next/legacy/imageã«ãªãã¼ã ããNext.js 12ã¨åãåä½ãç¶æãã¾ãã
ããã«ãããNext.js 13ã«è¿ éã«èªåæ´æ°ãããã¨ãã§ãã¾ãã
以ä¸ã¯ãnext-image-to-legacy-imageã®å®è¡ä¾ã§ãã
npx next-image-to-legacy-image
next-image-experimental
ãã®ã³ã¼ãã¢ããã¯ãnext-image-to-legacy-imageãå®è¡ããå¾ãä»»æã§å®è¡ãããã¨ãã§ãã¾ãã
ãã®ã³ã¼ãã¢ããã«ããã/legacy/image
ãæ°ããnext/image
ã«ã¢ããã°ã¬ã¼ããããã¨ãã§ãã¾ãã
ãã ãããã®ã³ã¼ãã¢ããã¯å®é¨çã§ãããåçãªä½¿ç¨ã«ã¯å¯¾å¿ãã¦ãã¾ããã
以ä¸ã¯ãnext-image-experimental
ã®å®è¡ä¾ã§ãã
npx next-image-to-legacy-image npx next-image-experimental
next-image-experimental
ã¯ãnext/legacy/image
ãæ°ããnext/image
ã«ããã°ã¬ã¼ãããããã®å®é¨çãªã³ã¼ãã¢ããã§ãããåçãªä½¿ç¨ã«ã¯å¯¾å¿ãã¦ããªããããåå¿è
ã«ã¯å°ãè¤éããããã¾ããã
ã§ãã®ã§æ¬æ¥ã§ã¯ããã¢ã³ã¹ãã¬ã¼ã·ã§ã³ã®çºã«ãnext-image-to-legacy-image
ã«ãã¾ãããã
ã¤ã¾ããåºæ¬çã«ã¯Next.js 13ã§Imageã³ã³ãã¼ãã³ãã使ç¨ããå ´åã¯ãã¾ãnext-image-to-legacy-image
ãå®è¡ãã¦ãnext/image
ã®ã¤ã³ãã¼ããèªçã«next/legacy/image
ã«ãªãã¼ã ããå¿
è¦ãããã¾ãã
ããã«ãããNext.js 12ã¨åãããã«åä½ããã¨ãã§ãã¾ãã
Next.js 12ã¨åãããã«åä½ãããããæ¯è¼çç°¡åã«å®è¡ã§ãã¾ãã
以ä¸ã®ã³ãã³ããå®è¡ãã¦ãnext-image-to-legacy-image
ãå®è¡ãã¾ãã
next-image-to-legacy-image
ããã«ãããnext/imageã®ã¤ã³ãã¼ããèªåçã«next/legacy/imageã«ãªãã¼ã ããã¾ãã
次ã«ã<Image>
ã³ã³ãã¼ãã³ãã使ç¨ãã¦ç»åã表示ãã¾ãã<Image>
ã³ã³ãã¼ãã³ãã«ã¯ãsrc
å±æ§ã¨alt
å±æ§ãå¿
è¦ã§ãã
import Image from 'next/legacy-image' <Image src="/path/to/image.jpg" alt="Image description" />
src
å±æ§ã«ã¯ç»åã®ãã¹ãæå®ãã¾ãã
é常ãNext.jsã§ã¯ã/publicããã£ã¬ã¯ããªå
ã®ç»åãã¡ã¤ã«ãåç
§ããããããã¹ã®å
é ã«/
ãä»ãã¾ãã
ä¾ãã°ãã/public/imagesããã£ã¬ã¯ããªå
ã«image.jpg
ãããå ´åãsrc
å±æ§ã«"/images/image.jpg"
ãæå®ãã¾ãã
alt
å±æ§ã«ã¯ç»åã®ä»£æ¿ããã¹ããæå®ãã¾ããããã¯ãç»åã表示ãããªãã£ããã¢ã¯ã»ã¹ä¸è½ãªå ´åã«ä»£ããã®æ
å ±ãæä¾ããããã®ãã®ã§ãã
ããã«ã
ããã«ãããç»åã®è¡¨ç¤ºãµã¤ãºãå¶å¾¡ã§ãã¾ãã
<Image src="/path/to/image.jpg" alt="Image description" width={500} height={300} />
ãã®ããã«ããã¨ãç»åã¯æå®ããå¹ ã¨é«ãã§è¡¨ç¤ºããã¾ãã
width
ã¨height
ããããã£ã¯ãã¯ã»ã«åä½ã§æå®ãã¾ãã
<Image>
ã³ã³ãã¼ãã³ãã¯ç»åã®æé©åã¨é
延èªã¿è¾¼ã¿ãèªåçã«å¦çãããããããã©ã¼ãã³ã¹ã®åä¸ã«å½¹ç«ã¡ã¾ãã
ç»åã®ããªãã¼ãã£ã³ã°ã«ã¤ãã¦
ç»åã®ããªãã¼ãã£ã³ã°ã¯ãç»åã表示ãããåã«äºåã«èªã¿è¾¼ã¾ãããã¨ãæå³ãã¾ãã
ããã«ãããã¦ã¼ã¶ã¼ãç»åãè¦ãåã«ãã¼ãæéãçºçãããã¨ãªããã¹ã ã¼ãºãªã¦ã¼ã¶ã¼ã¨ã¯ã¹ããªã¨ã³ã¹ãæä¾ãããã¨ãã§ãã¾ãã
Next.jsã®next/imageã³ã³ãã¼ãã³ãã¯ãç»åã®ããªãã¼ãã£ã³ã°ãèªåçã«è¡ã£ã¦ããã¾ãã
éçºè ãæåã§ããªãã¼ãã£ã³ã°ãå®è£ ããå¿ è¦ã¯ããã¾ããã
next/imageã³ã³ãã¼ãã³ãã¯ãç»åãç»é¢å ã«è¡¨ç¤ºãããåã«éåæã§èªã¿è¾¼ã¾ãã¾ãã
ãã©ã¦ã¶ã¯ç»åãããã¯ã°ã©ã¦ã³ãã§èªã¿è¾¼ããããã¦ã¼ã¶ã¼ãç»åãè¦ãåã«ãã¼ãæéãçºçãããã¨ã¯ããã¾ããã
ãã®ä»çµã¿ã«ãããã¹ã ã¼ãºãªã¦ã¼ã¶ã¼ã¨ã¯ã¹ããªã¨ã³ã¹ãå®ç¾ãããã¨ãã§ãã¾ãã
ãããã£ã¦ãNext.jsã使ç¨ãã¦ç»åã表示ããå ´åãnext/imageã³ã³ãã¼ãã³ãã使ç¨ããã ãã§ãç»åã®ããªãã¼ãã£ã³ã°ã«é¢ããæåã®å®è£ ã¯å¿ è¦ããã¾ããã
next/imageã³ã³ãã¼ãã³ãã¯ãç»åã®æé©åãã¬ã¹ãã³ã·ããªè¡¨ç¤ºãããªãã¼ãã£ã³ã°ãªã©ãç»åé¢é£ã®æè¯ã®ãã©ã¯ãã£ã¹ãèªåçã«å¦çãã¾ãã
æé©åãªãã·ã§ã³
layoutããã³quality
Next.jsã®<Image>
ã³ã³ãã¼ãã³ãã¯ãç»åã®è¡¨ç¤ºããã©ã¼ãã³ã¹ãåä¸ãããããã®æé©åãªãã·ã§ã³ãæä¾ãã¦ãã¾ãã
ä¾ãã°ãlayout
å±æ§ã使ç¨ãã¦ç»åã®ã¬ã¤ã¢ã¦ããæå®ããããquality
å±æ§ã使ç¨ãã¦ç»åã®å質ï¼å§ç¸®çï¼ã調æ´ããããããã¨ãã§ãã¾ãã
ãã ããlayoutããããã£ãqualityããããã£ã¯ãnext/legacy-imageã®<Image>
ã³ã³ãã¼ãã³ãã§ã¯ä½¿ç¨ã§ãã¾ããã
ãããã®ããããã£ã¯ãæ°ããnext/image
ã³ã³ãã¼ãã³ãã§ã®ã¿ä½¿ç¨ã§ãã¾ãã
ãã®æ°ããæ©è½ã使ç¨ããã«ã¯ãnext-image-experimentalãå®è¡ãã¦ãnext/imageã³ã³ãã¼ãã³ãã«ç§»è¡ããå¿ è¦ãããã¾ãã
<Image src="/path/to/image.jpg" alt="Image description" layout="responsive" quality={80} />
qualityã¯åºæ¬çã«ãæé©åãããç»åã®ç»è³ªã決å®ããæ´æ°ã¨ãªãã¾ãã
ãã®ãã©ã¡ã¼ã¿ã®å¤ã®ç¯å²ã¯ã1 ï½ 100ãã§ã75ãããã©ã«ãè¨å®ã¨ãªã£ã¦ãã¾ãã
ãã詳細ãªãªãã·ã§ã³ã«ã¤ãã¦ã¯ãNext.jsã®å ¬å¼ããã¥ã¡ã³ããåç §ãã¦ãã ããã
sizes
Next.jsã®Imageã³ã³ãã¼ãã³ãã«ã¯sizes
ãªãã·ã§ã³ãããã¾ãã
ããã«ãããç»åã®ã¬ã¹ãã³ã·ããªãµã¤ãºæå®ãè¡ããã¨ãã§ãã¾ãã
sizesãªãã·ã§ã³ã¯ã<Image>
ã³ã³ãã¼ãã³ãã®layoutããããã£ãresponsive
ã«è¨å®ããã¦ããå ´åã«ä½¿ç¨ããã¾ãã
sizesãªãã·ã§ã³ã¯ããã©ã¦ã¶ã«ç»åã®è¡¨ç¤ºãµã¤ãºãä¼ããé©åãªã¤ã¡ã¼ã¸ãµã¤ãºãé¸æããããã®ãã³ããæä¾ãã¾ãã
以ä¸ã«ãNext.jsã®Imageã³ã³ãã¼ãã³ãã§sizes
ãªãã·ã§ã³ã使ç¨ããä¾ã示ãã¾ãã
<Image src="/path/to/image.jpg" alt="Image" layout="responsive" sizes="(max-width: 768px) 100vw, 50vw" />
ä¸è¨ã§ã¯ãlayoutããããã£ãresponsiveã«è¨å®ããã¦ãããsizesãªãã·ã§ã³ã«ãµã¤ãºæå®ã®è¦åãæå®ãã¦ãã¾ãã
sizesãªãã·ã§ã³ã¯ã¡ãã£ã¢ã¯ã¨ãªã¨ç»åãµã¤ãºã®é¢ä¿ã表ç¾ãããã©ã¦ã¶ã«å¯¾ãã¦é©åãªç»åãµã¤ãºãé¸æãããã³ããæä¾ãã¾ãã
<Image src="/path/to/image.jpg" alt="Image" layout="fill" sizes="(min-width: 60em) 24vw, (min-width: 28em) 45vw, 100vw" quality={80} />
ä¸è¨ã§ã¯ãlayoutããããã£ãfill
ã«è¨å®ããsizesãªãã·ã§ã³ã§ç°ãªããã¥ã¼ãã¼ããµã¤ãºã«å¯¾ãã¦ç»åã®è¡¨ç¤ºãµã¤ãºãæå®ãã¦ãã¾ããã¾ããqualityãªãã·ã§ã³ã«ã¯å質ã®å¤ã¨ãã¦80
ãæå®ãã¦ãã¾ãã
ããã«ãããç»åã¯è¦ªè¦ç´ ã®ãµã¤ãºãã£ã±ãã«è¡¨ç¤ºããããã¥ã¼ãã¼ããµã¤ãºã«å¿ãã¦é©åãªç»åãµã¤ãºãé¸æããã¾ãã
ãã ãã注æç¹ã¨ãã¦ãlayout="fill"
ã使ç¨ããå ´åã親è¦ç´ ã«å¯¾ãã¦ç¸å¯¾çãªãµã¤ãºæå®ãå¿
è¦ã¨ãªãã¾ãã
å ·ä½çã«ã¯ã親è¦ç´ ã«é©åãªå¹ ã¨é«ãã®ã¹ã¿ã¤ã«ãè¨å®ãããã親è¦ç´ ããµã¤ãºãæã¤ã¤ã³ã©ã¤ã³ã¾ãã¯ãããã¯è¦ç´ ã§ããå¿ è¦ãããã¾ãã
Next/Imageé«åº¦ãªProps
< Image />
ã³ã³ãã¼ãã³ãã¯ãç»åã®åä½ãå¤æ´ããããã«ä½¿ç¨ã§ããé«åº¦ãªããããã£ãæä¾ãã¾ãã
ããããç´¹ä»è´ãã¾ãã
ç»åã®placeholder
ç»åãã¾ã èªã¿è¾¼ã¾ãã¦ããªãå ´åããèªã¿è¾¼ã¿ä¸ã®å ´åã«ãã¬ã¼ã¹ãã«ãã¼ã表示ãããã¨ã§ãã¦ã¼ã¶ã¼ã«è¦è¦çãªãã£ã¼ãããã¯ãæä¾ãããã¨ãã§ãã¾ããNext.jsã®<Image>
ã³ã³ãã¼ãã³ãã¯ãplaceholderå±æ§ãæå®ãã¦ãã¬ã¼ã¹ãã«ãã¼ç»åã表示ããæ©è½ãæä¾ãã¦ãã¾ãã
<Image src="/path/to/image.jpg" alt="Image description" placeholder="blur" />
placeholderå±æ§ã«ã¯ãblurãemptyãªã©ã®å¤ãæå®ã§ãã¾ãã
objectPosition
Next.jsã®Imageã³ã³ãã¼ãã³ãã®objectPositionããããã£ã¯ãç»åã親ã³ã³ããå ã§ã©ã®ä½ç½®ã«é ç½®ããããæå®ããããã®ãã®ã§ãã
ãã®ããããã£ã¯ã親ã³ã³ããå ã§ã®æ°´å¹³æ¹åã¨åç´æ¹åã®ä½ç½®ããã¯ã»ã«ã¾ãã¯ãã¼ã»ã³ãã¼ã¸ã§æå®ãããã¨ãã§ãã¾ãã
objectPositionããããã£ã«ã¯ã以ä¸ã®ãããªå¤ãæå®ãããã¨ãã§ãã¾ã:
ã» "top": ç»åã親ã³ã³ããã®ä¸ç«¯ã«é ç½®ãã¾ãã
ã» "bottom": ç»åã親ã³ã³ããã®ä¸ç«¯ã«é ç½®ãã¾ãã
ã» "left": ç»åã親ã³ã³ããã®å·¦ç«¯ã«é ç½®ãã¾ãã
ã» "right": ç»åã親ã³ã³ããã®å³ç«¯ã«é ç½®ãã¾ãã
ã» "center": ç»åã親ã³ã³ããã®ä¸å¤®ã«é ç½®ãã¾ãã
ã» "top left": ç»åã親ã³ã³ããã®å·¦ä¸ã«é ç½®ãã¾ãã
ã» "top right": ç»åã親ã³ã³ããã®å³ä¸ã«é ç½®ãã¾ãã
ã» "bottom left": ç»åã親ã³ã³ããã®å·¦ä¸ã«é ç½®ãã¾ãã
ã» "bottom right": ç»åã親ã³ã³ããã®å³ä¸ã«é ç½®ãã¾ãã
ã» ãã¯ã»ã«ã¾ãã¯ãã¼ã»ã³ãã¼ã¸å¤: ç»åã親ã³ã³ããå ã®ç¹å®ã®ä½ç½®ã«é ç½®ãã¾ããä¾ãã°ãã"50% 50%"ãã§ã¯ç»åã親ã³ã³ããã®ä¸å¤®ã«é ç½®ãã¾ãã
import Image from 'next/image'; const MyComponent = () => { return ( <div style={{ width: '500px', height: '300px', position: 'relative' }}> <Image src="/path/to/image.jpg" alt="Image" width={500} height={300} objectFit="cover" objectPosition="top right" /> </div> ); }; export default MyComponent;
ä¸è¨ã§ã¯ã親ã³ã³ããã®å¹
ã¯500pxãé«ãã¯300pxã«è¨å®ããã¦ãã¾ããobjectFit
ããããã£ã¯cover
ã«è¨å®ããã¦ãããç»åã親ã³ã³ããã«ãã£ããããããã«æ¡å¤§ã¾ãã¯ç¸®å°ããã¾ãã
ã¾ããobjectPositionããããã£ã¯top rightã«è¨å®ããã¦ãã¾ãã
ããã«ãããç»åã親ã³ã³ããã®å³ä¸ã«é ç½®ããã¾ãã
ã¾ããä¸è¨ã®ä¾ã§ã¯ã¤ã³ã©ã¤ã³ã¹ã¿ã¤ã«ã使ç¨ãã¦è¦ªã³ã³ããã®ãµã¤ãºã¨ä½ç½®ãæå®ãã¦ãã¾ãããå®éã®ã¢ããªã±ã¼ã·ã§ã³ã§ã¯CSSã¯ã©ã¹ãå¥ã®æ¹æ³ã使ç¨ãã¦ã¹ã¿ã¤ãªã³ã°ãããã¨ãã§ãã¾ãã
objectPositionããããã£ã使ç¨ãããã¨ã«ãããè¦è¦çãªã¬ã¤ã¢ã¦ãããã¶ã¤ã³ä¸ã®è¦ä»¶ã«åããã¦ç»åãé ç½®ãããã¨ãã§ãã¾ãã
loading
Next.jsã®Imageã³ã³ãã¼ãã³ãã®loadingããããã£ã¯ãç»åã®èªã¿è¾¼ã¿ä¸ã«è¡¨ç¤ºããããã¼ãã£ã³ã°ç¶æ ãå¶å¾¡ããããã®ãã®ã§ãã
ãã®ããããã£ã«ã¯ä»¥ä¸ã®3ã¤ã®å¤ãæå®ãããã¨ãã§ãã¾ãã
ã» "lazy": ç»åã¯é 延èªã¿è¾¼ã¿ããã¾ããç»åãç»é¢å ã«è¡¨ç¤ºãããç´åã«èªã¿è¾¼ã¿ãéå§ãããç»åã表示ãããã¾ã§ãã¼ãã£ã³ã°ç¶æ ã表示ããã¾ãã
ã» "eager": ç»åã¯å³åº§ã«èªã¿è¾¼ã¾ãã¾ããç»åã®èªã¿è¾¼ã¿ãéå§ãããã¨ããã«è¡¨ç¤ºããã¾ãã
ã» "unspecified": æ¢å®å¤ã§ããã©ã¦ã¶ã®ããã©ã«ãã®èªã¿è¾¼ã¿åä½ã«å¾ãã¾ãã
以ä¸ã«ãloadingããããã£ã使ç¨ããä¾ã示ãã¾ãã
<Image src="/path/to/image.jpg" alt="Image" width={500} height={300} loading="lazy" />
ä¸è¨ã§ã¯ãloadingããããã£ã«"lazy"
ãæå®ãã¦ãã¾ããããã«ãããç»åã¯é
延èªã¿è¾¼ã¿ãããç»åãç»é¢å
ã«è¡¨ç¤ºãããç´åã«èªã¿è¾¼ã¿ãéå§ããã¾ãã
注æç¹ã¨ãã¦ãloadingããããã£ã¯Next.jsã®Imageã³ã³ãã¼ãã³ãã§ã®ã¿ä½¿ç¨ã§ãã¾ããé常ã®<img>
ã¿ã°ã§ã¯ä½¿ç¨ã§ãã¾ããã
blurDataURL
blurDataURLã¯ãNext.jsã®Imageã³ã³ãã¼ãã³ãã«ãã£ã¦çæãããç»åã®ã¼ãããã¼ã¿URLï¼Data URLï¼ãæãã¾ãã
ã¼ãããã¼ã¿URLã¯ãå ã®ç»åã®ã¼ãããã¼ã¸ã§ã³ã表ãç»åãã¼ã¿ã®ã¨ã³ã³ã¼ããããæååã§ãã
Next.jsã®Imageã³ã³ãã¼ãã³ãã§ã¯ããã©ã¦ã¶ã«ããã¬ã¤ã¢ã¦ãã·ããï¼layout shiftï¼ãé²ããããç»åã®èªã¿è¾¼ã¿åã«ã¼ãããã¼ã¸ã§ã³ã表示ãããã¨ãã§ãã¾ãããã®ã¼ãããã¼ã¸ã§ã³ã¯ãå ã®ç»åã®å¤å½¢ããµã¤ãºãä¿æããªããããã¼ãã£ã³ã°ä¸ã®ãã¬ã¼ã¹ãã«ãã¼ã¨ãã¦ä½¿ç¨ããã¾ãã
ã¼ãããã¼ã¿URLã¯ãå ã®ç»åããã©ã¦ã¶ä¸ã§ã¼ããã¦çæããã¾ãã
ããã«ãããå ã®ç»åã®èªã¿è¾¼ã¿ãå®äºããåã«ãã¼ãããã¼ã¸ã§ã³ã表示ãããã¨ãã§ãã¾ããç»åã®ã¼ããå¦çã¯ãã©ã¦ã¶ä¸ã§è¡ããããããéä¿¡ã®å¾ ã¡æéãçºçãããã¦ã¼ã¶ã¼ã«ããè¯ãã¦ã¼ã¶ã¼ã¨ã¯ã¹ããªã¨ã³ã¹ãæä¾ãããã¨ãã§ãã¾ãã
以ä¸ã«ãNext.jsã®Imageã³ã³ãã¼ãã³ãã§blurDataURL
ã使ç¨ããä¾ã示ãã¾ãã
const MyComponent = () => { const blurDataURL = '/path/to/blur-image.jpg'; return ( <div> <Image src="/path/to/image.jpg" alt="Image" width={500} height={300} placeholder="blur" blurDataURL={blurDataURL} /> </div> ); };
ä¸è¨ã§ã¯ãplaceholderããããã£ã«"blur"
ãæå®ãã¦ãã¾ããã¾ããblurDataURLããããã£ã«ã¯ã¼ãããã¼ã¿URLã®ãã¹ãæå®ãã¦ãã¾ãã
ã¼ãããã¼ã¿URLã¯é常ããµã¼ãã¼ãµã¤ãã§åçã«çæããããã¼ã¸ã®ååã¬ã³ããªã³ã°æã«æä¾ããã¾ãããã®ããã«ãããã¨ã§ãç»åã®ã¼ãããã¼ã¸ã§ã³ãæ©æã«è¡¨ç¤ºãããã¦ã¼ã¶ã¼ã«ãã¼ãã£ã³ã°ç¶æ ãè¦è¦çã«ä¼ãããã¨ãã§ãã¾ãã
Style
Next.jsã®Imageã³ã³ãã¼ãã³ãã§ã¯ãstyle
ããããã£ã使ç¨ãã¦ç»åã«ã¹ã¿ã¤ã«ãé©ç¨ãããã¨ãã§ãã¾ãã
styleããããã£ã使ç¨ããã¨ãCSSã¹ã¿ã¤ã«ãªãã¸ã§ã¯ããæå®ãã¦ç»åã®ã¹ã¿ã¤ãªã³ã°ãã«ã¹ã¿ãã¤ãºãããã¨ãã§ãã¾ãã
ã¾ãããã®ããããã£ã«ã¯ããã£ã¡ã«ã±ã¼ã¹ã§ã¹ã¿ã¤ã«ããããã£åãæå®ããããã«å¯¾å¿ããå¤ãè¨å®ãã¾ãã
以ä¸ã«ãNext.jsã®Imageã³ã³ãã¼ãã³ãã§styleããããã£ã使ç¨ããä¾ã示ãã¾ãã
import Image from 'next/image'; const MyComponent = () => { const imageStyle = { width: '300px', height: '200px', borderRadius: '8px', boxShadow: '0 2px 4px rgba(0, 0, 0, 0.2)', }; return ( <div> <Image src="/path/to/image.jpg" alt="Image" width={500} height={300} style={imageStyle} /> </div> ); }; export default MyComponent;
ä¸è¨ã§ã¯ãstyleããããã£ã«ã¹ã¿ã¤ã«ããããã£åã¨å¤ã®ãã¢ãæã¤JavaScriptãªãã¸ã§ã¯ãimageStyle
ãæå®ãã¦ãã¾ãã
imageStyle
ãªãã¸ã§ã¯ãã«ã¯ãç»åã®å¹
ã»é«ãããã¼ãã¼åå¾ãããã¯ã¹ã·ã£ãã¦ãªã©ã®ã¹ã¿ã¤ã«ãæå®ãã¦ãã¾ãã
styleããããã£ã«æå®ã§ããã¹ã¿ã¤ã«ããããã£ã¯CSSã¨åæ§ã§ããããã£ã¡ã«ã±ã¼ã¹ã®ããããã£åã使ç¨ããå¤ã¯æååã¨ãã¦æå®ãã¾ãã
ã¾ããCSSã®å¤ã¨åæ§ã«åä½ï¼pxãremã%ãªã©ï¼ãæå®ãããã¨ãã§ãã¾ãã
ãã®ããã«ãstyleããããã£ã使ç¨ãããã¨ã§ãNext.jsã®Imageã³ã³ãã¼ãã³ãã«ã«ã¹ã¿ã ã®ã¹ã¿ã¤ãªã³ã°ãé©ç¨ãããã¨ãã§ãã¾ãã
é©åãªç»åãã©ã¼ãããã®é¸æ
ç»åãã©ã¼ãããã¯ãç¹å®ã®ä½¿ç¨ã±ã¼ã¹ã«æé©ãªãã®ãé¸æããå¿ è¦ãããã¾ãã
ä¸è¬çãªç»åãã©ã¼ãããã«ã¯ãJPEGãPNGãWebPãªã©ãããã¾ãã
ã» JPEGï¼ åçãã«ã©ãã«ãªç»åã«æé©ãªãã©ã¼ãããã§ããJPEGã¯å§ç¸®çã調æ´ã§ãããããç»è³ªã¨ãã¡ã¤ã«ãµã¤ãºã®ãã©ã³ã¹ã調æ´ãããã¨ãã§ãã¾ãã
ã» PNGï¼éæ度ãã°ã©ãã£ãã¯ã®å質ãéè¦ãªå ´åã«ä½¿ç¨ããã¾ããPNGã¯éå¯éå§ç¸®ã使ç¨ãããããç»è³ªãç¶æããªãããã¡ã¤ã«ãµã¤ãºã大ãããããã¨ãããã¾ãã
ã» WebPï¼Googleãéçºããæ°ããç»åãã©ã¼ãããã§ãããJPEGã¨PNGã®å©ç¹ãçµã¿åããã¦ãã¾ããWebPã¯é«å§ç¸®çã¨é«å質ãæä¾ããä¸è¬çã«ã¯JPEGãPNGãããå°ããªãã¡ã¤ã«ãµã¤ãºã§åçã®å質ãå®ç¾ã§ãã¾ãããã ããä¸é¨ã®å¤ããã©ã¦ã¶ã§ã¯ãµãã¼ãããã¦ããªãå ´åãããã¾ãã
ã¾ã¨ãã¾ãã¨ãJPEGã¯åçãã¤ã¡ã¼ã¸ã«é©ãã¦ãããPNGã¯éæ度ãå¿ è¦ã¨ããå ´åã«ä½¿ç¨ãã¾ããWebPã¯é«å§ç¸®çã¨é«å質ãæä¾ããã¢ãã³ãªãã©ã¦ã¶ã§ãµãã¼ãããã¦ãã¾ãã
æå¾ã«
Next/Imageã³ã³ãã¼ãã³ãã¯ãNextJSã§ç»åãæé©åããããã®ä¾¿å©ãªãã¼ã«ã§ãã
ãã®ã³ã³ãã¼ãã³ãã使ç¨ããã¨ãã¬ã¤ã¢ã¦ãããã¬ã¼ã¹ãã«ãã¼ããµã¤ãºãªã©ãç»åãå¹æçã«æé©åããããã®ãã¾ãã¾ãªçµã¿è¾¼ã¿æ©è½ãå©ç¨ãããã¨ãã§ãã¾ãã
ã¬ã¤ã¢ã¦ãæ©è½ã¯ãç»åãé©åãªä½ç½®ããµã¤ãºã§è¡¨ç¤ºããããã«å½¹ç«ã¡ã¾ãããã¬ã¼ã¹ãã«ãã¼æ©è½ã¯ãç»åã®èªã¿è¾¼ã¿ä¸ã«ãã¬ã¼ã¹ãã«ãã¼ç»åã表示ãããã¨ã§ãã¦ã¼ã¶ã¼ã«å¾ æ©æéãæå°éã«æãããã¨ãã§ãã¾ãã
ã¾ãããµã¤ãºæ©è½ã¯ãç»åã®è§£å度ããã©ã¼ããããèªåçã«æé©åãããã¼ã¸ã®èªã¿è¾¼ã¿é度ãåä¸ãããã®ã«å½¹ç«ã¡ã¾ãã
æ¬æ¥ã§è§£èª¬ãã¦ããããã¹ããã©ã¯ãã£ã¹ãé©ç¨ãããã¨ã§ãNext.jsã§ã®ç»åã®ããã©ã¼ãã³ã¹ã¨å質ãæé©åãããã¨ãã§ãã¾ãã
ç»åã®é©åãªãã©ã¼ãããã®é¸æãå§ç¸®ã¨æé©åãã¬ã¹ãã³ã·ããªè¡¨ç¤ºãããã¦ããªãã¼ãã£ã³ã°ã¯ãã¦ã¼ã¶ã¼ã¨ã¯ã¹ããªã¨ã³ã¹ãåä¸ãããããã«éè¦ãªè¦ç´ ã§ãã
ã¾ããNext/Imageã³ã³ãã¼ãã³ãã¯ãã¦ã¼ã¶ã¼ã¨ã¯ã¹ããªã¨ã³ã¹ãåä¸ãããã ãã§ãªããã¦ã§ããµã¤ãã®ããã©ã¼ãã³ã¹ãæé©åããã®ã«å½¹ç«ã¤ãã¨ãã§ãã¾ãã
éçºè ã¨ãã¦ãNext/Imageã³ã³ãã¼ãã³ãã使ãããªããã¨ã§ãã¦ã¼ã¶ã¼ã«ããåªããã¦ã§ããµã¤ãä½é¨ãæä¾ãããã¨ãã§ãã¾ãã
æ¬æ¥ã¯ä»¥ä¸ã¨ãªãã¾ãã
æå¾ã¾ã§èªãã§é ããããã¨ããããã¾ãã
ããããã®è¨äºãå½¹ã«ç«ã£ãããããã¯ãã¼ã¯ã¨å ±æããã¦ããã ããã¨å¹¸ãã§ãã