Skip to content

Commit d54ed07

Browse files
authored
Fix missing locale info for middleware data request (#54357)
This ensures we properly populate locale information with `skipMiddlewareUrlNormalize` enabled as we shouldn't provide incorrect values even if we are skipping normalizing. Fixes: #53646
1 parent ffe2d04 commit d54ed07

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

packages/next/src/shared/lib/router/utils/get-next-pathname-info.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ export function getNextPathnameInfo(
6464
info.pathname = removePathPrefix(info.pathname, basePath)
6565
info.basePath = basePath
6666
}
67+
let pathnameNoDataPrefix = info.pathname
6768

6869
if (
69-
options.parseData === true &&
7070
info.pathname.startsWith('/_next/data/') &&
7171
info.pathname.endsWith('.json')
7272
) {
@@ -76,21 +76,36 @@ export function getNextPathnameInfo(
7676
.split('/')
7777

7878
const buildId = paths[0]
79-
info.pathname = paths[1] !== 'index' ? `/${paths.slice(1).join('/')}` : '/'
8079
info.buildId = buildId
80+
pathnameNoDataPrefix =
81+
paths[1] !== 'index' ? `/${paths.slice(1).join('/')}` : '/'
82+
83+
// update pathname with normalized if enabled although
84+
// we use normalized to populate locale info still
85+
if (options.parseData === true) {
86+
info.pathname = pathnameNoDataPrefix
87+
}
8188
}
8289

8390
// If provided, use the locale route normalizer to detect the locale instead
8491
// of the function below.
85-
if (options.i18nProvider) {
86-
const result = options.i18nProvider.analyze(info.pathname)
92+
if (i18n) {
93+
let result = options.i18nProvider
94+
? options.i18nProvider.analyze(info.pathname)
95+
: normalizeLocalePath(info.pathname, i18n.locales)
96+
8797
info.locale = result.detectedLocale
8898
info.pathname = result.pathname ?? info.pathname
89-
} else if (i18n) {
90-
const pathLocale = normalizeLocalePath(info.pathname, i18n.locales)
91-
info.locale = pathLocale.detectedLocale
92-
info.pathname = pathLocale.pathname ?? info.pathname
93-
}
9499

100+
if (!result.detectedLocale && info.buildId) {
101+
result = options.i18nProvider
102+
? options.i18nProvider.analyze(pathnameNoDataPrefix)
103+
: normalizeLocalePath(pathnameNoDataPrefix, i18n.locales)
104+
105+
if (result.detectedLocale) {
106+
info.locale = result.detectedLocale
107+
}
108+
}
109+
}
95110
return info
96111
}

test/e2e/skip-trailing-slash-redirect/app/middleware.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ export default function handler(req) {
1818
console.log(req.nextUrl)
1919
let { pathname } = req.nextUrl
2020

21+
if (pathname.startsWith('/_next/data') && pathname.includes('locale-test')) {
22+
return NextResponse.json({
23+
locale: req.nextUrl.locale,
24+
pathname: req.nextUrl.pathname,
25+
})
26+
}
27+
2128
if (pathname.includes('docs') || pathname.includes('chained-rewrite')) {
2229
if (pathname.startsWith('/en')) {
2330
pathname = pathname.replace(/^\/en/, '') || '/'

test/e2e/skip-trailing-slash-redirect/index.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ describe('skip-trailing-slash-redirect', () => {
1616
})
1717
afterAll(() => next.destroy())
1818

19+
it('should parse locale info for data request correctly', async () => {
20+
const pathname = `/_next/data/${next.buildId}/ja-jp/locale-test.json`
21+
const res = await next.fetch(pathname)
22+
23+
expect(await res.json()).toEqual({
24+
locale: 'ja-jp',
25+
pathname,
26+
})
27+
})
28+
1929
it.each(['EN', 'JA-JP'])(
2030
'should be able to redirect locale casing $1',
2131
async (locale) => {

0 commit comments

Comments
 (0)