diff --git a/packages/next-routing/src/__tests__/conditions.test.ts b/packages/next-routing/src/__tests__/conditions.test.ts index 9b4811715a68..f608aac274f0 100644 --- a/packages/next-routing/src/__tests__/conditions.test.ts +++ b/packages/next-routing/src/__tests__/conditions.test.ts @@ -69,6 +69,43 @@ describe('Has conditions', () => { expect(result.resolvedPathname).toBe('/admin-dashboard') }) + it('should NOT match substring values in has conditions', async () => { + const headers = new Headers({ + 'x-role': 'not-admin', + }) + + const params = createBaseParams({ + url: new URL('https://example.com/dashboard'), + headers, + routes: { + beforeMiddleware: [], + beforeFiles: [ + { + sourceRegex: '^/dashboard$', + destination: '/admin', + has: [ + { + type: 'header', + key: 'x-role', + value: 'admin', + }, + ], + }, + ], + afterFiles: [], + dynamicRoutes: [], + onMatch: [], + fallback: [], + }, + pathnames: ['/dashboard', '/admin'], + }) + + const result = await resolveRoutes(params) + + expect(result.resolvedPathname).toBe('/dashboard') + expect(result.invocationTarget?.pathname).toBe('/dashboard') + }) + it('should match route with cookie condition', async () => { const headers = new Headers({ cookie: 'session=abc123; theme=dark', diff --git a/packages/next-routing/src/matchers.ts b/packages/next-routing/src/matchers.ts index 6911e56d62b2..e9afddd48a7b 100644 --- a/packages/next-routing/src/matchers.ts +++ b/packages/next-routing/src/matchers.ts @@ -25,9 +25,7 @@ function matchesCondition( // Try to match as regex first try { const exactRegex = new RegExp(`^(?:${conditionValue})$`) - const fallbackRegex = new RegExp(conditionValue) - const match = - actualValue.match(exactRegex) ?? actualValue.match(fallbackRegex) + const match = actualValue.match(exactRegex) if (match) { const namedCaptures: Record = {} if (match.groups) {