-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: show the error message if
images.loaderFile
doesn't export a d…
…efault function (#64036) <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md --> fixes #63803 ## What I do? - If the loader file export the function as `named`, Next.js throws the error. - But this error is a bit confusing for the developers. - So I open this PR for showing the accurate error message. ## AS-IS / TO-BE ### AS-IS ``` TypeError: Cannot use 'in' operator to search for '__next_img_default' in undefined ``` <img width="1202" alt="스크린샷 2024-03-28 16 10 53" src="https://github.com/vercel/next.js/assets/33178048/e7c81cb5-7976-46ff-b86f-9c8fd9a7a681"> ### TO-BE ``` Error: The loader file must export a default function that returns a string. See more info here: https://nextjs.org/docs/messages/invalid-images-config ``` <img width="500" alt="스크린샷 2024-03-28 16 10 53" src="https://github.com/vercel/next.js/assets/33178048/c391e61b-6a44-4f85-8600-28ab6cb5b0eb"> --------- Co-authored-by: Steven <[email protected]>
- Loading branch information
Showing
8 changed files
with
104 additions
and
0 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
17 changes: 17 additions & 0 deletions
17
test/e2e/app-dir/loader-file-named-export-custom-loader-error/app/get-img-props/page.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,17 @@ | ||
import { getImageProps } from 'next/image' | ||
|
||
export default function Page() { | ||
const { props: imageProps } = getImageProps({ | ||
id: 'logo', | ||
alt: 'logo', | ||
src: '/logo.png', | ||
width: '400', | ||
height: '400', | ||
}) | ||
|
||
return ( | ||
<div> | ||
<img {...imageProps} /> | ||
</div> | ||
) | ||
} |
7 changes: 7 additions & 0 deletions
7
test/e2e/app-dir/loader-file-named-export-custom-loader-error/app/layout.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,7 @@ | ||
export default function Root({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
9 changes: 9 additions & 0 deletions
9
test/e2e/app-dir/loader-file-named-export-custom-loader-error/app/page.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,9 @@ | ||
import Image from 'next/image' | ||
|
||
export default function Page() { | ||
return ( | ||
<p> | ||
<Image id="logo" alt="logo" src="/logo.png" width="400" height="400" /> | ||
</p> | ||
) | ||
} |
3 changes: 3 additions & 0 deletions
3
test/e2e/app-dir/loader-file-named-export-custom-loader-error/dummy-loader.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,3 @@ | ||
export function dummyLoader({ src, width, quality }) { | ||
return `/_next/image/?url=${src}&w=${width}&q=${quality || 50}` | ||
} |
53 changes: 53 additions & 0 deletions
53
...ile-named-export-custom-loader-error/loader-file-named-export-custom-loader-error.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,53 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
import { getRedboxHeader, hasRedbox } from 'next-test-utils' | ||
|
||
const errorMessage = | ||
'images.loaderFile detected but the file is missing default export.\nRead more: https://nextjs.org/docs/messages/invalid-images-config' | ||
|
||
async function testDev(browser, errorRegex) { | ||
expect(await hasRedbox(browser)).toBe(true) | ||
expect(await getRedboxHeader(browser)).toMatch(errorRegex) | ||
} | ||
|
||
describe('Error test if the loader file export a named function', () => { | ||
describe('in Development', () => { | ||
const { next, isNextDev } = nextTestSetup({ | ||
skipDeployment: true, | ||
files: __dirname, | ||
}) | ||
|
||
;(isNextDev ? describe : describe.skip)('development only', () => { | ||
it('should show the error when using `Image` component', async () => { | ||
const browser = await next.browser('/') | ||
await testDev(browser, errorMessage) | ||
}) | ||
|
||
it('should show the error when using `getImageProps` method', async () => { | ||
const browser = await next.browser('/get-img-props') | ||
await testDev(browser, errorMessage) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('in Build and Start', () => { | ||
const { next, isNextStart } = nextTestSetup({ | ||
skipDeployment: true, | ||
skipStart: true, | ||
files: __dirname, | ||
}) | ||
|
||
// next build doesn't support turbopack yet | ||
// see https://nextjs.org/docs/architecture/turbopack#unsupported-features | ||
;(isNextStart && !process.env.TURBOPACK ? describe : describe.skip)( | ||
'build and start only', | ||
() => { | ||
it('should show the build error', async () => { | ||
await expect(next.start()).rejects.toThrow( | ||
'next build failed with code/signal 1' | ||
) | ||
expect(next.cliOutput).toContain(errorMessage) | ||
}) | ||
} | ||
) | ||
}) | ||
}) |
10 changes: 10 additions & 0 deletions
10
test/e2e/app-dir/loader-file-named-export-custom-loader-error/next.config.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,10 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = { | ||
images: { | ||
loaderFile: '/dummy-loader.ts', | ||
}, | ||
} | ||
|
||
module.exports = nextConfig |
Binary file added
BIN
+1.51 KB
test/e2e/app-dir/loader-file-named-export-custom-loader-error/public/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.