100 Commits
Author SHA1 Message Date
StevenandGitHub 7c19c7c06a fix(next/image): improve test coverage for unsupported image types (#96301)
We were missing some test coverage for image optimization. This PR adds
tests for many missing image formats.

Additionally, this PR also ensures the sharp encoders match that
allowlist from detectContentType() such that future versions of sharp
that enable new encoders by default are not automatically enabled in
next/image.

Practically, there is no change to the end user because
detectContentType() runs before sharp so that is the true allowlist, but
this gives us a little more control to align the sharp allowlist as well
(since sharp can release new features in semver minor).
2026-07-28 14:05:06 -04:00
120c804803 chore(deps): bump [email protected] (#95507)
## Notable changes
- No more install script needed 
- No more dynamic require/import tracing workaround
- AVIF uses [tune
iq](https://aomedia.org/blog%20posts/Libavif_v1_4_0-Boasts-Major-Updates-to-Encoder-Technology/#the-tune-iq-image-quality-tuning-mode)
to better match human perception

## All changes
- https://github.com/lovell/sharp/releases/tag/v0.35.0
- https://github.com/lovell/sharp/releases/tag/v0.35.1
- https://github.com/lovell/sharp/releases/tag/v0.35.2
- https://github.com/lovell/sharp/releases/tag/v0.35.3

---------

Co-authored-by: Sebastian Sebbie Silbermann <[email protected]>
2026-07-21 19:52:09 +00:00
StevenandGitHub b229f6400c [cd] reduce size of npm metadata on publish (#94953) 2026-06-19 01:02:53 +02:00
70703e6f9d fix(deps): bump @vercel/[email protected] (#93979)
This fixes several bugs including `module-sync` conditions.


---------

Co-authored-by: Niklas Mischkulnig <[email protected]>
2026-06-05 19:24:43 +02:00
StevenandGitHub ce45a9393d feat(image-optimization): add experimental flag imgOptOperationCache (#94246)
This PR adds a new experimental option to enable/disable operation cache
in sharp.

https://sharp.pixelplumbing.com/api-utility/#cache
2026-05-29 16:10:14 -04:00
StevenandGitHub 4e08dc7bce fix(next/image): ensure images.maximumResponseBody applies to local images too (#92920)
In a previous PR https://github.com/vercel/next.js/pull/88183, we added
`images.maximumResponseBody` but in only applied to external images.

This PR ensures the same config also applies to internal images.
2026-04-16 16:36:50 -07:00
bfb8cc7ab3 chore(docs): mention behavior of redirects and remotePatterns (#91151)
Some users have been confused by the relationship of
`images.remotePatterns` and `images.maximumRedirects` so lets document
it.

This behavior is expected since you might allowlist something like
`https://github.com/styfle.png` and that can redirect to whatever it
needs to redirect to (assuming maximumRedirects > 0). For example, it
currently redirects to
`https://avatars.githubusercontent.com/u/229881?v=4` but you can imagine
in the future if the redirect was changed, you wouldn't want your app to
break.

Regardless of redirects, the final response must be an image.

---------

Co-authored-by: Zack Tanner <[email protected]>
2026-03-10 12:41:49 -04:00
StevenandGitHub 39eb8e0ac4 feat(next/image): add lru disk cache and images.maximumDiskCacheSize (#89963)
This PR adds an LRU disk cache so that reads and writes from the Image
Optimization API will evict old entries based on the value of
`images.maximumDiskCacheSize` configuration.

The LRU ensures that cache reads bump the entry to the top so that they
don't get evicted - only the least recently used entries get evicted.

When `next start` is run, if there is an existing disk cache the we will
replay the files in order to populate the LRU and respect
`images.maximumDiskCacheSize` if it was changed.

If no configuration is provided, default to 50% available disk space.
2026-02-20 20:12:26 +00:00
StevenandGitHub 54476c9c64 fetch(next/image): reduce maximumResponseBody from 300MB to 50MB (#88588)
Based on our metrics, the P99.9 for a source image is 47 MB so we can
adjust the default setting to be much lower (by changing from from 300
MB to 50 MB) in favor of reducing memory.
2026-01-15 14:34:13 -05:00
StevenandGitHub 3b5a210f3d fix(next/image): bump [email protected] (#88238)
Release notes: https://github.com/lovell/sharp/releases/tag/v0.34.5

- Upgrade to libvips v8.17.3 for upstream bug fixes.
- Add experimental support for prebuilt Linux RISC-V 64-bit binaries.
- Support building from source with npm v12+
- Add support for BigTIFF output.
- Improve error messaging when only warnings issued.
- Simplify ICC processing when retaining input profiles.
2026-01-07 17:38:38 +00:00
StevenandGitHub 00e92280d2 feat(next/image)!: add images.maximumResponseBody config (#88183)
Add `images.maximumResponseBody` configuration to exit early when
attempting to optimize large source images.
2026-01-06 14:49:45 -08:00
StevenandGitHub dfc51d0f40 fix(next/image): handle ?dpl for src without protocol (#86836)
Follow up to previous PR based on this comment:
https://github.com/vercel/next.js/pull/86485#discussion_r2589758455

We want to avoid adding `?dpl` when the deployment is an absolute url
missing the protocol which is allowed when `unoptimized=true`.
2025-12-04 15:51:51 -05:00
StevenandGitHub 12a80b453e fix(next/image): add ?dpl query string for local images (without static import) (#86485)
In a previous PR https://github.com/vercel/next.js/pull/73184 the `?dpl`
query string was added to local images from static imports but not to
images without the static import.

This was a subtle change based on malte's PR review comment here:
https://github.com/vercel/next.js/pull/73184#issuecomment-2499126751

However, I can't think of reason why we should differentiate these two
since they are both related to the current deployment (as opposed to
remote images which live outside the deployment).

Furthermore, this string matching is causing microfrontends to fail to
add the `?dpl` because they have a path prefix and thus fail skew
protection.

So this PR fixes both problems by loosening the string matching to look
for src images that start with `/`.
2025-12-04 16:35:02 +00:00
StevenandGitHub 333c30b86b fix(next/image): swap dependencies (#85419)
Swapping `is-local-address` for `ipaddr.js`
2025-10-27 17:08:01 -07:00
StevenandGitHub 8d415853f7 [Breaking] feat(next/image)!: add support for images.dangerouslyAllowLocalIP and images.maximumRedirects (#84676)
This PR adds a two new options and sets a strict default value for each.

- `images.dangerouslyAllowLocalIP`
- `images.maximumRedirects`

### dangerouslyAllowLocalIP

In rare cases when self-hosting Next.js on a private network, you may
want to allow optimizing images from local IP addresses on the same
network.

However, this is not recommended for most users so the default is
`false`.

> [!NOTE]
> BREAKING CHANGE: This change is breaking for those who self-hosting
Next.js on a private network and want to allow optimizing images from
local IP addresses on the same network. In those cases, you can still
enable the config.

### maximumRedirects

Since are also testing redirects for local IPs, we can also reduce the
maximum number of redirects to 3 by default.

Unlike normal websites which might redirect for features like auth, its
unusual to have more than 3 redirects for an image.

In some rare cases, developers may need to increase this value or set to
`0` to disable redirects.

> [!NOTE]
> BREAKING CHANGE: This change is breaking for those who need image
optimization to follow more than 3 redirects.
2025-10-09 22:00:53 +02:00
StevenandGitHub 600238f0b6 breaking(next/image)!: remove 16px from default images.imageSizes config (#84647)
We took a look at unique Next.js projects that have one or more requests
for a specific image size and here are the findings as seen from the
Image Optimization API:

- `w=16` 4.2%
- `w=32` 17.1%
- `w=48` 21.4%
- `w=64` 22.6%
- `w=96` 25.8%
- `w=128` 29.5%
- `w=256` 46.5%
- `w=384` 35.1%
- `w=640` 57.2%

Only 4.2% of Next.js projects ever made a request to a 16px width image.
This is low enough to remove from the default configuration so that all
other projects can benefit from reduced html
[srcset](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/srcset)
and fewer variations exposed from the backend API.

You might think that this means few developers are using `<Image
width={16} />` but it probably means that most displays nowadays use
devicePixelRatio 2 meaning that specifying a 16px image width will
actually fetch the 32px width to ensure it won't looking blurry on your
retina display.

BREAKING CHANGE: this is technically a breaking change but it won't
cause apps to stop working, its just 4% of them may have some requests
serve a 32px image instead of 16px. Those apps can of course opt in by
changing their `images.imageSizes` config back to allowing 16.
2025-10-09 14:31:58 +02:00
StevenandGitHub b41b737b24 feat(next/legacy/image)!: deprecate and warn on next/legacy/image usage (#84622)
`next/legacy/image` was introduced in Next.js 13 as way to allow
developers to upgrade from 12 to 13 with minimal effort.

Now that its been 3 years, its time to deprecate `next/legacy/image` in
Next.js 16 so we can remove it in a future version.

This will print a warning the first time the component is used.
2025-10-08 04:11:56 +00:00
StevenandGitHub 364db96f05 feat(next/image)!: deprecate and warn on images.domains config (#84625)
This was docs deprecated in Next.js 14 so we should begin warning in
Next.js 16 when `images.domains` config is used.

This will guide users to providing a strict `images.remotePatterns`
config ensuring only expected images can be optimized.
2025-10-07 23:21:36 -04:00
75b1ceff2f fix(cache-tags): add /index to implicit tags and adjust revalidatePath() (#84586)
This fixes an issue where the tag was `/` but the build output was
`/index` (and vice-versa) meaning you couldn't use `revalidatePath()` in
some cases because the tag didn't match.

---------

Co-authored-by: JJ Kasper <[email protected]>
2025-10-07 16:03:51 +00:00
StevenandGitHub 0f14e5333f BREAKING CHANGE!: bump default images.minimumCacheTTL from 1 min to 4 hours (#84105)
We have found many Next.js users are confused why images keep
revalidating so frequently (increasing cpu and cost).

The reason is usually that their upstream source images are missing the
`cache-control` header and thus fallback to the 60 second revalidation
default. This its not a great default since most images don't change
frequently.

This PR is a breaking change to bump the `images.minimumCacheTTL` config
from 60 (1 min) to 14400 (4 hours).

Why 4 hours? Because its long enough to take advantage of the durable
cache, but short enough that changing or deleting the upstream src image
will take effect on the same day.

For advanced use cases where images are changing frequently, developers
can change this behavior back by reducing `images.minimumCacheTTL` to a
lower value.
2025-09-23 10:49:34 -04:00
StevenandGitHub 07e9324016 feat(next/image): introduce preload prop and deprecated priority prop (#83351)
The Image component's existing `priority` prop is confusing since its
not obvious what it does.

In order to improve clarity, we are introducing a `preload` prop and
deprecating the `priority` prop.

We also updated the dev warning to no longer suggest using this prop for
the LCP and instead use `loading="eager"`.
2025-09-04 13:50:17 +00:00
StevenandGitHub e33a128da4 BREAKING CHANGE!: next/image only allow quality 75 by default (#83175)
## Background
We have default allowlists for the `url` (localPatterns/remotePatterns
config) and `w` (imageSizes/deviceSizes config) but we don't have a
default allowlist for `q` (currently opt-in with via qualities config).

## What's changing?
BREAKING CHANGE: This PR is a breaking change that sets the default
allowlist to `qualities: [75]`, meaning that anything other than 75 is
invalid. Since most images don't set the quality prop and therefore
default to 75, most apps will be unaffected.

However, we can be even more gracious when users upgrade because we can
coerce the value automatically to 75 (or rather any value in `qualities`
allowlist). In dev, this will print a warning explaining why the prop
was not observed. This also works if you had `qualities` configured but
removed 75, since the closet matching value in the array will be used
instead.
2025-09-02 19:26:14 +02:00
StevenandGitHub a90827bef9 chore(test): update sharp api test (#82740)
Testing to see if sharp works with turbopack
2025-08-18 22:43:55 +02:00
StevenandGitHub 39a771662f feat(next/image): warn when images.localPatterns is undefined and src has query (#82627)
Add a warning to Next.js 15.5 to prepare for Next.js 16 when
`images.localPatterns` is undefined and the user attempted to optimize a
`src` image with a query string, such as

```jsx
<Image src="/api/user?id=1" width="50" height="50" />
```
2025-08-14 16:36:44 +02:00
StevenandGitHub 55f705f1ab fix(next/image): handle empty buffer and experimental flag for skipMetadata (#82569)
As a follow up to PR https://github.com/vercel/next.js/pull/82538 this
PR adds an experimental flag to optionally skip `sharp.metadata()` and
rely only on the JS implementation for format detection.
2025-08-12 08:12:52 -06:00
StevenandGitHub e822c80904 fix: add ?dpl to fonts in /_next/static/media part 2 (#82488)
This is a follow up to PR https://github.com/vercel/next.js/pull/82384
which fixed it for `<link>` but not for `next/font` that embeds local
fonts in css.


https://nextjs.org/docs/14/app/building-your-application/deploying#version-skew
2025-08-11 20:18:08 +00:00
StevenandGitHub ee8286f126 fix(next/image): use image-size pkg as additional format detector (#82538)
In a previous PR https://github.com/vercel/next.js/pull/82118 we added a
fallback to `sharp().metadata()` when the magic number didn't detect the
image format in order to improve detection.

But `sharp` can be slow to read metadata, so we should avoid it if we
can.

So this PR uses the existing `image-size` package to detect the content
type in JS and only then does it fallback to the native `sharp` code if
necessary.

No tests were added since this doesn't change the behavior, only
performance.
2025-08-11 19:54:37 +00:00
StevenandGitHub fc70d77d57 fix: add ?dpl to fonts in /_next/static/media (#82384)
Fonts were not protected from version skew so this PR fixes it


https://nextjs.org/docs/14/app/building-your-application/deploying#version-skew
2025-08-05 23:45:15 +00:00
StevenandGitHub c49b7b11e8 fix(next/image): improve and simplify detect-content-type (#82118)
Add support for detecting more src image formats via magic number. The
src image formats are handled as follows:

- `image/jxl` - serve as is (since safari can render it)
- `image/heic` - serve as is (since safari can render it)
- `image/jp2` - serve as is (since safari can render it)
- `application/pdf` - error (since no browser will render it)
- `image/pic` - error (since no browser will render it)

We also fallback to `sharp().metadata()` if we can't detect the magic
number to ensure correctness.
2025-07-28 16:20:55 -04:00
StevenandGitHub 6b12c60c61 fix(next/image): fix image-optimizer.ts headers (#82114)
The headers were forwarded to the serverless function for "internal"
images but not "external" images.

This changes the behavior to be the same for both such that neither
receive headers.
2025-07-28 13:06:33 +00:00
StevenandGitHub 2b3728f901 feat(next/image): warn when images.qualities is undefined (#81690)
Add a warning to Next.js 15.5 to prepare for Next.js 16 when
`images.qualities` is undefined and the user attempted to use set
`quality` prop to something other than 75.
2025-07-15 19:26:23 -04:00
StevenandGitHub 90c71b4608 chore(bot): update comment (#81672)
The stale bot comment says 2 years even though its 1.5 years (or 545
days to be precise).

The exact amount of time is not as important in this message so we can
remove it to avoid drift between the message and actual stale time.
2025-07-15 14:22:21 +00:00
StevenandGitHub d48daff8c6 fix(next/image): bump sharp to 0.34.3 (#81556)
This fixes a handful of issues including:

- https://github.com/lovell/sharp/issues/4393
- https://github.com/lovell/sharp/issues/4382
- https://github.com/lovell/sharp/issues/4398
- https://github.com/lovell/sharp/issues/4374
- https://github.com/lovell/sharp/issues/4417

See full list of changes
https://github.com/lovell/sharp/compare/v0.34.1...v0.34.3
2025-07-14 19:24:13 +00:00
StevenandGitHub 499eb1a4cd fix: bump [email protected] (#78149)
This PR bumps the `image-size` dependency to version 1.2.1

[Full Changelog:
`v1.1.1...v1.2.1`](https://github.com/image-size/image-size/compare/v1.1.1...v1.2.1)
2025-04-14 12:24:36 -04:00
StevenandGitHub c010ecb237 Revert "docs: revert image 15.3 change until live" (#77977)
Reverts vercel/next.js#77941
2025-04-09 08:32:36 -07:00
StevenandGitHub d6d219d635 fix(next/image): bump [email protected] (#77839)
This PR updates deps to `[email protected].1` to fix few bugs we found:

- https://github.com/lovell/sharp/issues/4277
- https://github.com/lovell/sharp/issues/4328

See the full list of changes here:
https://github.com/lovell/sharp/issues/4248
2025-04-08 00:12:18 +00:00
StevenandGitHub c9e238edf9 feat(next/image): support new URL() for images.remotePatterns (#77692)
Configuring `remotePatterns` can be a hassle because you have to define
each of the parts. And if you forget a part, its a wildcard meaning it
will match anything. This is usually not desirable since most remote
images don't allow query strings.

This PR adds support for using an array of `URL` objects when defining
`images.remotePatterns`.

```js
module.exports = {
  images: {
    remotePatterns: [
      new URL('https://res.cloudinary.com/my-account/**'),
      new URL('https://s3.my-account.*.amazonaws.com/**'),
    ],
  },
}
```

Note that wildcards must be explicit and anything missing is assumed to
no longer match.
2025-04-02 19:13:57 -04:00
StevenandGitHub e26d683b08 chore(docs): update minimumCacheTTL example to 31 days (#76796)
The docs for `minimumCacheTTL` showed the default but didn't show an
example of changing it.

Based on looking at many websites and the frequency those images change,
I think 31 days is a good choice.
2025-03-04 07:34:22 -08:00
StevenandGitHub d70595e5b6 feat(next/image): add support for images.qualities in next.config (#74257)
This PR adds support for `images.qualities` configuration which is an
allowlist of qualities that can be used with the quality prop on the
Image component.

- Depends on https://github.com/vercel/vercel/pull/12792
2025-01-03 20:43:29 +00:00
StevenandGitHub ce881de043 chore(next/image): improve imgopt api bypass detection for unsupported images (#73909)
We currently bypass svg and animated images because vector doesn't need
to be rasterized and animated will turn to still.

However, there are other image types that we implicitly bypass once
sharp throws an error message and we catch it here:


https://github.com/vercel/next.js/blob/7d1ee4ff209d369d8cfce9ec2d00252cc1b8876f/packages/next/src/server/image-optimizer.ts#L798-L815

We already have existing tests for this behavior here:


https://github.com/vercel/next.js/blob/9435b7b9602136aeac77f5ff55bb553dcaa2b01a/test/integration/image-optimizer/test/util.ts#L201

This PR bypasses early so we can avoid sending the buffer, waiting for
it to throw the error, and then catching the error.

Also note this uncovered some discrepancies in how we handle the max age
cache-control so this unifies the behavior.
2024-12-17 08:43:58 -05:00
StevenandGitHub 2a25cbdd99 chore(docs): add missing search: '' on remotePatterns (#73925)
We typically add `search: ''` on `remotePatterns` to disallow the query
string, which is the most common case.

There were a few missing places in the docs so this PR fills in those
gaps.
2024-12-13 17:05:47 -05:00
StevenandGitHub 501c723866 chore(docs): update version history of next/image (#73923)
These features were added in Next.js 15.x but then back-ported to 14.x
since they can impact billing.

So we should land this docs PR and then also back-port it.

I also noticed that the heading was wrong for `localPatterns`.
2024-12-13 16:40:25 -05:00
StevenandGitHub c7d6ab7f4c fix: warnOnce() lru (#73742)
We regressed in https://github.com/vercel/next.js/pull/73483 since the
LRU calculates the value based on the value, not the key (and we were
not setting the value).
2024-12-10 16:22:02 +00:00
StevenandGitHub f8494b48eb chore(docs): mention uses for unoptimized (#73604)
Add docs for `unoptimized` use cases.

- Closes https://github.com/vercel/next.js/issues/72140
2024-12-06 22:13:36 +00:00
StevenandGitHub 0ea736efc0 fix: memory leak from warnOnce() (#73483)
The `warnOnce()` function is used to warn only once time, however this
means we might be storing every possible message in memory forever (aka
memory leak) since it can grow unbounded.

This PR changes the behavior to only store only 10KB in a Least Recently
Used (LRU) cache.

The tradeoff here is that you might see the same warning twice if you
have a lot of unique warnings.
2024-12-04 09:11:36 -05:00
StevenandGitHub 8a4b1d3ce6 chore(image-optimizer): cleanup unused code paths, refactor opts (#73373)
- cleanup unused code paths since `optimizedBuffer` and `upstreamBuffer`
are always defined
- refactor additional options in to `opts` object
- add `opts.silent` option to disable logs

This is easiest to review with whitespace disabled:
https://github.com/vercel/next.js/pull/73373/files?diff=split&w=1
2024-12-02 17:27:18 -05:00
StevenandGitHub 9ea20cb151 fix(next/image): dpl query string should only be used for local images, not remote images (#73184)
Currently, enabling skew protection will cause new deployments to
invalidate the browser cache since the deployment id is including in the
query string as `dpl`. In many cases, such as JS and CSS, these assets
are bundled and include a content hash in the file name, so new
deployments already invalidate browser cache. However, the optimized
image cache is preserved between deploys, so enabling skew protection
breaks that expectation.

This PR removes the `dpl` query string for remote images since those
files don't live in the deployment and therefore don't need skew
protection.

- Related to https://github.com/vercel/next.js/discussions/73015
2024-11-25 20:53:02 -05:00
StevenandGitHub d27182cfb1 fix(next/image): reduce avif sharp effort from 4 to 3 for faster encoding (#73030)
## History
In a previous PR https://github.com/vercel/next.js/pull/65846 the chroma
subsampling setting was removed for AVIF images. This caused the default
of `4:4:4` to be used which increased the image size and CPU a lot.

I considered switching back to `4:2:0` but found that reducing the
`effort: 3` actually yielded significantly better performance with only
a slight difference in size.

## Benchmark

For example, take this PNG that has a width of 1805.

```
curl -JO https://images.bookroo.com/site/landing/homepage/book-collection.png
```

Using `w=1850` and `q=75`:
```
Current: 2_778ms and 153_364 bytes
 Chroma: 2_214ms and 132_927 bytes
This PR: 1_265ms and 158_862 bytes
```

Using `w=1500` and `q=75`:
```
Current: 625ms and 95_810 bytes
 Chroma: 524ms and 89_500 bytes
This PR: 185ms and 99_127 bytes
```

Using `w=1200` and `q=75`:
```
Current: 506ms and 74_738 bytes
 Chroma: 407ms and 68_998 bytes
This PR: 144ms and 77_955 bytes
```

For this particular image, the size will increase a bit but there are
other images I tested where the size was actually smaller. So its a big
improve in CPU usage compared to reverting to the chroma subsampling
setting.

We also are keeping AVIF smaller than WebP which is the expected
behavior.

## Learn More
[Chroma subsampling](https://en.wikipedia.org/wiki/Chroma_subsampling)
is the practice of encoding images by implementing less resolution for
[chroma](https://en.wikipedia.org/wiki/Chrominance)
[information](https://en.wikipedia.org/wiki/Information) than for
[luma](https://en.wikipedia.org/wiki/Luma_(video)) information, taking
advantage of the human visual system's lower acuity for color
differences than for luminance

Image compression deep-dive video:
https://www.youtube.com/watch?v=F1kYBnY6mwg

Sharp Options: https://sharp.pixelplumbing.com/api-output#avif
2024-11-22 14:46:50 +00:00
StevenandGitHub 7e224521b0 fix(next/image): improve svg heuristic for unoptimized (#72970)
The `<Image>` component doesn't optimize SVG images since they are
vector images and can already scale up and down to any width.

Therefore, the component will try to detect SVG images based on the file
name to avoid sending them to the Image Optimization API at all. This is
because the default behavior of the Image Optimization API is to error
for SVG images due to the security implications of `<svg>` with nested
`<script>`, etc (unless `dangerouslyAllowSVG` is enabled.

However, we don't want users to reach for `dangerouslyAllowSVG` because
it is not usually what they intended. So instead, we can improve the
heuristic for detecting SVG in the `<Image>` component by ignoring the
query string.

Related to https://github.com/nodejs/nodejs.org/pull/7244
2024-11-20 14:11:55 -05:00
StevenandGitHub aaf28ab51c chore(example): add example for image optimization with secure compute (#72641)
This PR adds a new example that shows a more complex use case where you
don't want browser to know about the upstream image server so instead
you can proxy through a serverless function.

Some use cases might be checking for an IP address, user agent, etc.
2024-11-13 11:51:01 -05:00
StevenandGitHub 1c219b1660 feat: add experimental imgOptSequentialRead config (#72411)
This setting was originally added in PR
https://github.com/vercel/next.js/pull/44881 and then removed in PR
https://github.com/vercel/next.js/pull/65846 however it seems like it
may still be useful in some cases.

The [sharp docs](https://sharp.pixelplumbing.com/api-constructor) are
not very clear:

> false to use random access, true to use sequential reads. Some
operations will do this automatically.

Let's add it back as an experimental setting so we can measure
performance.
2024-11-06 16:20:08 +00:00
StevenandGitHub 0dce6bb473 feat: add experiment for sharpjs cpu flags (#71733)
This PR adds two experimental flags to configure sharpjs settings that
may improve cpu for image optimization.
2024-10-23 20:30:00 -04:00
StevenandGitHub a8445ed46e chore(docs): fix typo in image.mdx docs (#71647)
There was an extra backtick causing a format error.

https://nextjs.org/docs/app/api-reference/components/image#src

<img width="664" alt="image"
src="https://github.com/user-attachments/assets/2792e71b-f3a7-4790-80a0-eecb5210753a">
2024-10-22 11:34:04 -07:00
StevenandGitHub 7b9630f85f chore(docs): add docs for input image formats with next/image (#71565)
This PR adds missing documentation for the supported src image types.

The input format support is determined my `sharp` and can be found here:
https://sharp.pixelplumbing.com/api-constructor
2024-10-21 12:51:08 -04:00
StevenandGitHub e406def6a6 chore(docs): remove recommendation about using priority over loading=eager with next/image (#71562)
This recommendation to use `priority` instead of `loading=eager` is a
little confusing.

We don't want `priority` to be overused because it can hurt performance
so we can remove this recommendation from the documentation.

TLDR:

- use `priority` for LCP like a hero image above the fold
- use `loading=eager` for other images above the folder
- use `loading=lazy` (default) for everything below the fold
- use `fetchPriority=high` if you want to load an image out of order
that its found in the document
2024-10-21 11:55:07 -04:00
StevenandGitHub eca4c8d397 fix(next/image): improve error message when src={null} (#71398)
There is a React error that suggests the user change `src=""` to
`src={null}`.

> An empty string ("") was passed to the src attribute. This may cause
the browser to download the whole page again over the network. To fix
this, either do not render the element at all or pass null to src
instead of an empty string.

Even though `next/image` treats those both the same way, a user
attempting to use `src={null}` would cause the build to crash with a
confusing error message (JS only problem since TS would catch it much
earlier since src is a required prop).

```
⨯ TypeError: Cannot read properties of null (reading 'default')
    at isStaticRequire (.next/server/chunks/ssr/node_modules__pnpm_1b81c1._.js:142:16)
    at isStaticImport (.next/server/chunks/ssr/node_modules__pnpm_1b81c1._.js:148:40)
    at getImgProps (.next/server/chunks/ssr/node_modules__pnpm_1b81c1._.js:320:9)
    at .next/server/chunks/ssr/node_modules__pnpm_1b81c1._.js:2839:82
digest: "1613180311"
```

This PR ensures that `src={null}` will print the expected error that was
added back in a previous PR:

- https://github.com/vercel/next.js/pull/38847
2024-10-16 15:17:56 -04:00
StevenandGitHub f365109144 fix(next/image): handle undefined images.localPatterns config in images-manifest.json (#70730)
Since `images.localPatterns` config can be undefined, we need to support
that when emitting `images-manifest.json`.

- When `undefined`, all local images are allowed
- When `[]`, no local images are allowed

Note this is different than `remotePatterns` since it treats `undefined`
and `[]` the same (the default being no remote images are allowed).
2024-10-02 22:13:58 +00:00
StevenandGitHub a9c045de13 feat(next/image): add images.localPatterns config (#70529)
This adds support for `images.localPatterns` config to allow specific
local images to be optimized and (more importantly) block anything that
doesn't match a pattern.
2024-10-01 14:45:10 -07:00
StevenandGitHub b3b7709423 feat(next/image): add support for images.remotePatterns.search (#70302)
This PR adds a new feature to the existing `remotePatterns` allowlist
configuration, that enables the ability to filter on `search` (aka query
string).

The most common usage will likely be `search: ''`, which means no query
string allowed.

You can also set the exact query string such as `search: '?v=1'` which
can be useful to invalidate the cache one time for a specific version
without opening the door to any version. Note the leading question mark
to match the behavior of `new URL(url).search`.
2024-09-24 12:12:58 -04:00
StevenandGitHub 0bfa3ec15f feat(next/image): add support for decoding prop (#70298)
- Fixes https://github.com/vercel/next.js/pull/49772
2024-09-20 21:00:01 +00:00
StevenandGitHub 397a3eb475 fix: bump [email protected] (#69524)
In PR https://github.com/vercel/next.js/pull/63321, we bumped
[email protected].3 which dropped support for yarn@1.

However, it looks like [email protected].5 now supports yarn@1 again as seen in
https://github.com/lovell/sharp/issues/4130#issuecomment-2293501130
2024-08-31 20:22:31 -07:00
StevenandGitHub 50b5c7b597 chore(docs): update docs for error message no-img-element (#69312)
The `unoptimized` option was a bit hidden so I added an example code
snippet.

x-ref: https://x.com/KibruKuture/status/1827597148424175678
2024-08-26 15:36:10 -05:00
StevenandGitHub 28fc89448f fix(next/image): handle invalid url (#67465) 2024-07-04 21:55:32 +02:00
StevenandGitHub 73b552a9a5 fix(next/image): undo tight coupling between priority=true and fetchPriority=high (#67351)
## History

When we added `priority` prop to `next/image`, there was no
`fetchPriority` so we instead used this to preload the image in the
head.

Then when browsers added support `fetchPriority`, we automatically added
`fetchPriority=high` when `priority={true}` to signal to the browser
that this was a high priority image. This priority is added to the img
and the preload.

However, we saw cases where images are blocking critical css. Per
@gnoff:

> React currently prioritizes font preloads then high priority images,
then css in the initial page load

Due to these changes in React (aka Float), we should no longer set
`fetchPriority=high`, although the user can still manually add that prop
if needed.
2024-07-01 14:42:42 -04:00
StevenandGitHub 78dc2db916 fix(next/image): improve query string validation for integers (#67293)
The Image Optimization API currently uses `parseInt()` for `w` and `q`
query string parameters.

This doesn't handle floats as you might expect and instead coerces, for
example `parseInt('99.9') === 99`.

This PR changes the runtime to match the build time validation and only
accept integers for `w` and `q`.
2024-07-01 08:10:35 -07:00
StevenandGitHub f5d616b77e chore: add detection for image more types (#67112)
These image types aren't optimized but we can still detect them to
bypass optimization earlier.
2024-06-23 01:04:46 -07:00
StevenandGitHub c3d93b126c chore(next/image): remove unused output parameter (#66083)
This parameter was previously used for a warning message, but we no
longer print that warning since
https://github.com/vercel/next.js/pull/63321 so now we can remove the
unused parameter.
2024-05-22 12:15:30 -05:00
StevenandGitHub 9d70996ee9 chore(docs): remove suggestion of manually installing sharp (#66034)
Users no longer need to install `sharp` manually thanks to the
following:

- https://github.com/vercel/next.js/pull/63321
2024-05-21 15:42:55 +00:00
StevenandGitHub 780e77bef1 fix: bump [email protected] (#65854)
- https://github.com/vercel/nft/issues/415
([e001d05](https://github.com/vercel/nft/commit/e001d0586804b696849c125dbeefe76ef5a71434))
- https://github.com/vercel/nft/issues/417
([ddc5cba](https://github.com/vercel/nft/commit/ddc5cbac2361f0b9c9a90e8f905ee53b9dfa7477))
- https://github.com/vercel/nft/issues/419
([db6c65a](https://github.com/vercel/nft/commit/db6c65ab04d2ead35d1ca65a601b569b10ca2036))
2024-05-16 18:44:16 +00:00
StevenandGitHub 56eb359bea fix(next/image)!: set upstream timeout to 7 seconds (#65821)
This PR sets the upstream image timeout to 7 seconds so its not
unbounded (P99 is about 3 to 4 seconds).

We also set the sharp timeout to 7 seconds (P99 is about 2 seconds
although it depends on CPU).

This means an image could take at most 14 seconds to fetch and optimize.
2024-05-15 18:00:20 -07:00
StevenandGitHub f169b08901 fix(next/image)!: error when src has leading or trailing space (#65637)
BREAKING CHANGE:

Using the built-in image optimization API, the URL is parsed with `new
URL()` constructor which automatically trims spaces.

However, the developer may choose a 3rd party image optimization API via
`loader` or `loaderFile` (or perhaps a deployment platform that has its
own built in loader), so we shouldn't assume the API will parse the URL
in the same way as
[WHATWG](https://url.spec.whatwg.org/#:~:text=If%20input%20contains%20any%20leading%20or%20trailing%20C0%20control%20or%20space%2C%20invalid%2DURL%2Dunit%20validation%20error.).

While we could trim on the client, its probably best to fail fast and
let the developer make a conscience decision if a trailing space should
be removed or remain (by explicitly using `%20`).
2024-05-14 18:00:27 +00:00
StevenandGitHub 292fd4eb3f feat!(next/image): change default Content-Disposition to attachment (#65631)
### BREAKING CHANGE

This changes the behavior of the default image `loader` so that
[`Content-Disposition`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#as_a_response_header_for_the_main_body)
header is now `attachment` for added protection since the API can serve
arbitrary remote images.

The new default value, `attachment`, forces the browser to download the
image when visiting directly. This is particularly important when
`dangerouslyAllowSVG` is true. Most users will not notice the change
since visiting pages won't behave any differently, only visiting images
directly.

Users can switch back to the old behavior by configuring `inline` in
next.config.js

```js
module.exports = {
  images: {
    contentDispositionType: 'inline',
  },
}
2024-05-10 21:35:48 -04:00
StevenandGitHub 01db9854d0 chore(test): test image preload below the fold (#65528)
Follow up to https://github.com/vercel/next.js/pull/65058 to ensure
preloads work below the fold.

Closes NEXT-3364
2024-05-08 21:10:59 -04:00
StevenandGitHub 78507bcf69 fix!: add sharp timeout of 10 seconds (#65517)
This PR [configures
sharp](https://sharp.pixelplumbing.com/api-output#timeout) with a
timeout of 10 seconds when optimizing an image.

Anything longer will throw an error which we will catch and [fallback to
serving the unoptimized source
image](https://github.com/vercel/next.js/blob/c942006d83d504822ff2f6822fa2608e8e773324/packages/next/src/server/image-optimizer.ts#L637).



Closes NEXT-3359
2024-05-08 10:36:11 -07:00
StevenandGitHub c4d812cce3 fix: lazy load sharp (#65484)
> [!NOTE]  
> This PR is easiest to review without whitespace.
> See https://github.com/vercel/next.js/pull/65484/files?w=1

This PR fixes an issue where importing `image-optimization` was loading
sharp but we don't want to load it until we attempt to optimize an
image. So this PR changes it to lazy load on first invocation to avoid
metadata trying to initialize sharp.


![image](https://github.com/vercel/next.js/assets/229881/61f45f3c-922a-45b6-8ba6-b32764fd0257)

Closes NEXT-3355
2024-05-07 23:08:23 +00:00
StevenandGitHub 1f598bc313 fix(next/image): set max url length to 3072 (#65457)
This PR sets the maximum `url` parameter length to 3072 for image
optimization.

Closes NEXT-3348
2024-05-07 16:15:33 +00:00
StevenandGitHub 3dc2e672f1 fix(next/image): add missing svg test refactor missing types (#65345)
The tests added in https://github.com/vercel/next.js/pull/46219 were
never correctly testing the headers because `detectContentType()` is
called first and only when we can't detect the type from the response
body do we fallback to the `Content-Type` header.

I also refactored some of the tests because the `ctx: any` type was
causing some tests to not run when testing different configuration
options.

Closes NEXT-3321
2024-05-06 22:32:26 +00:00
StevenandGitHub 44a21f4cc2 fix(next/image): bypass icns images (#65414)
Closes NEXT-3329
2024-05-06 19:01:14 +00:00
StevenandGitHub cf0cec1b06 chore(test): add more tests for image optimizer formats (#65363)
There were a few cases that were untested so I added new tests to cover
them.

Closes NEXT-3322
2024-05-06 10:23:44 -04:00
StevenandGitHub 68d5a3892b fix(next/image): detect react@19 for fetchPriority prop (#65235)
In a previous PR, https://github.com/vercel/next.js/pull/47302,
detection for `fetchPriority` assumed that
https://github.com/facebook/react/pull/25927 would land in [email protected]
because that was the react@canary version at the time. However, it
didn't land in [email protected] and it is expected to land in [email protected]
due to the breaking change.

This means that users upgrading to [email protected] will see a warning.

The fix is to stop looking at the `React.version` string and instead
check for `React.use`, a feature that [will land in
[email protected]](https://react.dev/blog/2024/04/25/react-19#new-feature-use)
but is also available in react@canary and react@beta today.

Note: There were tests added for App Router and Pages Router in a
previous PR https://github.com/vercel/next.js/pull/47302 but they seem
to run on [email protected] which is why we don't see failures.

Fixes https://github.com/vercel/next.js/issues/65161
2024-05-01 15:17:54 -04:00
StevenandGitHub a6a6117197 feat(next/image)!: remove squoosh in favor of sharp as optional dependency (#63321)
## History

Previously, we added support for `squoosh` because it was a wasm
implementation that "just worked" on all platforms when running `next
dev` for the first time. However, it was slow so we always recommended
manually installing `sharp` for production use cases running `next
build` and `next start`.

Now that [`sharp` supports
webassembly](https://sharp.pixelplumbing.com/install#webassembly), we no
longer need to maintain `squoosh`, so it can be removed. We also don't
need to make the user install sharp manually because it can be installed
under `optionalDependencies`. I left it optional in case there was some
platform that still needed to manually install the wasm variant with
`npm install --cpu=wasm32 sharp` such as codesandbox/stackblitz (I don't
believe sharp has any fallback built in yet).

Since we can guarantee `sharp`, we can also remove `get-orientation` dep
and upgrade `image-size` dep.

I also moved an [existing `sharp`
test](https://github.com/vercel/next.js/pull/56674) into its own fixture
since it was unrelated to image optimization.

## Related Issues
- Fixes https://github.com/vercel/next.js/issues/41417
- Related https://github.com/vercel/next.js/pull/54670
- Related https://github.com/vercel/next.js/issues/54708
- Related https://github.com/vercel/next.js/issues/44804
- Related https://github.com/vercel/next.js/issues/48820
- Related https://github.com/vercel/next.js/pull/61810
- Related https://github.com/vercel/next.js/pull/61696
- Related https://github.com/vercel/next.js/issues/44685
- Closes https://github.com/vercel/next.js/issues/64362

## Breaking Change

This is a breaking change because newer versions of `sharp` no longer
support `yarn@1`.

- https://github.com/lovell/sharp/issues/3750

The workaround is to install with `yarn --ignore-engines` flag.

Also note that Vercel no longer defaults to yarn when no lockfile is
found

- https://github.com/vercel/vercel/pull/11131
- https://github.com/vercel/vercel/pull/11242

Closes NEXT-2823
2024-04-25 14:01:56 -04:00
StevenandGitHub 8c9c1d2b6d feat(next/image): add overrideSrc prop (#64221)
- Fixes https://github.com/vercel/next.js/discussions/60888
- Related
https://merj.com/blog/optimising-nextjs-image-component-for-image-search
2024-04-09 10:48:01 -04:00
StevenandGitHub abe74a5211 fix: call instrumentationHook earlier for prod server (#63536)
This ensures that the instrumentation hook for Node.js will run
immediately during `next start` instead of waiting for the first request
like does it `next dev`.

However, if there is a separate instrumentation hook for Edge, that will
still be lazy evaluated and wait until the first request.

Fixes #59999 
Fixes NEXT-2738
2024-03-21 13:11:03 +00:00
StevenandGitHub d8a58b5ac5 chore(test): fix typo occured => occurred (#63287)
Closes https://github.com/vercel/next.js/pull/63276

Closes NEXT-2818
2024-03-14 15:57:31 +00:00
StevenandGitHub 02a06175e3 feat: add deploymentId config (#63198)
This PR stabilizes an experimental feature that was added in a previous
PR https://github.com/vercel/next.js/pull/50470

It allows the user to set `deploymentId` in `next.config.js`, which is a
unique identifier for a deployment that will be included in each
request's query string or header.

This PR is easier to review with whitespace hidden:
https://github.com/vercel/next.js/pull/63198/files?w=1

Closes NEXT-2789
2024-03-13 16:46:50 -04:00
StevenandGitHub 560a584273 chore(test): add test for next/image in node_modules with app router (#63139)
This adds a test from PR https://github.com/vercel/next.js/pull/33559
that was testing Pages Router but makes sure the same test runs for App
Router.

Closes NEXT-2774
2024-03-11 16:38:57 +01:00
StevenandGitHub e61497f18d fix: add zeromq to server-external-packages.json (#62105)
The package [`zeromq`](https://unpkg.com/browse/[email protected]/)
contains native `.node` files that cannot be bundled (similar to
https://github.com/vercel/next.js/pull/60243)

- Fixes https://github.com/vercel/next.js/issues/61844

Closes NEXT-2507
2024-02-15 17:00:23 +00:00
StevenandGitHub 17fd7a5ce6 fix: bump @vercel/[email protected] (#62019)
- Release https://github.com/vercel/nft/releases/tag/0.26.4
- Fixes https://github.com/vercel/next.js/issues/61844

Fixes NEXT-2485
2024-02-13 18:53:01 -05:00
StevenandGitHub 9cf2a3273d fix(next/image): improve warning when fill and sizes="100vw" (#61949)
Previously, this error was confusing because it made it sound like the
`sizes` prop was missing. This was because the default value of `sizes`
is `100vw` so the previous code couldn't tell the different between
implicit vs explicit `100vw`.

This PR changes the code to read the input value from the `sizes` prop
and prints a better warning.


Fixes NEXT-2441
2024-02-12 17:32:39 +00:00
StevenandGitHub d894303a02 chore(test): switch a few tests from yarn to pnpm (#61793)
Since yarn@1 is no longer maintained, we shouldn't use it for tests that
are not designed specifically for yarn@1

Closes NEXT-2397
2024-02-09 15:11:52 +01:00
698fcbb8d1 fix: babel usage with next/image (#61835)
### Why

The `process/browser` lib was imported and being transformed, but
`process.browser = true` turns to `true = true` that causes the
compilation error, we need to exclude them from certain compilation
layers

### What

- Fixes https://github.com/vercel/next.js/issues/61116

Closes NEXT-2410
Closes NEXT-2328

---------

Co-authored-by: Jiachi Liu <[email protected]>
2024-02-08 18:16:30 -05:00
StevenandGitHub 07c4ec052e feat(next/image)!: remove squoosh in favor of sharp as optional dep (#61696)
## History

Previously, we added support for `squoosh` because it was a wasm
implementation that "just worked" on all platforms when running `next
dev` for the first time. However, it was slow so we always recommended
manually installing `sharp` for production use cases running `next
build` and `next start`.

Now that [`sharp` supports
webassembly](https://sharp.pixelplumbing.com/install#webassembly), we no
longer need to maintain `squoosh`, so it can be removed. We also don't
need to make the user install sharp manually because it can be installed
under `optionalDependencies`. I left it optional in case there was some
platform that still needed to manually install the wasm variant with
`npm install --cpu=wasm32 sharp` such as codesandbox/stackblitz (I don't
believe sharp has any fallback built in yet).

Since we can guarantee `sharp`, we can also remove `get-orientation` dep
and upgrade `image-size` dep.

I also moved an [existing `sharp`
test](https://github.com/vercel/next.js/pull/56674) into its own fixture
since it was unrelated to image optimization.

## Related Issues
- Fixes https://github.com/vercel/next.js/issues/41417
- Closes https://github.com/vercel/next.js/pull/54670
- Related https://github.com/vercel/next.js/issues/54708
- Related https://github.com/vercel/next.js/issues/44804
- Related https://github.com/vercel/next.js/issues/48820
2024-02-06 14:17:07 -05:00
StevenandGitHub 804cb937fe chore(docs): add // @ts-check to next.config.js docs (#61543)
This PR updates the docs to add `// @ts-check` at the top of each
`next.config.js` or `next.config.mjs`.

Even though you can't use `next.config.ts`, you can still get some type
checking to catch typos such as wrong type or even duplicate keys.


<img width="322" alt="image"
src="https://github.com/vercel/next.js/assets/229881/fcaba393-3560-4d16-95bf-a0504e66fa26">

<img width="325" alt="image"
src="https://github.com/vercel/next.js/assets/229881/e7156dd4-7b79-4f08-b843-4431b56b1c50">



Closes NEXT-2335
2024-02-02 00:36:00 +01:00
StevenandGitHub 5ee6a07d74 fix: bump @vercel/[email protected] (#61538)
- Release notes https://github.com/vercel/nft/releases/tag/0.26.3
- Fixes https://github.com/vercel/next.js/issues/59346
- Related to https://github.com/lovell/sharp/issues/3967

Closes NEXT-2334
2024-02-01 16:46:38 -05:00
StevenandGitHub 9d949fd428 chore(deps): bump swr to 2.2.4 (#61480)
- Closes https://github.com/vercel/next.js/pull/61478

Closes NEXT-2319
2024-02-01 00:29:04 +00:00
7f4ca97ef8 chore(docs): fix docs for static assets (public) (#61225)
Closes NEXT-2260

---------

Co-authored-by: Lee Robinson <[email protected]>
2024-01-26 15:19:54 -05:00
StevenandGitHub 39afdc5f97 chore: refactor image optimization to separate external/internal urls (#61172)
This PR is refactoring only and doesn't change the logic, therefore no
tests were added.

The improvement here is readability:
- separate functions to fetch external vs internal image (in the future,
we could improve perf of internal image routing)
- types of the arguments are more specific (rather than passing in
complete NextConfig)

> Note: Its easier to review with whitespace hidden:
https://github.com/vercel/next.js/pull/61172/files?w=1
2024-01-26 08:20:20 -08:00
StevenandGitHub 6c611fe9a8 fix(image): warn when animated image is missing unoptimized prop (#61045)
This PR adds a warning when attempting to optimize an animated image.

```jsx
<Image src="/image.gif" />
```

The warning looks like the following:

```
The requested resource "/image.gif" is an animated image so it will not be optimized. Consider adding the "unoptimized" property to the <Image>.
```

To remove the warning, add the `unoptimized` prop.

```jsx
<Image src="/image.gif" unoptimized />
```

We don't attempt to optimized animated images because it can be very
slow (30+ seconds) and sometimes deoptimizeds (the output is larger than
the input) so its best to serve the animated image as-is.

Closes NEXT-2199
2024-01-23 17:31:33 -05:00
StevenandGitHub 2227ae5f34 Revert "Fix: Throw an error for empty array return in generateStaticParams with output:export" (#60831)
Reverts vercel/next.js#57053 per this comment:
https://github.com/vercel/next.js/pull/57053#issuecomment-1892747474

Instead of erroring, we should warn (and only in dev mode). That can be
added in a future PR.

Closes NEXT-2155
2024-01-18 18:02:07 +01:00
StevenandGitHub cb71d80879 chore(deps): update browserslist and caniuse-lite (#60827)
Closes #57872
Closes NEXT-2152
2024-01-18 16:14:38 +00:00