Skip to content

Fix remote pattern (#80294) #80428

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

Closed
wants to merge 1 commit into from
Closed

Conversation

Han5991
Copy link
Contributor

@Han5991 Han5991 commented Jun 12, 2025

What?

Fixed matchRemotePattern function to properly handle query strings when using new URL() objects for images.remotePatterns with wildcard patterns.

Why?

When using new URL() objects for images.remotePatterns, URLs containing query strings were incorrectly rejected even when the pattern should have matched them. For example:

// next.config.js
module.exports = {
  images: {
    remotePatterns: [
      new URL('https://avatars.githubusercontent.com/u/**/*'),
    ],
  },
}

This pattern should match https://avatars.githubusercontent.com/u/74867549?v=4, but it was failing because the function didn't distinguish between root wildcards and specific path wildcards when handling empty search parameters.

How?

  1. Refactored search parameter validation logic: Added clear distinction between different types of wildcard patterns
  2. Allow query strings for specific wildcard paths: Patterns like /path/** or /path/**/* now correctly allow URLs with query parameters
  3. Preserve strict matching: Root wildcards (, /) and non-wildcard patterns continue to reject query strings as expected
  4. Improved code maintainability: Added clear variable names and comments

Fixes #80294
Fixes #78076

@ijjk
Copy link
Member

ijjk commented Jun 12, 2025

Allow CI Workflow Run

  • approve CI run for commit: 103665c

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

@Han5991 Han5991 force-pushed the fix-remote-pattern branch from 8076fce to 7e23e77 Compare June 12, 2025 03:16
- Handle empty `search` in remote pattern matching
@Han5991 Han5991 force-pushed the fix-remote-pattern branch from 7e23e77 to 103665c Compare June 12, 2025 21:16
@ijjk ijjk requested a review from styfle June 17, 2025 14:17
Copy link
Member

@styfle styfle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is expected behavior.

We shouldn't match query strings automatically.

You can instead define a pattern with the expected query string like this:

new URL('https://example.com/act123/**?v=4')

@Han5991 Han5991 closed this Jun 17, 2025
@github-actions github-actions bot added the locked label Jul 2, 2025
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 2, 2025
@Han5991 Han5991 deleted the fix-remote-pattern branch July 5, 2025 23:06
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

next/image remotePatterns config search parameters bug Search params in image remote patterns errors un-configured host
3 participants