Skip to content

Commit

Permalink
Disable image optimization for edit/preview
Browse files Browse the repository at this point in the history
  • Loading branch information
art-alexeyenko committed Aug 13, 2024
1 parent c525b47 commit b1f3a8e
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/sitecore-jss-nextjs/src/components/NextImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import {
ImageField,
ImageFieldValue,
withFieldMetadata,
useSitecoreContext,
} from '@sitecore-jss/sitecore-jss-react';
import Image, { ImageProps as NextImageProperties } from 'next/image';
import { withEmptyFieldEditingComponent } from '@sitecore-jss/sitecore-jss-react';
import { DefaultEmptyFieldEditingComponentImage } from '@sitecore-jss/sitecore-jss-react';
import { isFieldValueEmpty } from '@sitecore-jss/sitecore-jss/layout';
import { isFieldValueEmpty, LayoutServicePageState } from '@sitecore-jss/sitecore-jss/layout';

type NextImageProps = ImageProps & Partial<NextImageProperties>;
export const NextImage: React.FC<NextImageProps> = withFieldMetadata<NextImageProps>(
Expand Down Expand Up @@ -48,6 +49,11 @@ export const NextImage: React.FC<NextImageProps> = withFieldMetadata<NextImagePr
if (!img) {
return null;
}
const {
sitecoreContext: { pageState },
} = useSitecoreContext();
// disable image optimization for Edit and Preview
const unoptimized = pageState !== LayoutServicePageState.Normal;

const attrs = {
...img,
Expand All @@ -59,6 +65,7 @@ export const NextImage: React.FC<NextImageProps> = withFieldMetadata<NextImagePr
imageParams as { [paramName: string]: string | number },
mediaUrlPrefix as RegExp
),
unoptimized,
};

const imageProps = {
Expand Down

0 comments on commit b1f3a8e

Please sign in to comment.