Skip to content

Commit

Permalink
Fix middleware prefetch cases (#43056)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
ijjk committed Nov 18, 2022
1 parent cb8eeef commit 2f744a1
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions packages/next/shared/lib/router/router.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 2f744a1

Please sign in to comment.