cloudinary-community / next-cloudinary

⚡️ High-performance image delivery and uploading at scale in Next.js powered by Cloudinary.
https://next.cloudinary.dev
MIT License
250 stars 68 forks source link

Reduce Client Bundle Size #519

Open wottpal opened 2 weeks ago

wottpal commented 2 weeks ago

I was wondering if the noticeable client bundle size of next-cloudinary is justified somehow? I'm only using CldImage within a client component in this case.

Could the package maybe split into multiple parts?

CleanShot 2024-09-19 at 11 08 35@2x

colbyfayock commented 2 weeks ago

Hey @wottpal we're currently working on how this project is bundled and hope to improve this as part of that project, with part of the primary goal being to provide better support for using the components in RSCs. will update when we have more to share

wottpal commented 1 week ago

Nice, also the RSC part is great news.

A bit unrelated but speaking of RSC, I've built myself a server component wrapper around CldImage to support placeholder="blur" in Next.js. Sharing it here in case that's useful for anyone or even the core package:

const fetchImageDataUrl = unstable_cache(
  async (url) => {
    const response = await fetch(url)
    const arrayBuffer = await response.arrayBuffer()
    const base64String = Buffer.from(arrayBuffer).toString('base64')
    const mimeType = response.headers.get('Content-Type') || 'image/jpeg'
    const dataUrl = `data:${mimeType};base64,${base64String}`
    return dataUrl
  },
  ['image-data-url'],
)

interface CldImageWithPlaceholderProps extends ImageProps {
  src: string
}
export default async function CldImageWithPlaceholder({
  src,
  ...props
}: CldImageWithPlaceholderProps) {
  if (props.placeholder === 'blur') {
    try {
      const url = getCldImageUrl({
        src,
        width: 10,
        height: 10,
        format: 'jpg',
      })
      props.blurDataURL = await fetchImageDataUrl(url)
    } catch (error) {
      props.placeholder = 'empty'
      console.error(`Error fetching Cloudinary URL (${src}):`, error)
    }
  }

  return <CldImage src={src} {...props} />
}
colbyfayock commented 1 week ago

oh nice one! i did something somewhat similar in an example in this guide: https://next.cloudinary.dev/guides/placeholders

wottpal commented 1 week ago

Uh nice one, didn't saw that. would have saved me some time haha.

You can close this issue or use it to track the module restructurment progress – Feel free to handle it as you like.