Skip to content

Commit

Permalink
Ensure asPath is correctly normalized in minimalMode with i18n (verce…
Browse files Browse the repository at this point in the history
…l#31281)

This ensures the `asPath` and `req.url` values are normalized correctly for fallback pages with i18n in minimal mode. This also copies the minimal mode test suite to run against i18n as well. This also fixes an issue where rewrite params weren't passed correctly on the client when no params were already used in the destination. 

The provided reproduction has been deployed against this patch [here](https://nextjs-error-repro-9zdu3sp5r-ijjk-testing.vercel.app/page/test-page) show it working. 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`

Fixes: vercel#27563
Fixes: vercel#30203
  • Loading branch information
ijjk authored Nov 11, 2021
1 parent 6abc669 commit 6b89fbf
Show file tree
Hide file tree
Showing 3 changed files with 730 additions and 7 deletions.
6 changes: 6 additions & 0 deletions packages/next/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ export default class Server {
})

try {
// ensure parsedUrl.pathname includes URL before processing
// rewrites or they won't match correctly
if (this.nextConfig.i18n && !url.locale?.path.detectedLocale) {
parsedUrl.pathname = `/${url.locale?.locale}${parsedUrl.pathname}`
}
utils.handleRewrites(req, parsedUrl)

// interpolate dynamic params and normalize URL if needed
Expand Down Expand Up @@ -487,6 +492,7 @@ export default class Server {
? ''
: matchedPathname
}`
url.pathname = parsedUrl.pathname
}

addRequestMeta(req, '__nextHadTrailingSlash', url.locale?.trailingSlash)
Expand Down
10 changes: 3 additions & 7 deletions packages/next/shared/lib/router/utils/prepare-destination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ export function prepareDestination(args: {
query: NextParsedUrlQuery
}) {
const query = Object.assign({}, args.query)
const hadLocale = query.__nextLocale
delete query.__nextLocale
delete query.__nextDefaultLocale

Expand Down Expand Up @@ -170,12 +169,9 @@ export function prepareDestination(args: {

// add path params to query if it's not a redirect and not
// already defined in destination query or path
let paramKeys = Object.keys(args.params)

// remove internal param for i18n
if (hadLocale) {
paramKeys = paramKeys.filter((name) => name !== 'nextInternalLocale')
}
let paramKeys = Object.keys(args.params).filter(
(name) => name !== 'nextInternalLocale'
)

if (
args.appendParamsToQuery &&
Expand Down
Loading

0 comments on commit 6b89fbf

Please sign in to comment.