16.0 Introduction
next.js에서는 img 태그 쓰지마라.
<Image> 써라.
import Image from 'next/image'
import profilePic from '../public/me.png'
function Home() {
return (
<>
<h1>My Homepage</h1>
<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
/>
<p>Welcome to my homepage!</p>
</>
)
}
이미지 import해서 변수처럼 쓰면된다.
https://nextjs.org/docs/basic-features/image-optimization
Improved Performance: Always serve correctly sized image for each device, using modern image formatsVisual Stability: Prevent Cumulative Layout Shift automatically
Faster Page Loads: Images are only loaded when they enter the viewport, with optional blur-up placeholdersAsset Flexibility: On-demand image resizing, even for images stored on remote server
16.1 Local Images
local image와 remote image 다루는게 다르다.
blurry 버전의 이미지 기능은 local image만 가능. 파일을 갖고있어야 함.
_next/image API Handler로 이미지
http://127.0.0.1:3000/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Flocal.a1148338.jpg&w=3840&q=75
뒤의 Q는 퀄리티. 1~100까지.
blurry 이미지 적용하기 = placeholder에 blur적으면 됨.
<Image src={myImage} placeholder="blur" quality={100}/>
16.2 Remote Images
API로 받아오는 이미지 최적화 어떻게 하는지 알아보자.
nextjs는 이미지 서버를 물어본다.
next.config.js에 다음과 같이 작성.
imagedelivery.net은 cloudflare의 서버임.
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
images: {
domains: ["imagedelivery.net"],
},
};
module.exports = nextConfig;
근데 또 에러 뜸
Server Error
Error: Image with src "https://imagedelivery.net/0dNSIQsfAsyuUaSo7XjPgQ/undefined/public" is missing required "width" property.
16.3 Layout Fill
next.js는 이미지 사이즈를 알아야 한다. URL에 실제로 width넣을 필요 없음.
실제 화면에 보여질 사이즈만 알면 된다.
next.js는 내 서버에 이미지를 요청하고, 내가 태그에 적은 사이즈대로 이미지 생성 함.
그래서 실제 사용하는건 cloudflare에 있는게 아님.
로컬처럼 cloud 서비스 사용함.
https://nextjs.org/docs/api-reference/next/image#fill
<div className="relative pb-80">
<Image
src={`https://imagedelivery.net/0dNSIQsfAsyuUaSo7XjPgQ/${data?.product?.image}/public`}
className="h-96 bg-slate-300 object-sacle-down"
fill
/>
<h1 className="absolute w-full text-center text-red-500">Hi there</h1>
</div>
위처럼 relative주면 Image는 기본적으로 abolute라서 안보이게 할 수 있고,
위 태그 처럼 하면 배경이미지로 사용가능.
그리고 object-fit 사용하면 아주 좋음.
https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
object-fit - CSS: Cascading Style Sheets | MDN
The object-fit CSS property sets how the content of a replaced element, such as an <img> or <video>, should be resized to fit its container.
developer.mozilla.org
근데 next.js 13부터는 좀 바뀜
https://nextjs.org/docs/api-reference/next/legacy/image#layout
next/legacy/image | Next.js
Backwards compatible Image Optimization with the Legacy Image component.
nextjs.org
16.4 Conclusions:
remote image 작업할 경우.블러 효과 할 수 없음.
blurDataURL= 이런식으로 가능... 근데 굳이.
'클론코딩-캐럿마켓 > 전반' 카테고리의 다른 글
17 LIVE STREAMING, 18 Code Challenge (0) | 2023.01.13 |
---|---|
15 CLOUDFLARE IMAGES - 사진 다루기 (0) | 2023.01.11 |
14 Streams - seeding (0) | 2023.01.11 |
13 Profile (0) | 2023.01.09 |
12 동네생활 (0) | 2023.01.06 |