-
Notifications
You must be signed in to change notification settings - Fork 29.5k
[Breaking] Require images.localPatterns
for query in Image src
#84406
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
base: canary
Are you sure you want to change the base?
Changes from all commits
5ba1773
2e64cbc
0f57f54
34a8bdd
e386c65
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ module.exports = { | |
localPatterns: [ | ||
{ | ||
pathname: '/assets/**', | ||
search: '?v=2', | ||
}, | ||
], | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,17 @@ function defaultLoader({ | |
) | ||
} | ||
} | ||
|
||
if ( | ||
src.includes('?') && | ||
config.localPatterns.length === 1 && | ||
config.localPatterns[0].pathname === '/_next/static/media/**' | ||
) { | ||
throw new Error( | ||
`Image with src "${src}" is using a query string which requires images.localPatterns configuration.` + | ||
`\nRead more: https://nextjs.org/docs/messages/next-image-unconfigured-localpatterns` | ||
) | ||
} | ||
Comment on lines
+50
to
+59
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. The query string validation for local images only runs when View Details📝 Patch Detailsdiff --git a/packages/next/src/shared/lib/image-loader.ts b/packages/next/src/shared/lib/image-loader.ts
index 23a6d4af10..afa55be73c 100644
--- a/packages/next/src/shared/lib/image-loader.ts
+++ b/packages/next/src/shared/lib/image-loader.ts
@@ -30,27 +30,30 @@ function defaultLoader({
)
}
- if (src.startsWith('/') && config.localPatterns) {
- if (
- process.env.NODE_ENV !== 'test' &&
- // micromatch isn't compatible with edge runtime
- process.env.NEXT_RUNTIME !== 'edge'
- ) {
- // We use dynamic require because this should only error in development
- const { hasLocalMatch } =
- require('./match-local-pattern') as typeof import('./match-local-pattern')
- if (!hasLocalMatch(config.localPatterns, src)) {
- throw new Error(
- `Invalid src prop (${src}) on \`next/image\` does not match \`images.localPatterns\` configured in your \`next.config.js\`\n` +
- `See more info: https://nextjs.org/docs/messages/next-image-unconfigured-localpatterns`
- )
+ if (src.startsWith('/')) {
+ if (config.localPatterns) {
+ if (
+ process.env.NODE_ENV !== 'test' &&
+ // micromatch isn't compatible with edge runtime
+ process.env.NEXT_RUNTIME !== 'edge'
+ ) {
+ // We use dynamic require because this should only error in development
+ const { hasLocalMatch } =
+ require('./match-local-pattern') as typeof import('./match-local-pattern')
+ if (!hasLocalMatch(config.localPatterns, src)) {
+ throw new Error(
+ `Invalid src prop (${src}) on \`next/image\` does not match \`images.localPatterns\` configured in your \`next.config.js\`\n` +
+ `See more info: https://nextjs.org/docs/messages/next-image-unconfigured-localpatterns`
+ )
+ }
}
}
if (
src.includes('?') &&
- config.localPatterns.length === 1 &&
- config.localPatterns[0].pathname === '/_next/static/media/**'
+ (!config.localPatterns?.length ||
+ (config.localPatterns.length === 1 &&
+ config.localPatterns[0].pathname === '/_next/static/media/**'))
) {
throw new Error(
`Image with src "${src}" is using a query string which requires images.localPatterns configuration.` +
AnalysisQuery string validation skipped for local images when
|
||
} | ||
|
||
if (!src.startsWith('/') && (config.domains || config.remotePatterns)) { | ||
|
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.
This one will also need to be moved (I think legacy might use a different loader)