Next.js Image blurDataURL problem with plaiceholder
Answered
American black bear posted this in #help-forum
American black bearOP
this is my component.
This is my utils file:
When I run my code it give error. How can I solve this problem? Is there any other way to create Image blurDataURL?
import React, { useEffect, useState } from "react";
import { CloseIcon} from "@/components/SvgComponent"
import ImageWithBlur from "@/components/ImageWithBlur"
import { GeneratedImage } from "models";
import {getBlurData} from "utils/BlurDataUrlGenerator";
type ViewFullImageProps = {
image: GeneratedImage;
onClose: ()=>void
}
const ViewFullImage = ({image, onClose}:ViewFullImageProps) => {
const [blurDataURL, setBlurDataURL] = useState<string | undefined>(undefined);
useEffect(() => {
const fetchBlurDataURL = async () => {
try {
const blurData = await getBlurData(image.imageUrl);
console.log("blurData", blurData)
} catch (error) {
console.error("Error fetching blur data:", error);
}
};
fetchBlurDataURL();
}, [image.imageUrl]);
return (
<>
<div className="bg-[#E8E8E8] h-full pt-7 pb-[60px] px-10 relative">
<div className="h-full pt-7">
<ImageWithBlur
src={image.imageUrl}
height={500}
width={600}
priority={true}
alt="Ai generated"
className="object-contain w-full h-full"
placeholder="blur"
blurDataURL={base64}
/>
</div>
</div>
</>
);
};
export default ViewFullImage;This is my utils file:
import { getPlaiceholder } from "plaiceholder";
async function getBlurData(src: string): Promise<string> {
try {
const response = await fetch(src);
const buffer = await response.arrayBuffer();
const { base64 } = await getPlaiceholder(buffer);
return base64;
} catch (error) {
throw error;
}
}
export { getBlurData };When I run my code it give error. How can I solve this problem? Is there any other way to create Image blurDataURL?
33 Replies
American black bearOP
How can I solve it?
@American black bear How can I solve it?
use it on server
Original message was deleted
American black bearOP
I could not udersatand. May you send any code snippet?
@American black bear I could not udersatand. May you send any code snippet?
turn the client component to server component
@Ray https://github.com/joe-bell/plaiceholder/blob/main/examples/next/app/(example)/base64/single/page.tsx
American black bearOP
I have try with this code.
when I use async , it give this error
export default async function ViewFullImage({image}) {when I use async , it give this error
@American black bear I have try with this code.
export default async function ViewFullImage({image}) {
when I use async , it give this error
are you rendering
ViewFullImage in a client componentor are you using pages router
@Ray are you rendering `ViewFullImage` in a client component
American black bearOP
How can I make this parent component as a server component?
@Ray or are you using pages router
American black bearOP
no. I am using component. In page route I just put the template code
it is server component by default unless you mark it client by putting
'use client' on topAmerican black bearOP
ViewFullImage is rendering as a modalso you are using the route in pages folder?
pages router doesn't have server component
@Ray so you are using the route in pages folder?
American black bearOP
no it outside. src/components
but where is your route defined
American black bearOP
inside of
src/pages folderso you are using pages router
American black bearOP
yeah
which doesn't havev server component
American black bearOP
so, how can i make it server component
you will need to use
plaiceholder in getServerSideProps@American black bear so, how can i make it server component
you can't use server component in pages router
American black bearOP
export async function getServerSideProps() {
try {
const imageUrl = 'your_image_url_here'; // Replace with your image URL
const { base64 } = await getPlaiceholder(imageUrl);
return { props: { blurDataURL: base64 } };
} catch (error) {
console.error('Error fetching blur data:', error);
return { props: { blurDataURL: null } };
}
}like this?
@Ray on the page not the component
American black bearOP
if I call getServerSideProps on the page, then i need to send the blurDataURL as propos to the component?
@Ray yea
American black bearOP
There is no other way? In my case, i am using nested component
American black bearOP
ok. I try