Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce Client Bundle Size #519

Open
wottpal opened this issue Sep 19, 2024 · 4 comments · May be fixed by #523
Open

Reduce Client Bundle Size #519

wottpal opened this issue Sep 19, 2024 · 4 comments · May be fixed by #523
Assignees
Labels
enhancement New feature or request question Further information is requested

Comments

@wottpal
Copy link

wottpal commented Sep 19, 2024

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
Copy link
Collaborator

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
Copy link
Author

wottpal commented Sep 20, 2024

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
Copy link
Collaborator

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

@wottpal
Copy link
Author

wottpal commented Sep 21, 2024

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.

@colbyfayock colbyfayock linked a pull request Sep 27, 2024 that will close this issue
@colbyfayock colbyfayock added enhancement New feature or request question Further information is requested labels Sep 27, 2024
@colbyfayock colbyfayock self-assigned this Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants