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 support for images.loaderFile config (vercel#41585)
This PR adds a new configure property, `images.loaderFile` that allow
you to define a path to a file with an exported image loader function.
This is useful when migrating from `next/legacy/image` to `next/image`
because it lets you configure the loader for every instance of
`next/image` once, similar to the legacy "built-in loaders".
Copy file name to clipboardExpand all lines: docs/api-reference/next/image.md
+25
Original file line number
Diff line number
Diff line change
@@ -111,6 +111,8 @@ const MyImage = (props) => {
111
111
}
112
112
```
113
113
114
+
Alternatively, you can use the [loaderFile](#loader-configuration) configuration in next.config.js to configure every instance of `next/image` in your application, without passing a prop.
115
+
114
116
### fill
115
117
116
118
A boolean that causes the image to fill the parent element instead of setting [`width`](#width) and [`height`](#height).
@@ -343,6 +345,29 @@ module.exports = {
343
345
}
344
346
```
345
347
348
+
### Loader Configuration
349
+
350
+
If you want to use a cloud provider to optimize images instead of using the Next.js built-in Image Optimization API, you can configure the `loaderFile` in your `next.config.js` like the following:
351
+
352
+
```js
353
+
module.exports= {
354
+
images: {
355
+
loader:'custom',
356
+
loaderFile:'./my/image/loader.js',
357
+
},
358
+
}
359
+
```
360
+
361
+
This must point to a file relative to the root of your Next.js application. The file must export a default function that returns a string, for example:
Alternatively, you can use the [`loader` prop](#loader) to configure each instance of `next/image`.
370
+
346
371
## Advanced
347
372
348
373
The following configuration is for advanced use cases and is usually not necessary. If you choose to configure the properties below, you will override any changes to the Next.js defaults in future updates.
Copy file name to clipboardExpand all lines: docs/basic-features/image-optimization.md
+4-4
Original file line number
Diff line number
Diff line change
@@ -99,13 +99,13 @@ To protect your application from malicious users, you must define a list of remo
99
99
100
100
### Loaders
101
101
102
-
Note that in the [example earlier](#remote-images), a partial URL (`"/me.png"`) is provided for a remote image. This is possible because of the `next/image`[loader](/docs/api-reference/next/image.md#loader) architecture.
102
+
Note that in the [example earlier](#remote-images), a partial URL (`"/me.png"`) is provided for a remote image. This is possible because of the loader architecture.
103
103
104
104
A loader is a function that generates the URLs for your image. It modifies the provided `src`, and generates multiple URLs to request the image at different sizes. These multiple URLs are used in the automatic [srcset](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/srcset) generation, so that visitors to your site will be served an image that is the right size for their viewport.
105
105
106
-
The default loader for Next.js applications uses the built-in Image Optimization API, which optimizes images from anywhere on the web, and then serves them directly from the Next.js web server. If you would like to serve your images directly from a CDN or image server, you can use one of the [built-in loaders](/docs/api-reference/next/image.md#built-in-loaders) or write your own with a few lines of JavaScript.
106
+
The default loader for Next.js applications uses the built-in Image Optimization API, which optimizes images from anywhere on the web, and then serves them directly from the Next.js web server. If you would like to serve your images directly from a CDN or image server, you can write your own loader function with a few lines of JavaScript.
107
107
108
-
Loaders can be defined per-image, or at the application level.
108
+
You can define a loader per-image with the [`loader` prop](/docs/api-reference/next/image.md#loader), or at the application level with the [`loaderFile` configuration](https://nextjs.org/docs/api-reference/next/image#loader-configuration).
109
109
110
110
### Priority
111
111
@@ -151,7 +151,7 @@ Because `next/image` is designed to guarantee good performance results, it canno
151
151
>
152
152
> If you are accessing images from a source without knowledge of the images' sizes, there are several things you can do:
153
153
>
154
-
> **Use `fill``**
154
+
> **Use `fill`**
155
155
>
156
156
> The [`fill`](/docs/api-reference/next/image#fill) prop allows your image to be sized by its parent element. Consider using CSS to give the image's parent element space on the page along [`sizes`](/docs/api-reference/next/image#sizes) prop to match any media query break points. You can also use [`object-fit`](https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit) with `fill`, `contain`, or `cover`, and [`object-position`](https://developer.mozilla.org/en-US/docs/Web/CSS/object-position) to define how the image should occupy that space.
`Failed to parse src "${src}" on \`next/image\`, if using relative image it must start with a leading slash "/" or be an absolute URL (http:// or https://)`
508
-
)
509
-
}
510
-
511
-
if(process.env.NODE_ENV!=='test'){
512
-
// We use dynamic require because this should only error in development
@@ -379,7 +380,7 @@ function assignDefaults(userConfig: { [key: string]: any }) {
379
380
images.path===imageConfigDefault.path
380
381
){
381
382
thrownewError(
382
-
`Specified images.loader property (${images.loader}) also requires images.path property to be assigned to a URL prefix.\nSee more info here: https://nextjs.org/docs/api-reference/next/image#loader-configuration`
383
+
`Specified images.loader property (${images.loader}) also requires images.path property to be assigned to a URL prefix.\nSee more info here: https://nextjs.org/docs/api-reference/next/legacy/image#loader-configuration`
383
384
)
384
385
}
385
386
@@ -398,6 +399,22 @@ function assignDefaults(userConfig: { [key: string]: any }) {
`Failed to parse src "${src}" on \`next/image\`, if using relative image it must start with a leading slash "/" or be an absolute URL (http:// or https://)`
34
+
)
35
+
}
36
+
37
+
if(process.env.NODE_ENV!=='test'){
38
+
// We use dynamic require because this should only error in development
0 commit comments