Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show error message when using legacy props on new
next/image
(#41930)
This PR shows a better error message when using legacy props on new `next/image` (introduced in Next.js 13) and makes the codemod more discoverable.
- Loading branch information
Showing
17 changed files
with
230 additions
and
72 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,40 @@ | ||
# `next/image` changed in version 13 | ||
|
||
#### Why This Error Occurred | ||
|
||
Starting in Next.js 13, the `next/image` component has undergone some major changes. | ||
|
||
Compared to the legacy component, the new `next/image` component has the following changes: | ||
|
||
- Removes `<span>` wrapper around `<img>` in favor of [native computed aspect ratio](https://caniuse.com/mdn-html_elements_img_aspect_ratio_computed_from_attributes) | ||
- Adds support for canonical `style` prop | ||
- Removes `layout` prop in favor of `style` or `className` | ||
- Removes `objectFit` prop in favor of `style` or `className` | ||
- Removes `objectPosition` prop in favor of `style` or `className` | ||
- Removes `IntersectionObserver` implementation in favor of [native lazy loading](https://caniuse.com/loading-lazy-attr) | ||
- Removes `lazyBoundary` prop since there is no native equivalent | ||
- Removes `lazyRoot` prop since there is no native equivalent | ||
- Removes `loader` config in favor of [`loader`](#loader) prop | ||
- Changed `alt` prop from optional to required | ||
|
||
#### Possible Ways to Fix It | ||
|
||
Run the [next-image-to-legacy-image](https://nextjs.org/docs/advanced-features/codemods#next-image-to-legacy-image) codemod to automatically change `next/image` imports to `next/legacy/image`, for example: | ||
|
||
``` | ||
npx @next/codemod next-image-to-legacy-image . | ||
``` | ||
|
||
After running this codemod, you can optionally upgrade `next/legacy/image` to the new `next/image` with another codemod, for example: | ||
|
||
``` | ||
npx @next/codemod next-image-experimental . | ||
``` | ||
|
||
Please note this second codemod is experimental and only covers static usage, not dynamic usage (such `<Image {...props} />`). | ||
|
||
### Useful Links | ||
|
||
- [Next.js 13 Blog Post](https://nextjs.org/blog/next-13) | ||
- [`next/image` Documentation](https://nextjs.org/docs/api-reference/next/image) | ||
- [`next/legacy/image` Documentation](https://nextjs.org/docs/api-reference/next/legacy/image) |
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
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
18 changes: 0 additions & 18 deletions
18
test/integration/next-image-new/default/pages/invalid-objectfit.js
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
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
14 changes: 14 additions & 0 deletions
14
test/integration/next-image-new/invalid-layout/pages/index.js
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,14 @@ | ||
import React from 'react' | ||
import Image from 'next/image' | ||
import logo from '../public/logo.png' | ||
|
||
const Page = () => { | ||
return ( | ||
<div> | ||
<h1>Should not use "layout" prop</h1> | ||
<Image id="invalid-layout" layout="responsive" src={logo} /> | ||
</div> | ||
) | ||
} | ||
|
||
export default Page |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 66 additions & 0 deletions
66
test/integration/next-image-new/invalid-layout/test/index.test.ts
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,66 @@ | ||
/* eslint-env jest */ | ||
|
||
import { join } from 'path' | ||
import { | ||
findPort, | ||
hasRedbox, | ||
killApp, | ||
launchApp, | ||
nextBuild, | ||
} from 'next-test-utils' | ||
import webdriver from 'next-webdriver' | ||
|
||
const appDir = join(__dirname, '../') | ||
let appPort: number | ||
let app | ||
let stderr = '' | ||
const msg = | ||
/Error: Image with src "(.*)logo(.*)png" has legacy prop "layout". Did you forget to run the codemod?./ | ||
|
||
function runTests({ isDev }) { | ||
it('should show error', async () => { | ||
if (isDev) { | ||
const browser = await webdriver(appPort, '/') | ||
expect(await hasRedbox(browser, true)).toBe(true) | ||
expect(stderr).toMatch(msg) | ||
} else { | ||
expect(stderr).toMatch(msg) | ||
} | ||
}) | ||
} | ||
|
||
describe('Missing Import Image Tests', () => { | ||
describe('dev mode', () => { | ||
beforeAll(async () => { | ||
stderr = '' | ||
appPort = await findPort() | ||
app = await launchApp(appDir, appPort, { | ||
onStderr(msg) { | ||
stderr += msg || '' | ||
}, | ||
}) | ||
}) | ||
afterAll(async () => { | ||
if (app) { | ||
await killApp(app) | ||
} | ||
}) | ||
|
||
runTests({ isDev: true }) | ||
}) | ||
|
||
describe('server mode', () => { | ||
beforeAll(async () => { | ||
stderr = '' | ||
const result = await nextBuild(appDir, [], { stderr: true }) | ||
stderr = result.stderr | ||
}) | ||
afterAll(async () => { | ||
if (app) { | ||
await killApp(app) | ||
} | ||
}) | ||
|
||
runTests({ isDev: false }) | ||
}) | ||
}) |
14 changes: 14 additions & 0 deletions
14
test/integration/next-image-new/invalid-objectfit/pages/index.js
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,14 @@ | ||
import React from 'react' | ||
import Image from 'next/image' | ||
import logo from '../public/logo.png' | ||
|
||
const Page = () => { | ||
return ( | ||
<div> | ||
<h1>Should not use "objectFit" prop</h1> | ||
<Image id="invalid-layout" objectFit="contain" src={logo} /> | ||
</div> | ||
) | ||
} | ||
|
||
export default Page |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 66 additions & 0 deletions
66
test/integration/next-image-new/invalid-objectfit/test/index.test.ts
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,66 @@ | ||
/* eslint-env jest */ | ||
|
||
import { join } from 'path' | ||
import { | ||
findPort, | ||
hasRedbox, | ||
killApp, | ||
launchApp, | ||
nextBuild, | ||
} from 'next-test-utils' | ||
import webdriver from 'next-webdriver' | ||
|
||
const appDir = join(__dirname, '../') | ||
let appPort: number | ||
let app | ||
let stderr = '' | ||
const msg = | ||
/Error: Image with src "(.*)logo(.*)png" has legacy prop "objectFit". Did you forget to run the codemod?./ | ||
|
||
function runTests({ isDev }) { | ||
it('should show error', async () => { | ||
if (isDev) { | ||
const browser = await webdriver(appPort, '/') | ||
expect(await hasRedbox(browser, true)).toBe(true) | ||
expect(stderr).toMatch(msg) | ||
} else { | ||
expect(stderr).toMatch(msg) | ||
} | ||
}) | ||
} | ||
|
||
describe('Missing Import Image Tests', () => { | ||
describe('dev mode', () => { | ||
beforeAll(async () => { | ||
stderr = '' | ||
appPort = await findPort() | ||
app = await launchApp(appDir, appPort, { | ||
onStderr(msg) { | ||
stderr += msg || '' | ||
}, | ||
}) | ||
}) | ||
afterAll(async () => { | ||
if (app) { | ||
await killApp(app) | ||
} | ||
}) | ||
|
||
runTests({ isDev: true }) | ||
}) | ||
|
||
describe('server mode', () => { | ||
beforeAll(async () => { | ||
stderr = '' | ||
const result = await nextBuild(appDir, [], { stderr: true }) | ||
stderr = result.stderr | ||
}) | ||
afterAll(async () => { | ||
if (app) { | ||
await killApp(app) | ||
} | ||
}) | ||
|
||
runTests({ isDev: false }) | ||
}) | ||
}) |