Replies: 10 comments 39 replies
-
|
my quick workaround for now (this works right now): <Image
src={imageURL}
placeholder="blur"
blurDataURL={`/_next/image?url=${imageURL}&w=16&q=1`}
...
/>I know it's not optimal, since a base64 string embedded in the html wouldn't have to be loaded at all. but having a placeholder image that is only ~3kb to load is better than nothing |
Beta Was this translation helpful? Give feedback.
-
|
In the meantime, you could use Demo Code import * as React from "react";
import { InferGetStaticPropsType } from "next";
import Image from "next/image";
import { getPlaiceholder } from "plaiceholder";
export const getStaticProps = async () => {
const { base64, img } = await getPlaiceholder(
"https://images.unsplash.com/photo-1621961458348-f013d219b50c?auto=format&fit=crop&w=2850&q=80",
{ size: 10 }
);
return {
props: {
imageProps: {
...img,
blurDataURL: base64,
},
},
};
};
const ExamplePage: React.FC<InferGetStaticPropsType<typeof getStaticProps>> = ({
imageProps,
}) => <Image {...imageProps} placeholder="blur" />;
export default ExamplePage; |
Beta Was this translation helpful? Give feedback.
-
|
Ive been playing with this myself and Im running into a similar issue. Maybe I'm not understanding the use case well enough, but my use case is as follows:
FWIW, @joe-bell I can't seem to get Maybe I'm in over my head here. But it seems like it should be way easier to encode a blurry image and decode it with next/image. Even |
Beta Was this translation helpful? Give feedback.
-
|
I ended up using Jimp in the end to get base64 data in getStaticProps like this I can't help thinking it would be better if next/image took care of this. If you opt into it Next/Image could generate the base64 string at build / runtime on the server. Also perhaps you could opt in to have it cached in .next/cache as our images are generally immutable in the sense that if they change a new url is generated (my build times have swelled crazily since the above code). Also I think it's wasteful that the base64 string is in NEXT_DATA AND the image style tag. If next/image owned base64 it could just put it in the style attribute and not have them cascade down the react tree as props. Also a question that I'd like to ask, in the server rendered html the src is also a very small gif base64 string. Is there no way the proper src and srcsets can be sent so even without js images properly load? |
Beta Was this translation helpful? Give feedback.
-
|
This might be a daft idea, But if you are talking uploaded images in your app that you host on an external CDN or on S3 or something, write a routine that after the file is uploaded constructs the URL to fetch it via nextjs image processor to a buffer, add mimetype and push it as a data URI into your database? just create an extra column with blur_data_url in your image table. This way you won't have to fiddle around with sharp or imagic, its just a fetch to buffer and then a crud operation. |
Beta Was this translation helpful? Give feedback.
-
|
You can also achieve this with CSS. Here's an example with Tailwind CSS: import Image from 'next/image';
import { useState } from 'react';
import cn from 'clsx';
function BlurImage(props) {
const [isLoading, setLoading] = useState(true);
return (
<Image
{...props}
alt={props.alt}
className={cn(
props.className,
'duration-700 ease-in-out',
isLoading
? 'grayscale blur-2xl scale-110'
: 'grayscale-0 blur-0 scale-100'
)}
onLoadingComplete={() => setLoading(false)}
/>
);
} |
Beta Was this translation helpful? Give feedback.
-
|
I was able to solve this pretty well just using the Next image API to generate a small image with quality = 1. Like: static getPlaceholderImageURL(imageURL: string): string {
return `/_next/image?url=${encodeURIComponent(imageURL)}&q=1&w=128`
}Then used like: <Image src={props.featuredImageURL}
blurDataURL={Utils.getPlaceholderImageURL(props.featuredImageURL)}
placeholder="blur"
/> |
Beta Was this translation helpful? Give feedback.
-
|
I'm using the "app folder", and I have a somewhat clumsy method that fits my use case: 'use client';
import Image from 'next/image';
import { useState } from 'react';
import classNames from 'classnames';
interface ImageWithBlurProps {
src: string;
className?: string;
alt?: string;
fill?: boolean;
}
export default function ImageWithBlur(props: ImageWithBlurProps) {
const { src, className, alt = '', fill = false } = props;
const [loaded, setLoaded] = useState<boolean>(false);
return (
<>
<Image
fill={fill}
unoptimized
className={classNames(className, { hidden: loaded, block: !loaded })}
src={`${src}?image_process=resize,p_10`} // You can modify this to use a remote URL as a placeholder.
loading="eager"
alt={alt}
/>
<Image
fill={fill}
unoptimized
className={classNames(className, { hidden: !loaded, block: loaded })}
src={src}
alt={alt}
loading="eager"
onLoadingComplete={() => setLoaded(true)}
/>
</>
);
} |
Beta Was this translation helpful? Give feedback.
-
|
Are there any good solutions for blurring external images on client-side components? It seems that |
Beta Was this translation helpful? Give feedback.
-
|
Previous to Next.js, in an MPA context, I used LazySizes to achieve this pattern on the Stencil framework on BigCommerce; we use a ~10px wide LQIP served straight from our CDN with negligible LCP or Web Vitals impact. I believe it works just fine if you have a fast CDN to which the client is already connected; especially with HTTP/2 you can shovel dozens of <1kb assets into the browser in less than 30ms or so, and the experience can be quite nice. In the case of BigCommerce, we've had this strategy live for 7+ years and it has been durable and high performance through many years of Core Web Vitals adjustments. Demo example here: https://v0-image-gallery-app-eight.vercel.app/ Certainly it is not as optimal as having inline base64 data, but as is clear in this thread the infrastructural needs necessary to pre-populate that data are sometimes difficult to satisfy or come with their own tradeoffs, so allowing this ability to use an external URL (even if it's not recommended) feels like a highly desirable compromise to me. |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the feature you'd like to request
As of now, using
placeholder="blur"only works out of the box for statically imported images. Otherwise we would have to define ablurDataUrlproperty which would load a small version of that very image - but this placeholder image would have to be served via a custom server using e.g. blurhash.Describe the solution you'd like
Passing
placeholder="blur"should be enough for the user in order to enjoy the blur up effect - even for external hosted images (e.g. headless CMS).Describe alternatives you've considered
Alternatives atm are using a custom server as described above. Next.js mentions blurha.sh which would be fine, but stilll - it causes an additional overhead that might not be necessary.
Beta Was this translation helpful? Give feedback.
All reactions