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) + }) })