From 307a9400549f8b8556e8e53dab9eb9048ef6748b Mon Sep 17 00:00:00 2001 From: nkzawa Date: Fri, 4 Mar 2022 22:03:37 +0700 Subject: [PATCH 1/2] fix to not call middleware with shallow push, fix middleware call with query parameter --- packages/next/shared/lib/router/router.ts | 9 ++++++--- .../core/pages/rewrites/_middleware.js | 5 ++++- .../middleware/core/pages/rewrites/index.js | 17 +++++++++++++++++ .../middleware/core/test/index.test.js | 11 +++++++++++ 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/packages/next/shared/lib/router/router.ts b/packages/next/shared/lib/router/router.ts index 30214fa6a073..83392b261072 100644 --- a/packages/next/shared/lib/router/router.ts +++ b/packages/next/shared/lib/router/router.ts @@ -32,6 +32,7 @@ import { } from '../utils' import { isDynamicRoute } from './utils/is-dynamic' import { parseRelativeUrl } from './utils/parse-relative-url' +import { parseUrl } from './utils/parse-url' import { searchParamsToUrlQuery } from './utils/querystring' import resolveRewrites from './utils/resolve-rewrites' import { getRouteMatcher } from './utils/route-matcher' @@ -1165,8 +1166,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 +1866,9 @@ export default class Router implements BaseRouter { locale: string | undefined isPreview: boolean }): Promise { + const asPathname = parseUrl(options.as).pathname 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 d9b83488318c..734950493dca 100644 --- a/test/integration/middleware/core/pages/rewrites/_middleware.js +++ b/test/integration/middleware/core/pages/rewrites/_middleware.js @@ -55,7 +55,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 f15b3557ed56..4fbadef290f0 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 5f309b9dcbf7..3665118d2eef 100644 --- a/test/integration/middleware/core/test/index.test.js +++ b/test/integration/middleware/core/test/index.test.js @@ -404,6 +404,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 = '') { From cd299b420c310d7587e30dd0c9770ce741850d21 Mon Sep 17 00:00:00 2001 From: nkzawa Date: Mon, 7 Mar 2022 13:32:51 +0700 Subject: [PATCH 2/2] use pathNoQueryHash --- packages/next/shared/lib/router/router.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/next/shared/lib/router/router.ts b/packages/next/shared/lib/router/router.ts index 83392b261072..da481f225bcb 100644 --- a/packages/next/shared/lib/router/router.ts +++ b/packages/next/shared/lib/router/router.ts @@ -32,7 +32,6 @@ import { } from '../utils' import { isDynamicRoute } from './utils/is-dynamic' import { parseRelativeUrl } from './utils/parse-relative-url' -import { parseUrl } from './utils/parse-url' import { searchParamsToUrlQuery } from './utils/querystring' import resolveRewrites from './utils/resolve-rewrites' import { getRouteMatcher } from './utils/route-matcher' @@ -1866,7 +1865,7 @@ export default class Router implements BaseRouter { locale: string | undefined isPreview: boolean }): Promise { - const asPathname = parseUrl(options.as).pathname + const asPathname = pathNoQueryHash(options.as) const cleanedAs = delLocale( hasBasePath(asPathname) ? delBasePath(asPathname) : asPathname, options.locale