Skip to content
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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix lint
  • Loading branch information
styfle committed Sep 26, 2024
commit 542acef7390b03702946a34b216f9bc5ee427c4a
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,8 @@ export default function Page() {

> **Warning:** Dynamic `await import()` or `require()` are _not_ supported. The `import` must be static so it can be analyzed at build time.


You can optionally configure `localPatterns` in your `next.config.js` file in order to allow specific images and block all others.


```js filename="next.config.js"
module.exports = {
images: {
Expand Down
2 changes: 0 additions & 2 deletions docs/02-app/02-api-reference/01-components/image.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -490,12 +490,10 @@ Other properties on the `<Image />` component will be passed to the underlying

In addition to props, you can configure the Image Component in `next.config.js`. The following options are available:


## `localPatterns`

You can optionally configure `localPatterns` in your `next.config.js` file in order to allow specific paths to be optimized and block all others paths.


```js filename="next.config.js"
module.exports = {
images: {
Expand Down
4 changes: 3 additions & 1 deletion packages/next/src/client/legacy/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ function defaultLoader({
process.env.NEXT_RUNTIME !== 'edge'
) {
// We use dynamic require because this should only error in development
const { hasRemoteMatch } = require('../../shared/lib/match-remote-pattern')
const {
hasRemoteMatch,
} = require('../../shared/lib/match-remote-pattern')
if (!hasRemoteMatch(config.domains, config.remotePatterns, parsedSrc)) {
Copy link
Member

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?

Copy link
Member Author

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

throw new Error(
`Invalid src prop (${src}) on \`next/image\`, hostname "${parsedSrc.hostname}" is not configured under images in your \`next.config.js\`\n` +
Expand Down
3 changes: 1 addition & 2 deletions packages/next/src/shared/lib/image-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export type ImageLoaderPropsWithConfig = ImageLoaderProps & {
config: Readonly<ImageConfig>
}


export type LocalPattern = {
/**
* Can be literal or wildcard.
Expand Down Expand Up @@ -139,6 +138,6 @@ export const imageConfigDefault: ImageConfigComplete = {
contentSecurityPolicy: `script-src 'none'; frame-src 'none'; sandbox;`,
contentDispositionType: 'attachment',
localPatterns: undefined, // default: allow all local images
remotePatterns: [], // default: allow no remote images
remotePatterns: [], // default: allow no remote images
unoptimized: false,
}
1 change: 0 additions & 1 deletion packages/next/src/shared/lib/match-local-pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ export function hasLocalMatch(
}
const url = new URL(urlPathAndQuery, 'http://n')
return localPatterns.some((p) => matchLocalPattern(p, url))

}