-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(next/image): add
images.localPatterns
config (#70802)
- Backport #70529 to `14.2.x`
- Loading branch information
Showing
33 changed files
with
598 additions
and
23 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
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,29 @@ | ||
--- | ||
title: '`next/image` Un-configured localPatterns' | ||
--- | ||
|
||
## Why This Error Occurred | ||
|
||
One of your pages that leverages the `next/image` component, passed a `src` value that uses a URL that isn't defined in the `images.localPatterns` property in `next.config.js`. | ||
|
||
## Possible Ways to Fix It | ||
|
||
Add an entry to `images.localPatterns` array in `next.config.js` with the expected URL pattern. For example: | ||
|
||
```js filename="next.config.js" | ||
module.exports = { | ||
images: { | ||
localPatterns: [ | ||
{ | ||
pathname: '/assets/**', | ||
search: '', | ||
}, | ||
], | ||
}, | ||
} | ||
``` | ||
|
||
## Useful Links | ||
|
||
- [Image Optimization Documentation](/docs/pages/building-your-application/optimizing/images) | ||
- [Local Patterns Documentation](/docs/pages/api-reference/components/image#localpatterns) |
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
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
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,29 @@ | ||
import type { LocalPattern } from './image-config' | ||
import { makeRe } from 'next/dist/compiled/picomatch' | ||
|
||
// Modifying this function should also modify writeImagesManifest() | ||
export function matchLocalPattern(pattern: LocalPattern, url: URL): boolean { | ||
if (pattern.search !== undefined) { | ||
if (pattern.search !== url.search) { | ||
return false | ||
} | ||
} | ||
|
||
if (!makeRe(pattern.pathname ?? '**', { dot: true }).test(url.pathname)) { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
export function hasLocalMatch( | ||
localPatterns: LocalPattern[] | undefined, | ||
urlPathAndQuery: string | ||
): boolean { | ||
if (!localPatterns) { | ||
// if the user didn't define "localPatterns", we allow all local images | ||
return true | ||
} | ||
const url = new URL(urlPathAndQuery, 'http://n') | ||
return localPatterns.some((p) => matchLocalPattern(p, url)) | ||
} |
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.