You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add experimental wildcard remotePatterns config for upstream images (#36245)
## Description
This PR implements a new configuration object in `next.config.js` called `experimental.images.remotePatterns`.
This will eventually deprecate `images.domains` because it covers the same use cases and more by allowing wildcard pattern matching on `hostname` and `pathname` and also allows restricting `protocol` and `port`.
## Feature
- [x] Implements an existing feature request.
- [x] Related issues linked
- [x] Unit tests added
- [x] Integration tests added
- [x] Documentation added
- [x] Telemetry added. In case of a feature if it's used or not.
- [x] Errors have helpful link attached, see `contributing.md`
## Related
- Fixes#27925
- Closes #18429
- Closes#18632
- Closes#18730
- Closes#27345
|`v12.1.1`|`style` prop added. Experimental[\*](#experimental-raw-layout-mode) support for `layout="raw"` added. |
20
21
|`v12.1.0`|`dangerouslyAllowSVG` and `contentSecurityPolicy` configuration added. |
21
22
|`v12.0.9`|`lazyRoot` prop added. |
@@ -313,9 +314,64 @@ Other properties on the `<Image />` component will be passed to the underlying
313
314
314
315
## Configuration Options
315
316
317
+
### Remote Patterns
318
+
319
+
> Note: The `remotePatterns` configuration is currently **experimental** and subject to change. Please use [`domains`](#domains) for production use cases.
320
+
321
+
To protect your application from malicious users, configuration is required in order to use external images. This ensures that only external images from your account can be served from the Next.js Image Optimization API. These external images can be configured with the `remotePatterns` property in your `next.config.js` file, as shown below:
322
+
323
+
```js
324
+
module.exports= {
325
+
experimental: {
326
+
images: {
327
+
remotePatterns: [
328
+
{
329
+
protocol:'https',
330
+
hostname:'example.com',
331
+
port:'',
332
+
pathname:'/account123/**',
333
+
},
334
+
],
335
+
},
336
+
},
337
+
}
338
+
```
339
+
340
+
> Note: The example above will ensure the `src` property of `next/image` must start with `https://example.com/account123/`. Any other protocol, hostname, port, or unmatched path will respond with 400 Bad Request.
341
+
342
+
Below is another example of the `remotePatterns` property in the `next.config.js` file:
343
+
344
+
```js
345
+
module.exports= {
346
+
experimental: {
347
+
images: {
348
+
remotePatterns: [
349
+
{
350
+
protocol:'https',
351
+
hostname:'**.example.com',
352
+
},
353
+
],
354
+
},
355
+
},
356
+
}
357
+
```
358
+
359
+
> Note: The example above will ensure the `src` property of `next/image` must start with `https://img1.example.com` or `https://me.avatar.example.com` or any number of subdomains. Any other protocol or unmatched hostname will respond with 400 Bad Request.
360
+
361
+
Wildcard patterns can be used for both `pathname` and `hostname` and have the following syntax:
362
+
363
+
-`*` match a single path segment or subdomain
364
+
-`**` match any number of path segments at the end or subdomains at the beginning
365
+
366
+
The `**` syntax does not work in the middle of the pattern.
367
+
316
368
### Domains
317
369
318
-
To protect your application from malicious users, you must define a list of image provider domains that you want to be served from the Next.js Image Optimization API. This is configured in with the `domains` property in your `next.config.js` file, as shown below:
370
+
Similar to [`remotePatterns`](#remote-patterns), the `domains` configuration can be used to provide a list of allowed hostnames for external images.
371
+
372
+
However, the `domains` configuration does not support wildcard pattern matching and it cannot restrict protocol, port, or pathname.
373
+
374
+
Below is an example of the `domains` property in the `next.config.js` file:
Copy file name to clipboardexpand all lines: docs/basic-features/image-optimization.md
+4-10
Original file line number
Diff line number
Diff line change
@@ -66,7 +66,7 @@ function Home() {
66
66
67
67
### Remote Images
68
68
69
-
To use a remote image, the `src` property should be a URL string, which can be [relative](#loaders) or [absolute](#domains). Because Next.js does not have access to remote files during the build process, you'll need to provide the [`width`](/docs/api-reference/next/image.md#width), [`height`](/docs/api-reference/next/image.md#height) and optional [`blurDataURL`](/docs/api-reference/next/image.md#blurdataurl) props manually:
69
+
To use a remote image, the `src` property should be a URL string, which can be [relative](#loaders) or [absolute](/docs/api-reference/next/image.md#domains). Because Next.js does not have access to remote files during the build process, you'll need to provide the [`width`](/docs/api-reference/next/image.md#width), [`height`](/docs/api-reference/next/image.md#height) and optional [`blurDataURL`](/docs/api-reference/next/image.md#blurdataurl) props manually:
70
70
71
71
```jsx
72
72
importImagefrom'next/image'
@@ -93,15 +93,9 @@ export default function Home() {
93
93
94
94
Sometimes you may want to access a remote image, but still use the built-in Next.js Image Optimization API. To do this, leave the `loader` at its default setting and enter an absolute URL for the Image `src`.
95
95
96
-
To protect your application from malicious users, you must define a list of remote domains that you intend to access this way. This is configured in your `next.config.js` file, as shown below:
96
+
To protect your application from malicious users, you must define a list of remote hostnames you intend to allow remote access.
97
97
98
-
```js
99
-
module.exports= {
100
-
images: {
101
-
domains: ['example.com', 'example2.com'],
102
-
},
103
-
}
104
-
```
98
+
> Learn more about [`domains`](/docs/api-reference/next/image.md#domains) configuration.
105
99
106
100
### Loaders
107
101
@@ -207,7 +201,7 @@ For examples of the Image component used with the various fill modes, see the [I
207
201
208
202
## Configuration
209
203
210
-
The `next/image` component and Next.js Image Optimization API can be configured in the [`next.config.js` file](/docs/api-reference/next.config.js/introduction.md). These configurations allow you to [enable remote domains](/docs/api-reference/next/image.md#domains), [define custom image breakpoints](/docs/api-reference/next/image.md#device-sizes), [change caching behavior](/docs/api-reference/next/image.md#caching-behavior) and more.
204
+
The `next/image` component and Next.js Image Optimization API can be configured in the [`next.config.js` file](/docs/api-reference/next.config.js/introduction.md). These configurations allow you to [enable remote images](/docs/api-reference/next/image.md#domains), [define custom image breakpoints](/docs/api-reference/next/image.md#device-sizes), [change caching behavior](/docs/api-reference/next/image.md#caching-behavior) and more.
211
205
212
206
[**Read the full image configuration documentation for more information.**](/docs/api-reference/next/image.md#configuration-options)
`Specified images.remotePatterns should be an Array received ${typeofremotePatterns}.\nSee more info here: https://nextjs.org/docs/messages/invalid-images-config`
243
+
)
244
+
}
245
+
246
+
if(remotePatterns.length>50){
247
+
thrownewError(
248
+
`Specified images.remotePatterns exceeds length of 50, received length (${remotePatterns.length}), please reduce the length of the array to continue.\nSee more info here: https://nextjs.org/docs/messages/invalid-images-config`
)}\n\nremotePatterns value must follow format { protocol: 'https', hostname: 'example.com', port: '', pathname: '/imgs/**' }.\nSee more info here: https://nextjs.org/docs/messages/invalid-images-config`
0 commit comments