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

NextJS: Allow disabling next/image lazy loading #21909

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions code/e2e-tests/framework-nextjs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ test.describe('Next.js', () => {
await new SbPage(page).waitUntilLoaded();
});

test.describe('next/image', () => {
let sbPage: SbPage;

test.beforeEach(async ({ page }) => {
sbPage = new SbPage(page);
});

test('should lazy load images by default', async () => {
await sbPage.navigateToStory('frameworks/nextjs/Image', 'lazy');

const img = sbPage.previewRoot().locator('img');

expect(await img.evaluate<boolean, HTMLImageElement>((image) => image.complete)).toBeFalsy();
valentinpalkovic marked this conversation as resolved.
Show resolved Hide resolved
});

test('should eager load images when loading parameter is set to eager', async () => {
await sbPage.navigateToStory('frameworks/nextjs/Image', 'eager');

const img = sbPage.previewRoot().locator('img');

expect(await img.evaluate<boolean, HTMLImageElement>((image) => image.complete)).toBeTruthy();
});
});

test.describe('next/navigation', () => {
let root: Locator;
let sbPage: SbPage;
Expand Down
4 changes: 4 additions & 0 deletions code/frameworks/nextjs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,16 @@ export default {
framework: {
name: '@storybook/nextjs',
options: {
image: {
loading: 'eager',
},
nextConfigPath: path.resolve(__dirname, '../next.config.js'),
},
},
};
```

- `image`: Props to pass to every instance of `next/image`
valentinpalkovic marked this conversation as resolved.
Show resolved Hide resolved
- `nextConfigPath`: The absolute path to the `next.config.js`

### Next.js's Image Component
Expand Down
16 changes: 16 additions & 0 deletions code/frameworks/nextjs/src/images/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { createContext } from 'react';
import type { ImageProps, StaticImageData } from 'next/image';
import type { ImageProps as LegacyImageProps } from 'next/legacy/image';

// StaticRequire needs to be in scope for the TypeScript compiler to work.
// See: https://github.com/microsoft/TypeScript/issues/5711
// Since next/image doesn't export StaticRequire we need to re-define it here and set src's type to it.
valentinpalkovic marked this conversation as resolved.
Show resolved Hide resolved
interface StaticRequire {
default: StaticImageData;
}

declare type StaticImport = StaticRequire | StaticImageData;

export const ImageContext = createContext<
Partial<Omit<ImageProps, 'src'> & { src: string | StaticImport }> & Omit<LegacyImageProps, 'src'>
>({});
18 changes: 18 additions & 0 deletions code/frameworks/nextjs/src/images/decorator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as React from 'react';
import type { Addon_StoryContext } from '@storybook/types';
import { ImageContext } from './context';

export const ImageDecorator = (
Story: React.FC,
{ parameters }: Addon_StoryContext
): React.ReactNode => {
if (!parameters.nextjs?.image) {
return <Story />;
}

return (
<ImageContext.Provider value={parameters.nextjs.image}>
<Story />
</ImageContext.Provider>
);
valentinpalkovic marked this conversation as resolved.
Show resolved Hide resolved
};
35 changes: 28 additions & 7 deletions code/frameworks/nextjs/src/images/next-image-stub.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as React from 'react';
import type * as _NextImage from 'next/image';
import type * as _NextLegacyImage from 'next/legacy/image';
import semver from 'semver';
import { ImageContext } from './context';

const defaultLoader = ({ src, width, quality }: _NextImage.ImageLoaderProps) => {
const missingValues = [];
Expand Down Expand Up @@ -38,7 +39,11 @@ const OriginalNextImage = NextImage.default;
Object.defineProperty(NextImage, 'default', {
configurable: true,
value: (props: _NextImage.ImageProps) => {
return <OriginalNextImage {...props} loader={props.loader ?? defaultLoader} />;
const imageParameters = React.useContext(ImageContext);

return (
<OriginalNextImage {...imageParameters} {...props} loader={props.loader ?? defaultLoader} />
);
},
});

Expand All @@ -48,9 +53,17 @@ if (semver.satisfies(process.env.__NEXT_VERSION!, '^13.0.0')) {

Object.defineProperty(OriginalNextLegacyImage, 'default', {
configurable: true,
value: (props: _NextLegacyImage.ImageProps) => (
<OriginalNextLegacyImage {...props} loader={props.loader ?? defaultLoader} />
),
value: (props: _NextLegacyImage.ImageProps) => {
const imageParameters = React.useContext(ImageContext);

return (
<OriginalNextLegacyImage
{...imageParameters}
{...props}
loader={props.loader ?? defaultLoader}
/>
);
},
});
}

Expand All @@ -60,8 +73,16 @@ if (semver.satisfies(process.env.__NEXT_VERSION!, '^12.2.0')) {

Object.defineProperty(OriginalNextFutureImage, 'default', {
configurable: true,
value: (props: _NextImage.ImageProps) => (
<OriginalNextFutureImage {...props} loader={props.loader ?? defaultLoader} />
),
value: (props: _NextImage.ImageProps) => {
const imageParameters = React.useContext(ImageContext);

return (
<OriginalNextFutureImage
{...imageParameters}
{...props}
loader={props.loader ?? defaultLoader}
/>
);
},
});
}
8 changes: 7 additions & 1 deletion code/frameworks/nextjs/src/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './config/preview';
import { ImageDecorator } from './images/decorator';
import { RouterDecorator } from './routing/decorator';
import { StyledJsxDecorator } from './styledJsx/decorator';
import './images/next-image-stub';
Expand All @@ -13,7 +14,12 @@ function addNextHeadCount() {

addNextHeadCount();

export const decorators = [StyledJsxDecorator, RouterDecorator, HeadManagerDecorator];
export const decorators = [
StyledJsxDecorator,
ImageDecorator,
RouterDecorator,
HeadManagerDecorator,
];

export const parameters = {
docs: {
Expand Down
27 changes: 27 additions & 0 deletions code/frameworks/nextjs/template/stories/Image.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,30 @@ export const Sized = {
],
},
};

export const Lazy = {
args: {
src: 'https://storybook.js.org/images/placeholders/50x50.png',
width: 50,
height: 50,
},
decorators: [
(Story) => (
<>
<div style={{ height: '200vh' }} />
valentinpalkovic marked this conversation as resolved.
Show resolved Hide resolved
{Story()}
</>
),
],
};

export const Eager = {
...Lazy,
parameters: {
nextjs: {
image: {
loading: 'eager',
},
valentinpalkovic marked this conversation as resolved.
Show resolved Hide resolved
},
},
};