#blurDataUrl in client component

60 messages · Page 1 of 1 (latest)

summer summit
#

Hello, I've created a component called BlurData which looks like this:

import getBase64 from '@/utils/getLocalBase64';

import Image from 'next/image';

type BlurDataProps = {
    src: string;
};

export default async function BlurData({ src }: BlurDataProps) {
    const blurDataUrl = await getBase64(src);

    return (
        <Image
            src={src}
            alt="Generated icon"
            height="250"
            width="250"
            priority={true}
            placeholder="blur"
            blurDataURL={blurDataUrl}
        />
    );
}```

The problem is when I try to use it in my client component called `GenerateIconForm` like the following:

```js
<BlurData src={generatedIcon?.imageUrl ?? ''} />

I get an error:

./node_modules/detect-libc/lib/detect-libc.js:6:1
Module not found: Can't resolve 'child_process'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./node_modules/sharp/lib/utility.js
./node_modules/sharp/lib/index.js
./node_modules/plaiceholder/dist/plaiceholder.esm.js
./src/utils/getLocalBase64.ts
./src/app/components/BlurData.tsx
./src/app/components/GenerateIconForm.tsx

Is there something that I am missing?

shut zodiacBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

fading cloak
summer summit
#

I think the problem is that this needs to run in the server

#

and since I am importing it on the client, it throws error mhm

fading cloak
#

using client-components inside the server component shouldn't create a problem

summer summit
#

Problem is that BlurData is a server component

#

and GenerateIconForm is a client component

fading cloak
#

try removing getBase64 from the BlurData and see what happens

summer summit
#

Yeah that's the problem

fading cloak
#

could you show me the getLocalBase64 file? to see what's the issue

summer summit
#

export default async function getBase64(imageUrl: string) {
    try {
        const res = await fetch(imageUrl);

        if (!res.ok) {
            throw new Error(
                `Failed to fetch image: ${res.status} ${res.statusText}`,
            );
        }

        const buffer = await res.arrayBuffer();

        const { base64 } = await getPlaiceholder(Buffer.from(buffer));

        return base64;
    } catch (e) {
        if (e instanceof Error) console.log(e.stack);
    }
}
#

I am importing it in my BlurData component which is a server component, then importing BlurData into GenerateIconForm which is a client component

fading cloak
#

is getBase4 inside the client side?

summer summit
#

Wdym? I have this file in /src/utils

fading cloak
#

@/utils/getLocalBase64 file

summer summit
#

Yeah

fading cloak
#

that must be the problem, because according to plaiceholder website quote:

"Plaiceholder" is a suite of server-side functions for creating low quality image placeholders (LQIP).
so, you should use it inside the server-side componentds

#

you can't use server-side functions on the client-side

summer summit
#

BlurData is a server component

#

Ah yeah, that's what I thought

fading cloak
#

BlurData yes, but getLocalBase64 isn't

summer summit
#

I am thinking of a workaround here

#

Api endpoint that returns the blurData?

fading cloak
#

getPlaiceholder is your main concern, it should only be called inside the server-side component, either turn entire getLocalBase64 inside the server-side or create a new component with getPlaiceholder and import it

#

getPlaiceholder must be on the server-side

#

also make sure that sharp is installed

summer summit
#

Yeah just installed when y ou told me

#

Just checked their docs and it must be installed

fading cloak
#

and you still have the issue?

summer summit
#

I am moving the logic to my server component

#

I did the following, and still getting same error:

import { getPlaiceholder } from 'plaiceholder';

import Image from 'next/image';

type BlurDataProps = {
    src: string;
};

export default async function BlurData({ src }: BlurDataProps) {
    const res = await fetch(src);
    const buffer = await res.arrayBuffer();
    const { base64 } = await getPlaiceholder(Buffer.from(buffer));

    return (
        <Image
            src={src}
            alt="Generated icon"
            height="250"
            width="250"
            priority={true}
            placeholder="blur"
            blurDataURL={base64}
        />
    );
}```
#

this is my BlurData component

fading cloak
summer summit
#

Yep, removing const { base64 } = await getPlaiceholder(Buffer.from(buffer)); works

#

mhm

fading cloak
summer summit
#

oh no

#

But isn't the problem that I am trying to import a server component into a client component?

fading cloak
#

well, lets if wrapping will work

summer summit
#

type BlurDataProps = {
    src: string;
};

export default async function BlurData({ src }: BlurDataProps) {
    const res = await fetch(src);
    const arrayBuffer = await res.arrayBuffer();
    const buffer = Buffer.from(arrayBuffer);
    const base64String = buffer.toString('base64');

    return (
        <Image
            src={src}
            alt="Generated icon"
            height="250"
            width="250"
            priority={true}
            placeholder="blur"
            blurDataURL={base64String}
        />
    );
}```

I did the following, and now getting

Error: async/await is not yet supported in Client Components, only Server Components. This error is often caused by accidentally adding `'use client'` to a module that was originally written for the server.
#

alright

fading cloak
#

is BlurData client-component?

#

I forgot

summer summit
#

nope

#

server

fading cloak
#

try doing console.log() in the isolated server-side component and see what happens

#

if it will still work

#

just the getPlaiceholder component

summer summit
#

now I am getting

#

ReferenceError: require is not defined

#

Object.sharp

#

Idk

#

Nothing seems to work

fading cloak
#

at least we know that problem is with the plaiceholder

summer summit
#

Yeah, I might find another way of doing this

#

Too much stuff going on

fading cloak
summer summit
#

How to do that?

shut zodiacBOT
#
✅ Success!

This question has been marked as answered! If you have any other questions, feel free to create another post

Jump to answer