-
Notifications
You must be signed in to change notification settings - Fork 27.6k
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
Fix leaking internal config to user-defined loader
prop in next/image
#36013
Conversation
loader
prop in `next/im…loader
prop in next/image
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Failing test suitesCommit: 998b3af
Expand output● Image Component Tests › SSR Image Component Tests › should automatically load images if observer does not exist
Read more about building and testing Next.js in contributing.md. |
This comment has been minimized.
This comment has been minimized.
Stats from current PRDefault Build (Increase detected
|
vercel/next.js canary | vercel/next.js fix-custom-loader-prop-remove-config | Change | |
---|---|---|---|
buildDuration | 15.2s | 15.1s | -83ms |
buildDurationCached | 6s | 6s | |
nodeModulesSize | 478 MB | 478 MB |
Page Load Tests Overall increase ✓
vercel/next.js canary | vercel/next.js fix-custom-loader-prop-remove-config | Change | |
---|---|---|---|
/ failed reqs | 0 | 0 | ✓ |
/ total time (seconds) | 3.035 | 3.053 | |
/ avg req/sec | 823.74 | 818.91 | |
/error-in-render failed reqs | 0 | 0 | ✓ |
/error-in-render total time (seconds) | 1.155 | 1.149 | -0.01 |
/error-in-render avg req/sec | 2165.19 | 2175.89 | +10.7 |
Client Bundles (main, webpack)
vercel/next.js canary | vercel/next.js fix-custom-loader-prop-remove-config | Change | |
---|---|---|---|
925.HASH.js gzip | 179 B | 179 B | ✓ |
framework-HASH.js gzip | 42 kB | 42 kB | ✓ |
main-HASH.js gzip | 28 kB | 28 kB | ✓ |
webpack-HASH.js gzip | 1.44 kB | 1.44 kB | ✓ |
Overall change | 71.7 kB | 71.7 kB | ✓ |
Legacy Client Bundles (polyfills)
vercel/next.js canary | vercel/next.js fix-custom-loader-prop-remove-config | Change | |
---|---|---|---|
polyfills-HASH.js gzip | 31 kB | 31 kB | ✓ |
Overall change | 31 kB | 31 kB | ✓ |
Client Pages Overall increase ⚠️
vercel/next.js canary | vercel/next.js fix-custom-loader-prop-remove-config | Change | |
---|---|---|---|
_app-HASH.js gzip | 1.36 kB | 1.36 kB | ✓ |
_error-HASH.js gzip | 192 B | 192 B | ✓ |
amp-HASH.js gzip | 309 B | 309 B | ✓ |
css-HASH.js gzip | 327 B | 327 B | ✓ |
dynamic-HASH.js gzip | 3.05 kB | 3.05 kB | ✓ |
head-HASH.js gzip | 351 B | 351 B | ✓ |
hooks-HASH.js gzip | 920 B | 920 B | ✓ |
image-HASH.js gzip | 5.64 kB | 5.68 kB | |
index-HASH.js gzip | 263 B | 263 B | ✓ |
link-HASH.js gzip | 2.32 kB | 2.32 kB | ✓ |
routerDirect..HASH.js gzip | 320 B | 320 B | ✓ |
script-HASH.js gzip | 387 B | 387 B | ✓ |
withRouter-HASH.js gzip | 319 B | 319 B | ✓ |
85e02e95b279..7e3.css gzip | 107 B | 107 B | ✓ |
Overall change | 15.9 kB | 15.9 kB |
Client Build Manifests Overall increase ⚠️
vercel/next.js canary | vercel/next.js fix-custom-loader-prop-remove-config | Change | |
---|---|---|---|
_buildManifest.js gzip | 459 B | 460 B | |
Overall change | 459 B | 460 B |
Rendered Page Sizes
vercel/next.js canary | vercel/next.js fix-custom-loader-prop-remove-config | Change | |
---|---|---|---|
index.html gzip | 531 B | 531 B | ✓ |
link.html gzip | 544 B | 544 B | ✓ |
withRouter.html gzip | 524 B | 524 B | ✓ |
Overall change | 1.6 kB | 1.6 kB | ✓ |
Diffs
Diff for _buildManifest.js
@@ -12,7 +12,7 @@ self.__BUILD_MANIFEST = {
],
"/head": ["static\u002Fchunks\u002Fpages\u002Fhead-96a5d6ed07cf5a83.js"],
"/hooks": ["static\u002Fchunks\u002Fpages\u002Fhooks-9dfe734f583d4926.js"],
- "/image": ["static\u002Fchunks\u002Fpages\u002Fimage-f39504b2095f9b7d.js"],
+ "/image": ["static\u002Fchunks\u002Fpages\u002Fimage-0e5a6c840e535a1d.js"],
"/link": ["static\u002Fchunks\u002Fpages\u002Flink-a72b8ab130103a8f.js"],
"/routerDirect": [
"static\u002Fchunks\u002Fpages\u002FrouterDirect-cb15c8ac2322bf32.js"
Diff for image-HASH.js
@@ -155,8 +155,6 @@
objectPosition = _param.objectPosition,
onLoadingComplete = _param.onLoadingComplete,
onError = _param.onError,
- _loader = _param.loader,
- loader = _loader === void 0 ? defaultImageLoader : _loader,
_placeholder = _param.placeholder,
placeholder = _placeholder === void 0 ? "empty" : _placeholder,
blurDataURL = _param.blurDataURL,
@@ -177,7 +175,6 @@
"objectPosition",
"onLoadingComplete",
"onError",
- "loader",
"placeholder",
"blurDataURL"
]);
@@ -208,8 +205,26 @@
if ("layout" in rest) {
// Override default layout if the user specified one:
if (rest.layout) layout = rest.layout;
- // Remove property so it's not spread into image:
- delete rest["layout"];
+ // Remove property so it's not spread on <img>:
+ delete rest.layout;
+ }
+ var loader = defaultImageLoader;
+ if ("loader" in rest) {
+ if (rest.loader) {
+ var customImageLoader = rest.loader;
+ var _tmp;
+ (_tmp = function(obj) {
+ var _ = obj.config,
+ opts = _objectWithoutProperties(obj, ["config"]);
+ // The config object is internal only so we must
+ // not pass it to the user-defined loader()
+ return customImageLoader(opts);
+ }),
+ (loader = _tmp),
+ _tmp;
+ }
+ // Remove property so it's not spread on <img>
+ delete rest.loader;
}
var staticSrc = "";
if (isStaticImport(src)) {
Default Build with SWC (Increase detected ⚠️ )
General Overall increase ⚠️
vercel/next.js canary | vercel/next.js fix-custom-loader-prop-remove-config | Change | |
---|---|---|---|
buildDuration | 17.9s | 17.9s | |
buildDurationCached | 6s | 6s | |
nodeModulesSize | 478 MB | 478 MB |
Page Load Tests Overall decrease ⚠️
vercel/next.js canary | vercel/next.js fix-custom-loader-prop-remove-config | Change | |
---|---|---|---|
/ failed reqs | 0 | 0 | ✓ |
/ total time (seconds) | 3.024 | 3.035 | |
/ avg req/sec | 826.73 | 823.83 | |
/error-in-render failed reqs | 0 | 0 | ✓ |
/error-in-render total time (seconds) | 1.148 | 1.168 | |
/error-in-render avg req/sec | 2178.25 | 2140.71 |
Client Bundles (main, webpack)
vercel/next.js canary | vercel/next.js fix-custom-loader-prop-remove-config | Change | |
---|---|---|---|
925.HASH.js gzip | 178 B | 178 B | ✓ |
framework-HASH.js gzip | 42.3 kB | 42.3 kB | ✓ |
main-HASH.js gzip | 28.3 kB | 28.3 kB | ✓ |
webpack-HASH.js gzip | 1.45 kB | 1.45 kB | ✓ |
Overall change | 72.2 kB | 72.2 kB | ✓ |
Legacy Client Bundles (polyfills)
vercel/next.js canary | vercel/next.js fix-custom-loader-prop-remove-config | Change | |
---|---|---|---|
polyfills-HASH.js gzip | 31 kB | 31 kB | ✓ |
Overall change | 31 kB | 31 kB | ✓ |
Client Pages Overall increase ⚠️
vercel/next.js canary | vercel/next.js fix-custom-loader-prop-remove-config | Change | |
---|---|---|---|
_app-HASH.js gzip | 1.35 kB | 1.35 kB | ✓ |
_error-HASH.js gzip | 179 B | 179 B | ✓ |
amp-HASH.js gzip | 313 B | 313 B | ✓ |
css-HASH.js gzip | 325 B | 325 B | ✓ |
dynamic-HASH.js gzip | 3.03 kB | 3.03 kB | ✓ |
head-HASH.js gzip | 351 B | 351 B | ✓ |
hooks-HASH.js gzip | 921 B | 921 B | ✓ |
image-HASH.js gzip | 5.7 kB | 5.74 kB | |
index-HASH.js gzip | 261 B | 261 B | ✓ |
link-HASH.js gzip | 2.38 kB | 2.38 kB | ✓ |
routerDirect..HASH.js gzip | 322 B | 322 B | ✓ |
script-HASH.js gzip | 388 B | 388 B | ✓ |
withRouter-HASH.js gzip | 317 B | 317 B | ✓ |
85e02e95b279..7e3.css gzip | 107 B | 107 B | ✓ |
Overall change | 15.9 kB | 16 kB |
Client Build Manifests
vercel/next.js canary | vercel/next.js fix-custom-loader-prop-remove-config | Change | |
---|---|---|---|
_buildManifest.js gzip | 459 B | 459 B | ✓ |
Overall change | 459 B | 459 B | ✓ |
Rendered Page Sizes
vercel/next.js canary | vercel/next.js fix-custom-loader-prop-remove-config | Change | |
---|---|---|---|
index.html gzip | 531 B | 531 B | ✓ |
link.html gzip | 544 B | 544 B | ✓ |
withRouter.html gzip | 527 B | 527 B | ✓ |
Overall change | 1.6 kB | 1.6 kB | ✓ |
Diffs
Diff for _buildManifest.js
@@ -12,7 +12,7 @@ self.__BUILD_MANIFEST = {
],
"/head": ["static\u002Fchunks\u002Fpages\u002Fhead-96a5d6ed07cf5a83.js"],
"/hooks": ["static\u002Fchunks\u002Fpages\u002Fhooks-9dfe734f583d4926.js"],
- "/image": ["static\u002Fchunks\u002Fpages\u002Fimage-f39504b2095f9b7d.js"],
+ "/image": ["static\u002Fchunks\u002Fpages\u002Fimage-0e5a6c840e535a1d.js"],
"/link": ["static\u002Fchunks\u002Fpages\u002Flink-a72b8ab130103a8f.js"],
"/routerDirect": [
"static\u002Fchunks\u002Fpages\u002FrouterDirect-cb15c8ac2322bf32.js"
Diff for image-HASH.js
@@ -155,8 +155,6 @@
objectPosition = _param.objectPosition,
onLoadingComplete = _param.onLoadingComplete,
onError = _param.onError,
- _loader = _param.loader,
- loader = _loader === void 0 ? defaultImageLoader : _loader,
_placeholder = _param.placeholder,
placeholder = _placeholder === void 0 ? "empty" : _placeholder,
blurDataURL = _param.blurDataURL,
@@ -177,7 +175,6 @@
"objectPosition",
"onLoadingComplete",
"onError",
- "loader",
"placeholder",
"blurDataURL"
]);
@@ -208,8 +205,26 @@
if ("layout" in rest) {
// Override default layout if the user specified one:
if (rest.layout) layout = rest.layout;
- // Remove property so it's not spread into image:
- delete rest["layout"];
+ // Remove property so it's not spread on <img>:
+ delete rest.layout;
+ }
+ var loader = defaultImageLoader;
+ if ("loader" in rest) {
+ if (rest.loader) {
+ var customImageLoader = rest.loader;
+ var _tmp;
+ (_tmp = function(obj) {
+ var _ = obj.config,
+ opts = _objectWithoutProperties(obj, ["config"]);
+ // The config object is internal only so we must
+ // not pass it to the user-defined loader()
+ return customImageLoader(opts);
+ }),
+ (loader = _tmp),
+ _tmp;
+ }
+ // Remove property so it's not spread on <img>
+ delete rest.loader;
}
var staticSrc = "";
if (isStaticImport(src)) {
…age` (vercel#36013) The `config` was changed from a global variable to a function parameter of the `loader()` function in PR vercel#33559 as an implementation detail, not as a public API. It was not meant to be used by the end user which is why it was [undocumented](https://nextjs.org/docs/api-reference/next/image#loader). This config is meant for the default Image Optimization API. Since the `loader` prop bypasses the default Image Optimization API in favor of a custom function, that config is no longer be relevant because the function can implement the optimization url however it desires. - Fixes vercel#35115
This decision costed me 1 hour of refactoring... The values in NextJs image solution consist of 2 distinct parts:
There are tons of problems with the While a lot of improvements have been done with the By making It's like NextJs never anticipated that we could use NextJs image optimization service, the In any case, I just wanted to point out these kind of decisions are not improving flexibility, it just makes it frustrating to work with NextJs Image solution. |
next/image desperately lacks out-of-the-box support for modern CSS frameworks like TailwindCSS. We need to use workarounds and define static widths and heights. I feel that instead of assisting the developer, it's just a chore to work with it most of the time. |
Thanks for the feedback! Please see my comment #35115 (comment) as well as #35115 (comment). |
The
config
was changed from a global variable to a function parameter of theloader()
function in PR #33559 as an implementation detail, not as a public API. It was not meant to be used by the end user which is why it was undocumented.This config is meant for the default Image Optimization API. Since the
loader
prop bypasses the default Image Optimization API in favor of a custom function, that config is no longer be relevant because the function can implement the optimization url however it desires.config
arg should not be passed to user-definedloader
function in `next/image #35115