Video není dostupné.
Omlouváme se.

Blurred placeholder for dynamic images in NextJs

Sdílet
Vložit
  • čas přidán 3. 06. 2024
  • In this video, we’ll look at generating `blurDataURL` to use as a placeholder when lazy loading dynamic images in NextJs using the plaiceholder package.
    👉🏼 The ultimate NextJs course
    → www.hamedbahram.io/courses
    👉🏼 Project source code
    → github.com/HamedBahram/next-i...
    👉🏼 Work with me
    → www.hamedbahram.io/hire

Komentáře • 54

  • @jamesfoley4426
    @jamesfoley4426 Před měsícem +3

    Yes to a full video on the image component and the differences between next image and using an image service such as cloudinary on performance.

  • @collinsk8754
    @collinsk8754 Před měsícem +1

    Well explained! Thanks.

  • @wandenreich770
    @wandenreich770 Před měsícem +1

    Love your nexts videos

  • @vivekthapa5979
    @vivekthapa5979 Před měsícem +2

    Nice work. Can you show how to create Progressive Web Application using nextjs? I think this will be useful to lots of developer.

    • @hamedbahram
      @hamedbahram  Před měsícem +2

      I agree! Thanks for the suggestion. I'll add that to my schedule.

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

      ​@@hamedbahram agreed!

  • @thomasschwinn
    @thomasschwinn Před měsícem +1

    thank you for your good work, I think there are several ways to create blurred images and serve them. For me I save a blurhash in my database, then during built, I create a base64 image from the from blurhash for images that are inside the initial viewport, for images that are outside the inital viewport i use the blurhash with the react blurhash component. It hink it builts a bit faster like that, because the images do not have to be fetched during built. But I'll try the plaiceholder package, thank you

  • @fazlechistyhimel3958
    @fazlechistyhimel3958 Před měsícem +1

    Amazing.

  • @fakhrulislamfuad8072
    @fakhrulislamfuad8072 Před měsícem +1

    nice work bro.. want you to do something with the monorepo with nextjs 13 including the deployment process.. keep this good work up man😀

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

      Thanks! I've a couple of turborepo videos on the channel, seen them?

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

      @@hamedbahram yeah.. I've seen them.. but I haven't seen any project deployment video yet.. please try to make at least one video on it and another request: please make a video on web components with nextjs 13.. I think react 19 will have a built in support for web components.. I guess it's high time for you to create something on that web-component topic..

  • @ExileEditing
    @ExileEditing Před 2 měsíci +2

    Not able to fully watch right now but do you know how much time it takes to generate that base64 image. Is it fine to do this on every request for each image or is it better to generate it on image upload and store that in the database. I'm currently doing the 2nd option because I wasn't sure if it would be a bad idea to generate all of these if you had many on one page. For example with a list of courses with their thumbnail images

    • @hamedbahram
      @hamedbahram  Před 2 měsíci +1

      It loads pretty much at the same rate as if you were importing the image statically.

  • @exposuredesignhelp
    @exposuredesignhelp Před měsícem +1

    Great video. Any idea how you would use Plaiceholder if your images are in a client component - I have a lightbox type 3rd party component that uses ref's so needs to be client component but I believe plaiceholder needs to be server based?

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

      Thank you! You can create your own wrapper server component to create the blurred data and pass it to the 3rd party client component.

  • @leulfanuel3550
    @leulfanuel3550 Před měsícem +1

    Great video sir. 2 questions tho, how do we load two logo images one for dark and light without a mismatch? also how do we set a dynamic hostname in next.config if the image we store has a dynamic url? can we use process.env?

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

      You can use the `next-themes` package and depending on the theme pass the desired url or statically imported image to the next/image component. As far as dynamic hostname, you can use a wildcard pattern to match a range of subdomains and paths. Read here → nextjs.org/docs/app/api-reference/components/image#remotepatterns

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

      @@hamedbahram i am using remote patterns but the hostname is dynamic i can't know it on build time

  • @aminsoraya4517
    @aminsoraya4517 Před měsícem +1

    So useful but there is two notes: the first one is How can i shows my logo instead of whitespace before rendering the original image. And the second one is , Are there any performance pitfalls for plaiceholer rendering for each request (I mean in server side section) . It seems either each request have a workload to creating blur data or Maybe cache it to save costs?

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

      You can use the `loading` and `onLoadingComplete` events to show your logo and swap it with the actual image when the next/image finishes loading. As far as the performance, if you're pages are static, this process would run at build time and result will be cached as static pages.

  • @nikhilpsathyanathan
    @nikhilpsathyanathan Před měsícem +1

    How to manage tailwind merge conflict when we use a custom tailwind config.
    eg, like text-primary - is a color and text-large is a typography sometimes it conflict with wrong one. when i checked the docs it was a bit confusing.

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

      When using custom utilities you should come up with a less conflicting naming convention, or at least follow Tailwind's convention `font-*` for sized and `text-*` for colors.

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

      @@hamedbahram normally we use text-red-200 text-lg it won't conflict. front for font family and weight

    • @kuan88
      @kuan88 Před měsícem +1

      @@nikhilpsathyanathan
      We also encountered this issue where Tailwind sometimes confuses custom text classes like text-primary (color) and text-large (typography). Tailwind determines font size-related attributes by checking if the suffix of text- matches a predefined T-shirt size. We solved it using the `extendTailwindMerge` API to define custom merging rules.
      Here’s how we did it:
      ```
      const twMerge = extendTailwindMerge({
      classGroups: {
      'font-size': [
      {
      text: [
      'section-title',
      'body',
      'small',
      'extra-small',
      // ...
      ],
      },
      ],
      },
      });
      export { twMerge };
      ```
      This approach ensures text-extra-small is treated as a font size, avoiding conflicts.
      Hope this solves your issue.

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

      @@kuan88 thanks man, I was looking for this answer.

  • @rezaz7167
    @rezaz7167 Před měsícem +1

    How to tell the image show up or animate when it's fully loaded? instead of loading from top to bottom.

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

      You can use the loading events on the image component.

  • @kubilaybzk
    @kubilaybzk Před měsícem +1

    for this use methods , only use server components right ?

  • @joshuawalker7375
    @joshuawalker7375 Před 25 dny

    Does anyone know a good blog or tutorial on revalidating a cached next/image on-demand. I'm using Next 13 and currently when a user uploads a profile picture the first time works but when they update the image with a new one, it registers in Cloudinary but not on the website due to next.js built-in caching functionality. I'm starting to think I may have chosen the wrong tool for the job. I thought I might be able to use revalidateTag if I configured the next config file to enable server actions but that didn't work.

    • @hamedbahram
      @hamedbahram  Před 25 dny

      On-demand revalidation should do the job, you can use `revalidatePath` or `revalidateTag` in your server action in response to the user uploading a new image. Watch this video where at the end I show how to use server action via the new `useActioState` hook and `revalidatePath` to purge the cache. czcams.com/video/EB0Nu_e9wCs/video.html

  • @rahulchourasiya4148
    @rahulchourasiya4148 Před měsícem +1

    Please 🙏make a video on single sign on

    • @hamedbahram
      @hamedbahram  Před měsícem +1

      Sure! That'll make a good video, did you mean using Auth.js?

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

      @@hamedbahram yes,but in my case I want to make a authentication website which authenticate another saas website hosted on the different domain, like Google

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

    Am I right to assume that your DynamicImage component could not be used in a 'use client' component because of its async nature?

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

    i tried this but it keep saying "ReferenceError: require is not defined".

    • @hamedbahram
      @hamedbahram  Před měsícem +1

      It seems like you're using the `plaiceholder` on the client side, it need to run server side. You can clone my code and see what you're doing differently.

  • @Happyday-nn6rh
    @Happyday-nn6rh Před měsícem +1

    nice but why dont use this style :
    setLoading(false)}
    />
    ?

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

      That's still loading the original image, the place holder is a very small base64 image that loads very fast instead of a white space while the image is loading.

  • @waeltarabishi6890
    @waeltarabishi6890 Před 5 dny

    require is not defined
    why ???

  • @ramlingsuryavanshi6141
    @ramlingsuryavanshi6141 Před měsícem +1

    Hi Hamed, I tried this but there is error while running the app. As you mention the next.config extension should be .mjs and it is there.
    Error:
    layout.js:18 Uncaught (in promise) ReferenceError: require is not defined
    at Object.sharp (layout.js:18:1)
    at options.factory (webpack.js?v=1717570510240:715:31)

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

      It seems like you might be using the `plaiceholder` package on the client side. Look where you've defined the `getImage` function and make sure the component using the function is not a using the `use client` directive.

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

      @@hamedbahram yes, you are right 👍. I added getImage function to utils file (which is default in nextjs).
      Thank you so much for help and nice video. I really helps us.

  • @muhammadk23
    @muhammadk23 Před 2 dny

    i am getting an error says "require is not defined"
    import "sharp";
    index.d.ts
    import type { NextConfig } from "next";
    export default function withPlaiceholder(nextConfig: NextConfig): NextConfig;
    /***/
    "sharp": /*!************************!*\
    !*** external "sharp" ***!
    \************************/
    /***/
    (function(module, __unused_webpack_exports, __webpack_require__) {
    "use strict";
    module.exports = require("sharp"); // this line is highlighted
    /***/
    }
    ),
    i installed sharp but still getting the error.