Skip to content

Commit 3399500

Browse files
[Next.js] Render "unoptimized" Next Image in component rendering mode (#2068)
1 parent 9d505ba commit 3399500

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Our versioning strategy is as follows:
1313

1414
### 🎉 New Features & Improvements
1515

16-
* `[nextjs][sitecore-jss-nextjs]` Support for Component Library feature in XMCloud ([#1987](https://github.com/Sitecore/jss/pull/1987))([#2000](https://github.com/Sitecore/jss/pull/2000))([#2002](https://github.com/Sitecore/jss/pull/2002))([#2005](https://github.com/Sitecore/jss/pull/2005))([#2024](https://github.com/Sitecore/jss/pull/2024))([#2053](https://github.com/Sitecore/jss/pull/2053))([#2059](https://github.com/Sitecore/jss/pull/2059))([#2064](https://github.com/Sitecore/jss/pull/2064))
16+
* `[nextjs][sitecore-jss-nextjs]` Support for Component Library in XMCloud ([#1987](https://github.com/Sitecore/jss/pull/1987))([#2000](https://github.com/Sitecore/jss/pull/2000))([#2002](https://github.com/Sitecore/jss/pull/2002))([#2005](https://github.com/Sitecore/jss/pull/2005))([#2024](https://github.com/Sitecore/jss/pull/2024))([#2053](https://github.com/Sitecore/jss/pull/2053))([#2059](https://github.com/Sitecore/jss/pull/2059))([#2064](https://github.com/Sitecore/jss/pull/2064))([#2068](https://github.com/Sitecore/jss/pull/2068))
1717
* `[create-sitecore-jss]` Create apps with exact jss dependency versions for canary and beta releases; all apps are now created with v0.1.0 instead of the version of JSS ([#2032](https://github.com/Sitecore/jss/pull/2032))
1818

1919
### 🐛 Bug Fixes

packages/sitecore-jss-nextjs/src/components/NextImage.test.tsx

+17
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
LayoutServicePageState,
1111
SitecoreContextReactContext,
1212
} from '@sitecore-jss/sitecore-jss-react';
13+
import { RenderingType } from '@sitecore-jss/sitecore-jss/src/layout';
1314
import Image, { ImageLoader } from 'next/image';
1415
import { spy, match } from 'sinon';
1516
import sinonChai from 'sinon-chai';
@@ -563,6 +564,22 @@ describe('<NextImage />', () => {
563564
expect(rendered.prop('unoptimized')).to.equal(true);
564565
});
565566

567+
it('should render unoptimized image in component rendering type', () => {
568+
const testEditingContext = {
569+
...testContextProps,
570+
context: {
571+
...testContextProps.context,
572+
renderingType: RenderingType.Component,
573+
},
574+
};
575+
const rendered = mount(
576+
<SitecoreContextReactContext.Provider value={testEditingContext}>
577+
<NextImage loader={mockLoader} {...props} />
578+
</SitecoreContextReactContext.Provider>
579+
).find(Image);
580+
expect(rendered.prop('unoptimized')).to.equal(true);
581+
});
582+
566583
it('should render respect original unoptimized value in normal mode', () => {
567584
const modifiedProps = {
568585
...props,

packages/sitecore-jss-nextjs/src/components/NextImage.tsx

+9-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ import {
88
ImageFieldValue,
99
withFieldMetadata,
1010
SitecoreContextReactContext,
11+
DefaultEmptyFieldEditingComponentImage,
12+
withEmptyFieldEditingComponent,
1113
} from '@sitecore-jss/sitecore-jss-react';
1214
import Image, { ImageProps as NextImageProperties } from 'next/image';
13-
import { withEmptyFieldEditingComponent } from '@sitecore-jss/sitecore-jss-react';
14-
import { DefaultEmptyFieldEditingComponentImage } from '@sitecore-jss/sitecore-jss-react';
15-
import { isFieldValueEmpty, LayoutServicePageState } from '@sitecore-jss/sitecore-jss/layout';
15+
import {
16+
isFieldValueEmpty,
17+
LayoutServicePageState,
18+
RenderingType,
19+
} from '@sitecore-jss/sitecore-jss/layout';
1620

1721
type NextImageProps = ImageProps & Partial<NextImageProperties>;
1822
export const NextImage: React.FC<NextImageProps> = withFieldMetadata<NextImageProps>(
@@ -51,9 +55,10 @@ export const NextImage: React.FC<NextImageProps> = withFieldMetadata<NextImagePr
5155
return null;
5256
}
5357

54-
// disable image optimization for Edit and Preview, but preserve original value if true
58+
// disable image optimization for Edit / Preview / Component rendering, but preserve original value if true
5559
const unoptimized =
5660
otherProps.unoptimized ||
61+
sitecoreContext.context?.renderingType === RenderingType.Component ||
5762
sitecoreContext.context?.pageState !== LayoutServicePageState.Normal;
5863

5964
const attrs = {

0 commit comments

Comments
 (0)