-
Notifications
You must be signed in to change notification settings - Fork 27.3k
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
feat(next/image): add images.localPatterns
config
#70529
Changes from all commits
5ce2dd9
6fb98b6
542acef
2ea111c
4d3c093
5ec4450
6a6eacb
c02b014
918aae4
dac699b
246d0c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does ordering on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes the order matters because I don't think CDN's changing it will be very common since this is going to be the value passed to the In the future, we could add support for any order using an an object or array. |
||
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)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this check be present for
localPatterns
as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! I had it on the new image component but not the legacy image component.
Added in 246d0c2