fix: detect next/image usage in client components#10228
Merged
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request improves the detection of next/image usage within Next.js app directories by introducing a fallback mechanism that scans prerendered HTML files for the data-nimg attribute, covering 'use client' components. The existing manifest-based check was refactored into a dedicated function for server components. Feedback suggests optimizing the combined check by executing the cheaper manifest scan first and only proceeding to the more resource-intensive HTML globbing if needed, enabling an early exit.
annajowang
reviewed
Apr 8, 2026
annajowang
approved these changes
Apr 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Thanks to @dxdc for reporting this in #10214.
When every file that imports
next/imageis a"use client"component,isUsingImageOptimization()fails to detect image usage because:export-marker.json):isNextImageImportedis only set for Pages Router pages — alwaysfalsein App Router-only projects.client-reference-manifest): The Image component never crosses a server→client RSC boundary, so it's never registered in the manifest.This adds a fallback that scans prerendered HTML files under
.next/server/app/for thedata-nimg="attribute thatnext/imagerenders on every<imgit produces. This attribute has been stable since Next.js 11.1, covering all supported versions (12–16). The check only runs when both existing tiers fail, so it's fully backward compatible.Also refactors
isUsingNextImageInAppDirectoryinto two explicit sub-checks:isUsingNextImageInServerComponent— scansclient-reference-manifest.js(existing logic)isUsingNextImageInClientComponent— scans prerendered HTML fordata-nimg="(new)Scenarios Tested
next/imageis used in Server Components (Tier 2 detects it)next/imageimports are in"use client"components (new Tier 3 detects it viadata-nimg=")images.unoptimized: true— correctly returnsfalseeven whendata-nimg="is presentnext/imageusage — correctly returnsfalseexport-marker.json) still worksVerified against a Next.js 15.5.14 test app with
"use client"on all image-importing components.Sample Commands