-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23296 from storybookjs/valentin/fix-next-images-i…
…n-latest-release Next.js: Fix next/image usage in latest Next.js release
- Loading branch information
Showing
10 changed files
with
175 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React from 'react'; | ||
import type * as _NextImage from 'next/image'; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore import is aliased in webpack config | ||
import OriginalNextFutureImage from 'sb-original/next/future/image'; | ||
import { ImageContext } from './context'; | ||
import { defaultLoader } from './next-image-default-loader'; | ||
|
||
function NextFutureImage(props: _NextImage.ImageProps) { | ||
const imageParameters = React.useContext(ImageContext); | ||
|
||
return ( | ||
<OriginalNextFutureImage | ||
{...imageParameters} | ||
{...props} | ||
loader={props.loader ?? defaultLoader} | ||
/> | ||
); | ||
} | ||
|
||
export default NextFutureImage; |
28 changes: 28 additions & 0 deletions
28
code/frameworks/nextjs/src/images/next-image-default-loader.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type * as _NextImage from 'next/image'; | ||
|
||
export const defaultLoader = ({ src, width, quality }: _NextImage.ImageLoaderProps) => { | ||
const missingValues = []; | ||
if (!src) { | ||
missingValues.push('src'); | ||
} | ||
|
||
if (!width) { | ||
missingValues.push('width'); | ||
} | ||
|
||
if (missingValues.length > 0) { | ||
throw new Error( | ||
`Next Image Optimization requires ${missingValues.join( | ||
', ' | ||
)} to be provided. Make sure you pass them as props to the \`next/image\` component. Received: ${JSON.stringify( | ||
{ | ||
src, | ||
width, | ||
quality, | ||
} | ||
)}` | ||
); | ||
} | ||
|
||
return `${src}?w=${width}&q=${quality ?? 75}`; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore import is aliased in webpack config | ||
import OriginalNextImage from 'sb-original/next/image'; | ||
import type * as _NextImage from 'next/image'; | ||
import React from 'react'; | ||
import { ImageContext } from './context'; | ||
import { defaultLoader } from './next-image-default-loader'; | ||
|
||
const MockedNextImage = (props: _NextImage.ImageProps) => { | ||
const imageParameters = React.useContext(ImageContext); | ||
|
||
return ( | ||
<OriginalNextImage {...imageParameters} {...props} loader={props.loader ?? defaultLoader} /> | ||
); | ||
}; | ||
|
||
export default MockedNextImage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore import is aliased in webpack config | ||
import OriginalNextLegacyImage from 'sb-original/next/legacy/image'; | ||
import type * as _NextLegacyImage from 'next/legacy/image'; | ||
import React from 'react'; | ||
import { ImageContext } from './context'; | ||
import { defaultLoader } from './next-image-default-loader'; | ||
|
||
function NextLegacyImage(props: _NextLegacyImage.ImageProps) { | ||
const imageParameters = React.useContext(ImageContext); | ||
|
||
return ( | ||
<OriginalNextLegacyImage | ||
{...imageParameters} | ||
{...props} | ||
loader={props.loader ?? defaultLoader} | ||
/> | ||
); | ||
} | ||
|
||
export default NextLegacyImage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.