jss icon indicating copy to clipboard operation
jss copied to clipboard

NextImage loader doesn't correctly set the width and height params

Open mg-aceik opened this issue 1 year ago • 2 comments

Describe the Bug

NextImage loader doesn't correctly set the width and height params on the url depending on the fill value. This results in Sitecore image processor to add black borders to the image depending on its size.

It returns srcset with values like /-/jssmedia/test.jpg?h=1705&iar=0&w=2740&rev=7a67deeaa84446e69d7bbafbe2d4baf4&mw=640

To Reproduce

<NextImage
  field={props.fields.image}
  fill 
  sizes="100vw" />

Expected Behavior

srcset with values like /-/jssmedia/test.jpg?iar=0&amp;rev=7a67deeaa84446e69d7bbafbe2d4baf4&amp;mw=640 In other words, strip out the height and width params.

Possible Fix

Update the sitecoreLoader to remove these params if the fill param is passed, similar to how it is done on the props themselves.

Provide environment information

  • Sitecore Version: XM Cloud
  • JSS Version: 21.5.0

mg-aceik avatar Oct 31 '23 01:10 mg-aceik

I'm experiencing the same issue. I've written a custom loader to remove the h and w properties, and I use it in the image component when I add the fill attribute.

// src/lib/sitecore-loader-override

import { ImageLoader, ImageLoaderProps } from 'next/image';

export const sitecoreLoader: ImageLoader = ({
  src,
  width,
}: ImageLoaderProps): string => {
  const [root, paramString] = src.split('?');
  const params = new URLSearchParams(paramString);
  params.set('mw', width.toString());

  // custom change
  params.delete('h');
  params.delete('w');

  return `${root}?${params}`;
};
import { sitecoreLoader } from 'lib/sitecore-loader-override';

<NextImage
  field={props.fields.image}
  fill
  loader={sitecoreLoader}
/>

Juansereina avatar Nov 02 '23 16:11 Juansereina

@mg-aceik Thank you for submitting your contribution! I've added a ticket containing all the details. It has been added to our internal backlog for thorough review

illiakovalenko avatar Nov 20 '23 13:11 illiakovalenko