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

there is a bug in highly optimized images for the size of 200 #47

Open
4 tasks done
chidam333 opened this issue Mar 18, 2025 · 3 comments
Open
4 tasks done

there is a bug in highly optimized images for the size of 200 #47

chidam333 opened this issue Mar 18, 2025 · 3 comments

Comments

@chidam333
Copy link

Initial checklist

Affected package

I am a noob

Steps to reproduce

Hmm nothing much i know of

Actual behavior

Image

while i am loading the page the link below is the where it is fetching the image from
unifiedjs.com/image/prettier-dark.png,/image/prettier-dark-200.pngIt is obviously wrong

Expected behavior

it should fetch from the below instead
unifiedjs.com/image/prettier-dark-200.png

I would also expect it to load same assets for similar internet speeds. In vivaldi it fetches size 200 but in chrome and edge it fetches 600.

Runtime

Browser

Package manager

NA

Operating system

Windows 11

Build and bundle tools

No response

@chidam333
Copy link
Author

if given some direction i can fix it ?

async function transformPng(file) {
  // Note: see `rehype-pictures` for the inverse.
  const sizes = [200, 600, 1200, 2000]
  const options = {
    png: {compressionLevel: 9, quality: 50},
    webp: {alphaQuality: 50, quality: 50}
  }
  const formats = /** @type {Array<keyof typeof options>} */ (
    Object.keys(options)
  )

  await read(file)
  const sharpPipeline = sharp(file.value)
  const metadata = await sharpPipeline.metadata()
  assert(metadata.width)

  const files = [file]

  for (const size of sizes) {
    if (size > metadata.width) continue

    for (const format of formats) {
      const buf = await sharpPipeline
        .clone()
        .resize(size)
        [format](options[format])
        .toBuffer()

      const copy = new VFile({path: file.path, value: buf})

      copy.stem += '-' + size
      copy.extname = '.' + format
      files.push(copy)
    }
  }

  return files
}

Is this the code i should focus on ?

@ChristianMurphy
Copy link
Member

It may be.
Try changing the code, building the site, and see if it fixes it!

@chidam333
Copy link
Author

Sure will get to you shortly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants