diff --git a/packages/next/src/shared/lib/router/utils/prepare-destination.ts b/packages/next/src/shared/lib/router/utils/prepare-destination.ts index fdd86e151ead3..95b1fab3da7cd 100644 --- a/packages/next/src/shared/lib/router/utils/prepare-destination.ts +++ b/packages/next/src/shared/lib/router/utils/prepare-destination.ts @@ -176,7 +176,9 @@ export function prepareDestination(args: { let escapedDestination = args.destination for (const param of Object.keys({ ...args.params, ...query })) { - escapedDestination = escapeSegment(escapedDestination, param) + escapedDestination = param + ? escapeSegment(escapedDestination, param) + : escapedDestination } const parsedDestination = parseUrl(escapedDestination) diff --git a/test/e2e/app-dir/app/index.test.ts b/test/e2e/app-dir/app/index.test.ts index c813f863ab884..89412657ce973 100644 --- a/test/e2e/app-dir/app/index.test.ts +++ b/test/e2e/app-dir/app/index.test.ts @@ -189,6 +189,7 @@ describe('app dir - basic', () => { { pathname: '/blog/old-post' }, { pathname: '/redirect-3/some' }, { pathname: '/redirect-4' }, + { pathname: '/redirect-4/?q=1&=' }, ])( 'should match redirects in pages correctly $path', async ({ pathname }) => { diff --git a/test/e2e/app-dir/rewrites-redirects/rewrites-redirects.test.ts b/test/e2e/app-dir/rewrites-redirects/rewrites-redirects.test.ts index 4e44901dbfe77..0e4f9e3bebadf 100644 --- a/test/e2e/app-dir/rewrites-redirects/rewrites-redirects.test.ts +++ b/test/e2e/app-dir/rewrites-redirects/rewrites-redirects.test.ts @@ -68,5 +68,14 @@ describe('redirects and rewrites', () => { const url = new URL(await browser.url()) expect(url.pathname).toEndWith('-after/thing') }) + it('should redirect from next.config.js correctly with empty query params', async () => { + const browser = await next.browser('/?q=1&=') + await browser + .elementById(`${testType}-config-redirect`) + .click() + .waitForElementByCss('.page_config-redirect-after') + const url = new URL(await browser.url()) + expect(url.pathname).toEndWith('-after') + }) }) })