From 103665c7db8807273a12d462c5d223fa0d812222 Mon Sep 17 00:00:00 2001 From: sangwook Date: Thu, 12 Jun 2025 11:58:11 +0900 Subject: [PATCH] Fix remote pattern (#80294) - Handle empty `search` in remote pattern matching --- packages/next/src/shared/lib/match-remote-pattern.ts | 11 +++++++++-- .../match-remote-pattern-with-url.test.ts | 10 ++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/next/src/shared/lib/match-remote-pattern.ts b/packages/next/src/shared/lib/match-remote-pattern.ts index 657d8224a012c3..595dabc1c12839 100644 --- a/packages/next/src/shared/lib/match-remote-pattern.ts +++ b/packages/next/src/shared/lib/match-remote-pattern.ts @@ -27,10 +27,17 @@ export function matchRemotePattern( } } - if (pattern.search !== undefined) { - if (pattern.search !== url.search) { + const patternSearch = pattern.search ?? '' + const patternPath = pattern.pathname ?? '**' + const isSpecificWildcardPath = + patternPath.includes('**') && patternPath !== '**' && patternPath !== '/**' + + if (patternSearch !== '') { + if (patternSearch !== url.search) { return false } + } else if (url.search && !isSpecificWildcardPath) { + return false } // Should be the same as writeImagesManifest() diff --git a/test/unit/image-optimizer/match-remote-pattern-with-url.test.ts b/test/unit/image-optimizer/match-remote-pattern-with-url.test.ts index d0910990ce033b..b733e06bc3d8e0 100644 --- a/test/unit/image-optimizer/match-remote-pattern-with-url.test.ts +++ b/test/unit/image-optimizer/match-remote-pattern-with-url.test.ts @@ -315,4 +315,14 @@ describe('matchRemotePattern with URL', () => { false ) }) + + it('should match URLs with wildcard patterns', () => { + const p = 'https://example.com/act123/**' + expect(m(new URL(p), new URL('https://example.com/act123/usr4?v=4'))).toBe( + true + ) + expect( + m(new URL(`${p}/*`), new URL('https://example.com/act123/u/74867549?v=4')) + ).toBe(true) + }) })