From 2f744a1ae32a65f1362af0957e3c57f33d102914 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 17 Nov 2022 17:20:14 -0800 Subject: [PATCH] Fix middleware prefetch cases (#43056) This ensures we properly handle prefetching with config based rewrites with middleware configured. No additional tests have been added as the existing tests caught this. Fixes: https://github.com/vercel/next.js/actions/runs/3492657731/jobs/5847159406 ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md) --- packages/next/shared/lib/router/router.ts | 45 +++++++++++++---------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/packages/next/shared/lib/router/router.ts b/packages/next/shared/lib/router/router.ts index cbedc1eec9cff..0815955205b79 100644 --- a/packages/next/shared/lib/router/router.ts +++ b/packages/next/shared/lib/router/router.ts @@ -2097,13 +2097,13 @@ export default class Router implements BaseRouter { data?.json && !wasBailedPrefetch ? data : await fetchNextData({ - dataHref: - data?.dataHref || - this.pageLoader.getDataHref({ - href: formatWithValidation({ pathname, query }), - asPath: resolvedAs, - locale, - }), + dataHref: data?.json + ? data?.dataHref + : this.pageLoader.getDataHref({ + href: formatWithValidation({ pathname, query }), + asPath: resolvedAs, + locale, + }), isServerRender: this.isSsr, parseJSON: true, inflightCache: wasBailedPrefetch ? {} : this.sdc, @@ -2272,6 +2272,7 @@ export default class Router implements BaseRouter { let parsed = parseRelativeUrl(url) let { pathname, query } = parsed + const originalPathname = pathname if (process.env.__NEXT_I18N_SUPPORT) { if (options.locale === false) { @@ -2320,10 +2321,13 @@ export default class Router implements BaseRouter { if (rewritesResult.externalDest) { return } - resolvedAs = removeLocale( - removeBasePath(rewritesResult.asPath), - this.locale - ) + + if (!isMiddlewareMatch) { + resolvedAs = removeLocale( + removeBasePath(rewritesResult.asPath), + this.locale + ) + } if (rewritesResult.matchedPage && rewritesResult.resolvedHref) { // if this directly matches a page we need to update the href to @@ -2365,7 +2369,10 @@ export default class Router implements BaseRouter { fetchData: () => fetchNextData({ dataHref: this.pageLoader.getDataHref({ - href: formatWithValidation({ pathname, query }), + href: formatWithValidation({ + pathname: originalPathname, + query, + }), skipInterpolation: true, asPath: resolvedAs, locale, @@ -2408,13 +2415,13 @@ export default class Router implements BaseRouter { this.pageLoader._isSsg(route).then((isSsg) => { return isSsg ? fetchNextData({ - dataHref: - data?.dataHref || - this.pageLoader.getDataHref({ - href: url, - asPath: resolvedAs, - locale: locale, - }), + dataHref: data?.json + ? data?.dataHref + : this.pageLoader.getDataHref({ + href: url, + asPath: resolvedAs, + locale: locale, + }), isServerRender: false, parseJSON: true, inflightCache: this.sdc,