Using Images in Next.js (next/image examples)

  Рет қаралды 93,049

leerob

leerob

Күн бұрын

Learn how to use the Next.js Image Component (next/image) to automatically optimize images and prevent layout shift, helping provide a better user experience for your visitors.
0:00 - Introduction
0:34 - Using next/image
1:21 - Image component HTML output
2:00 - Image optimization
3:02 - Remote images
4:19 - Background images
5:03 - Image sizes and fill
6:29 - Image grid with fill
7:41 - Responsive images
8:19 - Image API reference and loaders
9:00 - Conclusion
◆ Docs: nextjs.org/docs/app/building-...
◆ API reference: nextjs.org/docs/app/api-refer...
◆ Example: github.com/vercel/next.js/tre...

Пікірлер: 111
@ala.garbaa
@ala.garbaa Жыл бұрын
Being responsive by default is what we have been waiting for 😊
@favouritejome
@favouritejome Жыл бұрын
Exactly ❤
@loveboat
@loveboat 11 ай бұрын
Too bad it isn't.
@aberba
@aberba 6 ай бұрын
CSS isn't responsive by default. None of the HTML elements are. It's that way for a reason
@waggadash
@waggadash 11 ай бұрын
Love it! Currently I am building my first SaaS product with NextJS ... and this 'optimization course' was exactly what I needed! Thank you Lee
@HungNguyen-tl9kg
@HungNguyen-tl9kg Жыл бұрын
Image component is cool, however: - I hope this video talks more about how "deviceSizes" and "sizes" work together - I currently find no way to remove the "blur" effect when using placeholder. In my case, I need to provide a solid default svg image. - The caching image url is pointed to the next server itself, it would be much better if we have a way to config this, maybe to point to a cdn url, to avoid DDOS, for example. - It will be awesome if there is a Picture component from next/picture, I need some behavior like switching different image urls in different breakpoints of screens. -> I think I should try to look into implementation of next/image to study the way it decide which width is the most suitable one with the screen, then create a Picture component myself. For anyone have solutions or ideas for these, I appreciate when you leave a comment 😍
@AsToNlele
@AsToNlele 11 ай бұрын
Always great content Lee, thank you!
@kevinbatdorf
@kevinbatdorf 11 ай бұрын
Very nice background set up and video production quality.
@ala.garbaa
@ala.garbaa Жыл бұрын
I am happy to see you posting new videos ❤ keep going 🎉
@yuriiholskyi9009
@yuriiholskyi9009 Жыл бұрын
Thanks, Lee. Excellent and useful examples. Like )
@kylemckell
@kylemckell Жыл бұрын
Very honestly, the Image component is one of the reasons why I really like Next. Image optimization is such a headache to think about, and having it built into my framework is easily one of the greatest possible things about it. There's no need for me to reach for Cloudinary or anything like that aside from storage.
@loveboat
@loveboat 11 ай бұрын
That's funny. I've never used the Image component because it's so confusing. Literally never managed to get an image working using it. So it just gives me more headaches.
@kylemckell
@kylemckell 11 ай бұрын
@@loveboat the old one was super confusing, but the new one is a lot better
@rockNbrain
@rockNbrain Жыл бұрын
Great job Lee! Tks 🎉
@juricablazevic9329
@juricablazevic9329 8 ай бұрын
Thank you so much! That was very helpful!!!
@lukas.webdev
@lukas.webdev Жыл бұрын
Great video, very valuable! 👍
@ericlindell3777
@ericlindell3777 Жыл бұрын
Great video! I’d like to see one about caching, both with appdir and pagedir.
@leerob
@leerob Жыл бұрын
kzfaq.info/get/bejne/nbmDpr2Dyq-qgp8.html
@PieterBos
@PieterBos 11 ай бұрын
Would love to see a picture tag implementation , using different images / aspects ratios on mobile / desktop
@ala.garbaa
@ala.garbaa Жыл бұрын
Amazing update !
@tdombui
@tdombui 6 ай бұрын
I love it when I find the solution to one of my problems while randomly scrubbing through coding videos
@user-pt6hf7xo3o
@user-pt6hf7xo3o Жыл бұрын
Great input
@sck3570
@sck3570 Жыл бұрын
the question is how to use blurDataUrl for dynamic images (remote images) 1. should we save the 10px image data in database OR 2. save the 10px image and use plaiceholder lib to generate the data of the image - it would be nice if blurDataUrl accepted the src of an small image instead of its data (so we dont have to use yet another lib)
@appstuff6565
@appstuff6565 8 ай бұрын
Thanks for this Lee! I was wondering how can i allow a 2mb image but during the upload itself it shrinks/compresses and THEN stores in the bucket at a much reduced size, saving some sorage space as well? Im using supabase. Thanks.
@kunaaal13
@kunaaal13 11 ай бұрын
Hey Lee, amazing video, just wanted to know what vscode theme are you using looks quite attractive 😅
@AHN1444
@AHN1444 11 ай бұрын
What happend with external images from a bucket? will the resize be made server side on the fly? or will store a cache for each image even if it is external?
@denilsoncosta9837
@denilsoncosta9837 11 ай бұрын
I think the next step for the image component is to set the width and height and generate the blur version without having to import the image first, just using the normal /[your public assets].
@kevinat71
@kevinat71 Жыл бұрын
great video!... what about svg icons with multiple icon sizes? should we use next/image for that?
@Victor-dd7el
@Victor-dd7el Жыл бұрын
SVG format is scalable by default, so you just need one icon for all the sizes. I would embbed SVG inline code as a React component instead of having a .svg file
@user-sc6dy3kj9w
@user-sc6dy3kj9w 2 ай бұрын
quick question, so its ok to put any value for width and height as long as you inline style with the maxwidth : 100% and height : auto properties when youre using remote images? nextjs will automatically avoid layout shifts and render with the aspect ratio? it looks like its doing that in my local project but im not definitely sure.
@EmmanuelOdii80
@EmmanuelOdii80 Жыл бұрын
Thanks mate! ❤
@leerob
@leerob Жыл бұрын
No problem!
@buddy.abc123
@buddy.abc123 11 ай бұрын
Nice! So we do we still need to install Sharp or not?
@canklc5772
@canklc5772 8 ай бұрын
crystal clear
@EricTsai-rj9vm
@EricTsai-rj9vm 10 күн бұрын
thanks a lot
@sergey_gabrielyan1001
@sergey_gabrielyan1001 4 ай бұрын
Hello. Is there any opportunity to have image optimization library for React JS like Next/Image, which way is common for plain React projects, to have CDN like Imagekit, Imgix or Cloudinary or there is another better way without CDN? I’m using Supabase storage in plain React app.
@merotuts9819
@merotuts9819 Жыл бұрын
What if the external image is hosted on a Protected route ? Can we supply Authorization Headers to Next/Image ?
@pepew7102
@pepew7102 Жыл бұрын
So when we import an image from the public directory we do not need to specify a width and a height or even use the fill prop ? But we need to use those props when the image is remote ?
@leerob
@leerob Жыл бұрын
The image width/height are automatically added when you import an image directly, yes. This works regardless if you use `fill` or not. When the image is remote, Next.js can't automatically determine that for you, so you have to add width/height to prevent layout shift.
@ala.garbaa
@ala.garbaa Жыл бұрын
Blur data URL is epic 👍
@lucaapa
@lucaapa 9 ай бұрын
hi , I am using images from the public folder but when I build and see the src and the browser downloads it does not compress them and leaves them in .jpeg or .png form and in theory I should optimize them what am I doing wrong? that's how I use them import Image from 'next/image'
@oOserkanCakmakOo
@oOserkanCakmakOo Жыл бұрын
Thank you
@sangeethreddychejerla6527
@sangeethreddychejerla6527 Жыл бұрын
Waiting for your video.
@StudioHetKanaal
@StudioHetKanaal Жыл бұрын
Would love to see how to handle anything else than happy paths with the new app router. Retries for example
@realintro7470
@realintro7470 10 ай бұрын
any idea on how to use videos as backgrounds in next js please?
@joshua_hc_cr
@joshua_hc_cr 7 ай бұрын
The image component in Next.js is particularly beneficial for individuals who want to avoid the complexities of handling various rendered sizes.
@janikaikkonen6384
@janikaikkonen6384 11 ай бұрын
Doing exactly like you did on the video, setting style={{maxWidth: "100%", height: "auto"}} is giving warning into console: "Image with src "abcdefg" has either width or height modified, but not the other. If you use CSS to change the size of your image, also include the styles 'width: "auto"' or 'height: "auto"' to maintain the aspect ratio." If I change maxWidth -> width I can get rid of the warning, but the layout explodes. I'm a bit confused...
@glow6026
@glow6026 4 ай бұрын
have you found a solution?
@greg.j
@greg.j 7 ай бұрын
Love all your videos on NextJS, and love NextJS and what you guys are doing atm @leerob but just had to point out, in the English language, “height” is not pronounced with the “th” sound at the end. It rhymes with words like “right” or “bite” 😅
@jazsouf
@jazsouf 9 ай бұрын
I was wondering why in the Image srcset generated when specifying the sizes property, all the sources have the same pixel dimensions ? Am I doing something wrong ? I would have guessed that the image's pixel dimensions would be different for each specified size
@manishbalhara9562
@manishbalhara9562 6 ай бұрын
I have my frontend on nextjs 13 using app router and backed is in python django. And i want to use Image component currently both frontend and backend is on locally. Can you guide me how i can use the Image component of nextjs?
@milovangudelj
@milovangudelj Жыл бұрын
Nice! Here's a question tho... Let's say I have two versions of an image, one for regular screens and one that's double in size for HiDPI screens, how can I tell Image to serve one over the other? The sizes prop isn't really letting me do that and I have to resort to using the regular img tag with the srcSet attribute and specify "/image.png, /image-2x.png 2x" for it to work properly.
@dariuskulevicius1640
@dariuskulevicius1640 Жыл бұрын
This is the main issue with Image component. The only way to do this is with deviceSizes/imageSizes in next.config.js. There are multiple issues registered regarding this and Vercel does not even try to solve this...
@Victor-dd7el
@Victor-dd7el Жыл бұрын
I have the same doubt. HTML element has the srcset attribute which works with sizes attribute to tell it which image file download depending on browser width. How can use the equivalent in Nextjs?
@Victor-dd7el
@Victor-dd7el Жыл бұрын
Maybe it is possible using a custom loader which generates the right URL depending on the browser width, but I am not sure about that. I am researching it. If you find out the answer post here please
@kazsm9666
@kazsm9666 11 ай бұрын
Do we have to import the image for this to work? because for some reason adding width and height properties are still required for me
@leerob
@leerob 11 ай бұрын
Importing the image adds the width and height for you. If you are using a remote image, you need to either add the width and height yourself, or use the fill prop as shown in this video.
@rezaz7167
@rezaz7167 Ай бұрын
Which version of the image should I put as static file? (1x or 2x)?
@husseinkizz
@husseinkizz Жыл бұрын
How to opt out for image optimization cause some site that is eating my usage quota yet it's not so important in my scenario!
@ashuzon
@ashuzon Жыл бұрын
Can anyone tell me how to handle image from url with dynmic viewport sizes? I though to create a hook around viewport width and then generate image at run time (which will already be because its from a url) using the said width. Is any other way exists? Can this be done? Beside this, thanks for the heads up for all the new features of the image tag from next. I love it and your tutorials. 😉 🎉
@DVSK1
@DVSK1 10 ай бұрын
what VSCode Theme do u use?
@waleedsharif618
@waleedsharif618 Жыл бұрын
Two questions regarding nextjs before i jump. 1: IF i want to use nextjs just for client side rendering (cuz i like many features of it like Link), how can i use nodejs + nextjs ? 2: how to test Nextjs with jest + react testing library ?
@leerob
@leerob Жыл бұрын
1. Everything is still sent from the server, but your initial response from the server could be no HTML and then render everything on the client-side, if you really want, yes. 2. nextjs.org/docs/pages/building-your-application/optimizing/testing
@harriswong89
@harriswong89 6 ай бұрын
Unfortunately, I still got this "Error: Image with src "/..." is missing required "width" property.". I've already set style which contains width and height like you did in this video. I got an error but you didn't. What should I do?😭
@itsabun
@itsabun 11 ай бұрын
hallo, can you make a tutorial to save image/file to local directory while API Posr?
@WesleyOverdijk
@WesleyOverdijk 11 ай бұрын
"Images are only loaded when they enter the viewport using native browser lazy loading, with optional blur-up placeholders." Why is this not working for me? The layers tool shows me that below the viewport the images are loaded already. The sentence makes me think it's using intersection, is that not the case? Or am I missing something?
@hasinmahmood6908
@hasinmahmood6908 2 ай бұрын
this doesnt work, Ive just tried to use the style property to set width and height (without the explicit width and height properties) and its not letting me:
@emilmirzyev7126
@emilmirzyev7126 11 ай бұрын
this next version big problem is state managementing. Please do something for this. Internal global state managementing or connect other state managementing tools at least. Thanks
@nemethricsi
@nemethricsi 11 ай бұрын
how about using placeholder="blur" with images coming from a url, cms, cdn, ... I think that's a much more real world example and I'm struggling with that for a long time.
@ala.garbaa
@ala.garbaa Жыл бұрын
We would like to see the server actions with turbopack
@lilshex
@lilshex 4 ай бұрын
Does anyone know why when I index a bunch of photos in nextJs, and import them they don't show, but when importing individual ones they work ? I'm trying to map them into a grid.
@joshua4262
@joshua4262 10 ай бұрын
Images get 404 error and wont get displayed after deploying to AWS Amplify. Any thoughts?
@spongebob93lover
@spongebob93lover 2 ай бұрын
i am using sizes and fill as taught, the width is correct but why is my height 0 on the browser?
@gold-junge91
@gold-junge91 25 күн бұрын
i dosen't get it full, how is the workaround to global setup a backround image and get it converted
@abdulsalamquadriolasunkanm9944
@abdulsalamquadriolasunkanm9944 Жыл бұрын
Awesome
@imAlexisdzn
@imAlexisdzn 4 ай бұрын
I got a problem when building my app, local images are not rendering. Any reason why is this happening?
@OpTechMarketing
@OpTechMarketing Жыл бұрын
Does the image component work with .svg ?
@leerob
@leerob Жыл бұрын
It does, but you don't need to use it. To optimize your SVG, you'd want to use a different tool. next/image is for optimizing raster images like pngs, not vector images like svgs. Like SVGO! jakearchibald.github.io/svgomg/
@VilaMM
@VilaMM Жыл бұрын
Now we need one for inline videos to play like gifs
@maxskewes
@maxskewes 6 ай бұрын
Using next version 13.3.4 and when I use the first example in this video, what you called, "the most basic," I get the error 'missing required width property'. Why am I or how are you not? Solving this is why I'm here.
@ganapathireddykarri417
@ganapathireddykarri417 7 ай бұрын
why my next images are breaking after i deployed my next website using gitlab pages
@abrarchowdhury5687
@abrarchowdhury5687 9 ай бұрын
trying to do this, similar to how you did it in the video but I keep getting this error: Image with src "./SVG/tintin.svg" is missing required "width" property.
@twisterrjl
@twisterrjl 8 ай бұрын
yeah, same.
@astrophilo2000
@astrophilo2000 7 ай бұрын
same, I guess I will stick to , using 14.0 version ...
@user-on2rx4si7g
@user-on2rx4si7g Жыл бұрын
Good video
@leerob
@leerob Жыл бұрын
Thanks!
@ionutale1950
@ionutale1950 Жыл бұрын
I think is missing a lot of points. just have a look at how many questions NextJS experienced developers have about next/image component.
@khue8703
@khue8703 Жыл бұрын
Hi I think my comment got delete b/c I linked to the issue. next/image currently has a bug with "fill", see issue 50067
@daucuskarota9497
@daucuskarota9497 2 ай бұрын
I do not understand how to make responsive images)) can someone explain?
@mihaillepadatu8256
@mihaillepadatu8256 Жыл бұрын
GOAT🚀
@shayantriedcoding
@shayantriedcoding Жыл бұрын
Hi, loading.tsx is not working in app directory why?
@andrsh9388
@andrsh9388 Жыл бұрын
maybe page.tsx "use client" ??
@shayantriedcoding
@shayantriedcoding Жыл бұрын
@@andrsh9388 no both page.tsx and loading.tsx are server components
@user-xr4oo2xp7q
@user-xr4oo2xp7q Жыл бұрын
This doesn't work for GCP deployment, right?
@leerob
@leerob Жыл бұрын
Yes, it works. The images would be optimized by the Next.js server when using `next start`. I'd recommend installing and using `sharp`, too. nextjs.org/docs/app/building-your-application/deploying#:~:text=If%20you%20are,excessive%20memory%20usage.
@user-xr4oo2xp7q
@user-xr4oo2xp7q 11 ай бұрын
@@leerob Thanks. Will try it.
@sh8yt
@sh8yt 11 ай бұрын
So finally😩 the annoy error gone now use like img tag I mean the annoy require prop
@mohdsahil226
@mohdsahil226 10 ай бұрын
Background image example is not good
@davidadokuru8139
@davidadokuru8139 Жыл бұрын
My little Nigerian startup using the image component got charged using the image component and additional 50 usd I love the component but the cost is too high when you start getting some traffic
@EmmanuelOdii80
@EmmanuelOdii80 Жыл бұрын
What do you mean?
@davidadokuru8139
@davidadokuru8139 Жыл бұрын
@@EmmanuelOdii80 so we are on the pro plan You get 5000 image optimisation free for a month after that you need to pay for additional optimisations. 5000 might seem like a lot but it really isn’t, it doesn’t even last for a week on our product.
@nivethan_me
@nivethan_me Жыл бұрын
so you deployed the app on Vercel?
@davidadokuru8139
@davidadokuru8139 Жыл бұрын
@@nivethan_me yeah
@ionutale1950
@ionutale1950 Жыл бұрын
i don't get how this works? kzfaq.info/get/bejne/f7uPpNSQyMXQe3U.html how does it optimize images from a remote source? this seems like a fake feature, that does not actually work. unless you are doing an SSG, this is the only way that can actually work
@cagdasucar3932
@cagdasucar3932 5 ай бұрын
This video leaves many questions unanswered. No information on how to do art directing for example. Can you really talk about responsive images without art direction? What about background images? I mean the real background images - CSS background-image property. Why can't it use it?
@leerob
@leerob 5 ай бұрын
github.com/vercel/next.js/discussions/19880#discussioncomment-6310787
@galacticascent9218
@galacticascent9218 3 ай бұрын
Your responsive isn't responsive at all. What if we don't want to always stretch the image to 100% width.
@k2p11
@k2p11 Жыл бұрын
First
@niki5223
@niki5223 4 ай бұрын
But for Nextjs 14 images are still throwing off error tried the below stuff but no use - protocol: 'https', hostname: 'example.cloudfront.net', port: " ", pathname: '/**',
@rezaz7167
@rezaz7167 Ай бұрын
Which version of the image should I put as static file? (1x or 2x)?
Next.js Image - Never struggle again (+ ImageKit)
36:01
ByteGrad
Рет қаралды 17 М.
10 common mistakes with the Next.js App Router
20:37
Vercel
Рет қаралды 185 М.
터키아이스크림🇹🇷🍦Turkish ice cream #funny #shorts
00:26
Byungari 병아리언니
Рет қаралды 24 МЛН
Универ. 13 лет спустя - ВСЕ СЕРИИ ПОДРЯД
9:07:11
Комедии 2023
Рет қаралды 5 МЛН
10 Tailwind Tricks You NEED To Know!
10:45
Ravi - Perfect Base
Рет қаралды 270 М.
DON'T Serve Unoptimized Images - Webp, Srcset, ImageKit, and more!
18:29
Next.js App Router Caching: Explained!
25:22
Vercel
Рет қаралды 82 М.
How To Load Images Like A Pro
15:48
Web Dev Simplified
Рет қаралды 358 М.
Next.js Server Actions...  5 awesome things you can do
7:51
Beyond Fireship
Рет қаралды 253 М.
Next js 15 is Here… New Changes Again?!
8:13
JavaScript Mastery
Рет қаралды 117 М.
Using CSS custom properties like this is a waste
16:12
Kevin Powell
Рет қаралды 159 М.
Next.js App Router: Routing, Data Fetching, Caching
14:32
Vercel
Рет қаралды 295 М.
How To Unlock Your iphone With Your Voice
0:34
요루퐁 yorupong
Рет қаралды 25 МЛН
WWDC 2024 - June 10 | Apple
1:43:37
Apple
Рет қаралды 10 МЛН
iOS 18 vs Samsung, Xiaomi,Tecno, Android
0:54
AndroHack
Рет қаралды 21 М.
Main filter..
0:15
CikoYt
Рет қаралды 9 МЛН