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

Sdílet
Vložit
  • čas přidán 1. 06. 2024
  • 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...
  • Věda a technologie

Komentáře • 110

  • @waggadash
    @waggadash Před 10 měsíci

    Love it! Currently I am building my first SaaS product with NextJS ... and this 'optimization course' was exactly what I needed! Thank you Lee

  • @ala.garbaa
    @ala.garbaa Před 11 měsíci +41

    Being responsive by default is what we have been waiting for 😊

    • @favouritejome
      @favouritejome Před 11 měsíci

      Exactly ❤

    • @loveboat
      @loveboat Před 11 měsíci +5

      Too bad it isn't.

    • @aberba
      @aberba Před 5 měsíci +1

      CSS isn't responsive by default. None of the HTML elements are. It's that way for a reason

  • @HungNguyen-tl9kg
    @HungNguyen-tl9kg Před 11 měsíci +21

    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 Před 11 měsíci +2

    Always great content Lee, thank you!

  • @kevinbatdorf
    @kevinbatdorf Před 11 měsíci +1

    Very nice background set up and video production quality.

  • @kylemckell
    @kylemckell Před 11 měsíci +33

    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 Před 11 měsíci +2

      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 Před 11 měsíci +1

      @@loveboat the old one was super confusing, but the new one is a lot better

  • @ala.garbaa
    @ala.garbaa Před 11 měsíci +3

    I am happy to see you posting new videos ❤ keep going 🎉

  • @yuriiholskyi9009
    @yuriiholskyi9009 Před 11 měsíci

    Thanks, Lee. Excellent and useful examples. Like )

  • @rockNbrain
    @rockNbrain Před 11 měsíci +1

    Great job Lee! Tks 🎉

  • @juricablazevic9329
    @juricablazevic9329 Před 8 měsíci

    Thank you so much! That was very helpful!!!

  • @PieterBos
    @PieterBos Před 10 měsíci +5

    Would love to see a picture tag implementation , using different images / aspects ratios on mobile / desktop

  • @lukas.webdev
    @lukas.webdev Před 11 měsíci

    Great video, very valuable! 👍

  • @sck3570
    @sck3570 Před 11 měsíci +3

    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)

  • @ala.garbaa
    @ala.garbaa Před 11 měsíci

    Amazing update !

  • @appstuff6565
    @appstuff6565 Před 8 měsíci

    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 Před 10 měsíci

    Hey Lee, amazing video, just wanted to know what vscode theme are you using looks quite attractive 😅

  • @ericlindell3777
    @ericlindell3777 Před 11 měsíci +4

    Great video! I’d like to see one about caching, both with appdir and pagedir.

    • @leerob
      @leerob  Před 11 měsíci

      czcams.com/video/gSSsZReIFRk/video.html

  • @tdombui
    @tdombui Před 5 měsíci

    I love it when I find the solution to one of my problems while randomly scrubbing through coding videos

  • @user-pt6hf7xo3o
    @user-pt6hf7xo3o Před 11 měsíci

    Great input

  • @denilsoncosta9837
    @denilsoncosta9837 Před 10 měsíci +1

    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].

  • @AHN1444
    @AHN1444 Před 10 měsíci

    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?

  • @ala.garbaa
    @ala.garbaa Před 11 měsíci +2

    Blur data URL is epic 👍

  • @EmmanuelOdii80
    @EmmanuelOdii80 Před 11 měsíci +1

    Thanks mate! ❤

    • @leerob
      @leerob  Před 11 měsíci +1

      No problem!

  • @buddy.abc123
    @buddy.abc123 Před 10 měsíci

    Nice! So we do we still need to install Sharp or not?

  • @milovangudelj
    @milovangudelj Před 11 měsíci +1

    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 Před 11 měsíci +4

      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 Před 11 měsíci

      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 Před 11 měsíci

      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

  • @janikaikkonen6384
    @janikaikkonen6384 Před 10 měsíci +4

    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 Před 3 měsíci

      have you found a solution?

  • @lucaapa
    @lucaapa Před 9 měsíci +1

    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'

  • @kevinat71
    @kevinat71 Před 11 měsíci

    great video!... what about svg icons with multiple icon sizes? should we use next/image for that?

    • @Victor-dd7el
      @Victor-dd7el Před 11 měsíci +1

      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 Před měsícem

    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.

  • @sergey_gabrielyan1001
    @sergey_gabrielyan1001 Před 3 měsíci

    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 Před 11 měsíci

    What if the external image is hosted on a Protected route ? Can we supply Authorization Headers to Next/Image ?

  • @pepew7102
    @pepew7102 Před 11 měsíci +1

    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  Před 11 měsíci +2

      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.

  • @jazsouf
    @jazsouf Před 8 měsíci

    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

  • @waleedsharif618
    @waleedsharif618 Před 11 měsíci

    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  Před 11 měsíci

      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

  • @manishbalhara9562
    @manishbalhara9562 Před 6 měsíci

    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?

  • @canklc5772
    @canklc5772 Před 8 měsíci

    crystal clear

  • @greg.j
    @greg.j Před 6 měsíci +1

    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” 😅

  • @realintro7470
    @realintro7470 Před 9 měsíci

    any idea on how to use videos as backgrounds in next js please?

  • @husseinkizz
    @husseinkizz Před 11 měsíci

    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!

  • @WesleyOverdijk
    @WesleyOverdijk Před 10 měsíci

    "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?

  • @itsabun
    @itsabun Před 11 měsíci

    hallo, can you make a tutorial to save image/file to local directory while API Posr?

  • @StudioHetKanaal
    @StudioHetKanaal Před 11 měsíci

    Would love to see how to handle anything else than happy paths with the new app router. Retries for example

  • @kazsm9666
    @kazsm9666 Před 11 měsíci

    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  Před 11 měsíci

      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 Před 29 dny

    Which version of the image should I put as static file? (1x or 2x)?

  • @joshua_hc_cr
    @joshua_hc_cr Před 6 měsíci

    The image component in Next.js is particularly beneficial for individuals who want to avoid the complexities of handling various rendered sizes.

  • @oOserkanCakmakOo
    @oOserkanCakmakOo Před 11 měsíci

    Thank you

  • @nemethricsi
    @nemethricsi Před 10 měsíci

    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.

  • @sangeethreddychejerla6527
    @sangeethreddychejerla6527 Před 11 měsíci

    Waiting for your video.

  • @ashuzon
    @ashuzon Před 11 měsíci

    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 Před 9 měsíci

    what VSCode Theme do u use?

  • @emilmirzyev7126
    @emilmirzyev7126 Před 10 měsíci

    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

  • @joshua4262
    @joshua4262 Před 9 měsíci

    Images get 404 error and wont get displayed after deploying to AWS Amplify. Any thoughts?

  • @gold-junge91
    @gold-junge91 Před 4 dny

    i dosen't get it full, how is the workaround to global setup a backround image and get it converted

  • @harriswong89
    @harriswong89 Před 5 měsíci

    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?😭

  • @abdulsalamquadriolasunkanm9944

    Awesome

  • @spongebob93lover
    @spongebob93lover Před měsícem

    i am using sizes and fill as taught, the width is correct but why is my height 0 on the browser?

  • @lilshex
    @lilshex Před 3 měsíci

    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.

  • @imAlexisdzn
    @imAlexisdzn Před 3 měsíci

    I got a problem when building my app, local images are not rendering. Any reason why is this happening?

  • @maxskewes
    @maxskewes Před 5 měsíci

    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.

  • @OpTechMarketing
    @OpTechMarketing Před 11 měsíci +1

    Does the image component work with .svg ?

    • @leerob
      @leerob  Před 11 měsíci

      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/

  • @ala.garbaa
    @ala.garbaa Před 11 měsíci

    We would like to see the server actions with turbopack

  • @abrarchowdhury5687
    @abrarchowdhury5687 Před 9 měsíci

    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.

  • @VilaMM
    @VilaMM Před 11 měsíci

    Now we need one for inline videos to play like gifs

  • @ganapathireddykarri417
    @ganapathireddykarri417 Před 7 měsíci

    why my next images are breaking after i deployed my next website using gitlab pages

  • @mihaillepadatu8256
    @mihaillepadatu8256 Před 11 měsíci

    GOAT🚀

  • @khue8703
    @khue8703 Před 11 měsíci

    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

  • @user-on2rx4si7g
    @user-on2rx4si7g Před 11 měsíci

    Good video

    • @leerob
      @leerob  Před 11 měsíci

      Thanks!

    • @ionutale1950
      @ionutale1950 Před 11 měsíci

      I think is missing a lot of points.
      just have a look at how many questions NextJS experienced developers have about next/image component.

  • @hasinmahmood6908
    @hasinmahmood6908 Před měsícem

    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:

  • @ShayansCodeCommunity
    @ShayansCodeCommunity Před 11 měsíci

    Hi, loading.tsx is not working in app directory why?

    • @andrsh9388
      @andrsh9388 Před 11 měsíci +1

      maybe page.tsx "use client" ??

    • @ShayansCodeCommunity
      @ShayansCodeCommunity Před 11 měsíci

      @@andrsh9388 no both page.tsx and loading.tsx are server components

  • @daucuskarota9497
    @daucuskarota9497 Před měsícem

    I do not understand how to make responsive images)) can someone explain?

  • @user-xr4oo2xp7q
    @user-xr4oo2xp7q Před 11 měsíci

    This doesn't work for GCP deployment, right?

    • @leerob
      @leerob  Před 11 měsíci +1

      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 Před 11 měsíci

      @@leerob Thanks. Will try it.

  • @sh8yt
    @sh8yt Před 10 měsíci

    So finally😩 the annoy error gone now use like img tag
    I mean the annoy require prop

  • @davidadokuru8139
    @davidadokuru8139 Před 11 měsíci +3

    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 Před 11 měsíci

      What do you mean?

    • @davidadokuru8139
      @davidadokuru8139 Před 11 měsíci

      @@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 Před 11 měsíci

      so you deployed the app on Vercel?

    • @davidadokuru8139
      @davidadokuru8139 Před 11 měsíci

      @@nivethan_me yeah

  • @mohdsahil226
    @mohdsahil226 Před 9 měsíci +3

    Background image example is not good

  • @cagdasucar3932
    @cagdasucar3932 Před 4 měsíci

    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  Před 4 měsíci +1

      github.com/vercel/next.js/discussions/19880#discussioncomment-6310787

  • @ionutale1950
    @ionutale1950 Před 11 měsíci +1

    i don't get how this works?
    czcams.com/video/IU_qq_c_lKA/video.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

  • @k2p11
    @k2p11 Před 11 měsíci

    First

  • @galacticascent9218
    @galacticascent9218 Před 2 měsíci

    Your responsive isn't responsive at all. What if we don't want to always stretch the image to 100% width.

  • @niki5223
    @niki5223 Před 3 měsíci

    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 Před 29 dny

    Which version of the image should I put as static file? (1x or 2x)?