diff --git a/packages/next/shared/lib/router/router.ts b/packages/next/shared/lib/router/router.ts index 30214fa6a0734..da481f225bcbe 100644 --- a/packages/next/shared/lib/router/router.ts +++ b/packages/next/shared/lib/router/router.ts @@ -1165,8 +1165,9 @@ export default class Router implements BaseRouter { * request as it is not necessary. */ if ( - (options as any)._h !== 1 || - isDynamicRoute(removePathTrailingSlash(pathname)) + (!options.shallow || (options as any)._h === 1) && + ((options as any)._h !== 1 || + isDynamicRoute(removePathTrailingSlash(pathname))) ) { const effect = await this._preflightRequest({ as, @@ -1864,8 +1865,9 @@ export default class Router implements BaseRouter { locale: string | undefined isPreview: boolean }): Promise { + const asPathname = pathNoQueryHash(options.as) const cleanedAs = delLocale( - hasBasePath(options.as) ? delBasePath(options.as) : options.as, + hasBasePath(asPathname) ? delBasePath(asPathname) : asPathname, options.locale ) diff --git a/test/integration/middleware/core/pages/rewrites/_middleware.js b/test/integration/middleware/core/pages/rewrites/_middleware.js index dcb61e1d176d2..2d6b154dc86d0 100644 --- a/test/integration/middleware/core/pages/rewrites/_middleware.js +++ b/test/integration/middleware/core/pages/rewrites/_middleware.js @@ -65,7 +65,10 @@ export async function middleware(request) { return NextResponse.rewrite(url) } - if (url.pathname === '/rewrites/rewrite-me-without-hard-navigation') { + if ( + url.pathname === '/rewrites/rewrite-me-without-hard-navigation' || + url.searchParams.get('path') === 'rewrite-me-without-hard-navigation' + ) { url.searchParams.set('middleware', 'foo') url.pathname = request.cookies['about-bypass'] === '1' diff --git a/test/integration/middleware/core/pages/rewrites/index.js b/test/integration/middleware/core/pages/rewrites/index.js index f15b3557ed562..4fbadef290f05 100644 --- a/test/integration/middleware/core/pages/rewrites/index.js +++ b/test/integration/middleware/core/pages/rewrites/index.js @@ -1,6 +1,8 @@ import Link from 'next/link' +import { useRouter } from 'next/router' export default function Home() { + const router = useRouter() return (

Home Page

@@ -32,6 +34,21 @@ export default function Home() { Rewrite me to internal path +
+ { + e.preventDefault() + router.push( + '/rewrites?path=rewrite-me-without-hard-navigation&message=refreshed', + undefined, + { shallow: true } + ) + }} + > + Do not rewrite me +
) } diff --git a/test/integration/middleware/core/test/index.test.js b/test/integration/middleware/core/test/index.test.js index b1ad4d164eec9..1ef9ddf582db9 100644 --- a/test/integration/middleware/core/test/index.test.js +++ b/test/integration/middleware/core/test/index.test.js @@ -484,6 +484,17 @@ function rewriteTests(log, locale = '') { const element = await browser.elementByCss('.title') expect(await element.text()).toEqual('About Bypassed Page') }) + + it(`${locale} should not call middleware with shallow push`, async () => { + const browser = await webdriver(context.appPort, '/rewrites') + await browser.elementByCss('#link-to-shallow-push').click() + await browser.waitForCondition( + 'new URL(window.location.href).searchParams.get("path") === "rewrite-me-without-hard-navigation"' + ) + await expect(async () => { + await browser.waitForElementByCss('.refreshed', 500) + }).rejects.toThrow() + }) } function redirectTests(locale = '') {