Skip to content

Commit 2f744a1

Browse files
authored
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)
1 parent cb8eeef commit 2f744a1

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

packages/next/shared/lib/router/router.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,13 +2097,13 @@ export default class Router implements BaseRouter {
20972097
data?.json && !wasBailedPrefetch
20982098
? data
20992099
: await fetchNextData({
2100-
dataHref:
2101-
data?.dataHref ||
2102-
this.pageLoader.getDataHref({
2103-
href: formatWithValidation({ pathname, query }),
2104-
asPath: resolvedAs,
2105-
locale,
2106-
}),
2100+
dataHref: data?.json
2101+
? data?.dataHref
2102+
: this.pageLoader.getDataHref({
2103+
href: formatWithValidation({ pathname, query }),
2104+
asPath: resolvedAs,
2105+
locale,
2106+
}),
21072107
isServerRender: this.isSsr,
21082108
parseJSON: true,
21092109
inflightCache: wasBailedPrefetch ? {} : this.sdc,
@@ -2272,6 +2272,7 @@ export default class Router implements BaseRouter {
22722272
let parsed = parseRelativeUrl(url)
22732273

22742274
let { pathname, query } = parsed
2275+
const originalPathname = pathname
22752276

22762277
if (process.env.__NEXT_I18N_SUPPORT) {
22772278
if (options.locale === false) {
@@ -2320,10 +2321,13 @@ export default class Router implements BaseRouter {
23202321
if (rewritesResult.externalDest) {
23212322
return
23222323
}
2323-
resolvedAs = removeLocale(
2324-
removeBasePath(rewritesResult.asPath),
2325-
this.locale
2326-
)
2324+
2325+
if (!isMiddlewareMatch) {
2326+
resolvedAs = removeLocale(
2327+
removeBasePath(rewritesResult.asPath),
2328+
this.locale
2329+
)
2330+
}
23272331

23282332
if (rewritesResult.matchedPage && rewritesResult.resolvedHref) {
23292333
// if this directly matches a page we need to update the href to
@@ -2365,7 +2369,10 @@ export default class Router implements BaseRouter {
23652369
fetchData: () =>
23662370
fetchNextData({
23672371
dataHref: this.pageLoader.getDataHref({
2368-
href: formatWithValidation({ pathname, query }),
2372+
href: formatWithValidation({
2373+
pathname: originalPathname,
2374+
query,
2375+
}),
23692376
skipInterpolation: true,
23702377
asPath: resolvedAs,
23712378
locale,
@@ -2408,13 +2415,13 @@ export default class Router implements BaseRouter {
24082415
this.pageLoader._isSsg(route).then((isSsg) => {
24092416
return isSsg
24102417
? fetchNextData({
2411-
dataHref:
2412-
data?.dataHref ||
2413-
this.pageLoader.getDataHref({
2414-
href: url,
2415-
asPath: resolvedAs,
2416-
locale: locale,
2417-
}),
2418+
dataHref: data?.json
2419+
? data?.dataHref
2420+
: this.pageLoader.getDataHref({
2421+
href: url,
2422+
asPath: resolvedAs,
2423+
locale: locale,
2424+
}),
24182425
isServerRender: false,
24192426
parseJSON: true,
24202427
inflightCache: this.sdc,

0 commit comments

Comments
 (0)