João Pedro
João Pedro

Reputation: 978

How to display a placeholder while the image is loaded in Next.Js?

I'm getting some images from Firebase using getStaticProps, and then displaying them with next Image component. However, they take a while to display, how can I display a placeholder while they haven't loaded?

Upvotes: 15

Views: 34855

Answers (2)

Ramin eghbalian
Ramin eghbalian

Reputation: 2677

use placeholder property, a placeholder to use while the image is loading, possible values are blur or empty. Defaults to empty.

When blur, the blurDataURL property will be used as the placeholder. If src is an object from a static import and the imported image is jpg, png, or webp, then blurDataURL will automatically be populated.

notice that for dynamic images, you must provide the blurDataURL property. the contents of "blurDataURL" property should be a small Data URL to represent the image.

Upvotes: 6

elpmid
elpmid

Reputation: 932

There is an experimental merged pull request on thery @canary branch. If you want to try it out just update your nextjs version on the latest canary released. Just add a new placeholder="blur" props on the Image component.

Or you can use this package.

Edit: Nextjs 11 now has officially released the placeholder='blur' props for the <Image> component. You can read here more about this image placeholders. Make sure you update your nextjs, react, react-dom versions by running npm i next@latest react@latest react-dom@latest

Upvotes: 13

Related Questions