Skip to content

Commit

Permalink
Fix missing locale info for middleware data request (#54357)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ijjk authored Aug 21, 2023
1 parent ffe2d04 commit d54ed07
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ export function getNextPathnameInfo(
info.pathname = removePathPrefix(info.pathname, basePath)
info.basePath = basePath
}
let pathnameNoDataPrefix = info.pathname

if (
options.parseData === true &&
info.pathname.startsWith('/_next/data/') &&
info.pathname.endsWith('.json')
) {
Expand All @@ -76,21 +76,36 @@ export function getNextPathnameInfo(
.split('/')

const buildId = paths[0]
info.pathname = paths[1] !== 'index' ? `/${paths.slice(1).join('/')}` : '/'
info.buildId = buildId
pathnameNoDataPrefix =
paths[1] !== 'index' ? `/${paths.slice(1).join('/')}` : '/'

// update pathname with normalized if enabled although
// we use normalized to populate locale info still
if (options.parseData === true) {
info.pathname = pathnameNoDataPrefix
}
}

// If provided, use the locale route normalizer to detect the locale instead
// of the function below.
if (options.i18nProvider) {
const result = options.i18nProvider.analyze(info.pathname)
if (i18n) {
let result = options.i18nProvider
? options.i18nProvider.analyze(info.pathname)
: normalizeLocalePath(info.pathname, i18n.locales)

info.locale = result.detectedLocale
info.pathname = result.pathname ?? info.pathname
} else if (i18n) {
const pathLocale = normalizeLocalePath(info.pathname, i18n.locales)
info.locale = pathLocale.detectedLocale
info.pathname = pathLocale.pathname ?? info.pathname
}

if (!result.detectedLocale && info.buildId) {
result = options.i18nProvider
? options.i18nProvider.analyze(pathnameNoDataPrefix)
: normalizeLocalePath(pathnameNoDataPrefix, i18n.locales)

if (result.detectedLocale) {
info.locale = result.detectedLocale
}
}
}
return info
}
7 changes: 7 additions & 0 deletions test/e2e/skip-trailing-slash-redirect/app/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export default function handler(req) {
console.log(req.nextUrl)
let { pathname } = req.nextUrl

if (pathname.startsWith('/_next/data') && pathname.includes('locale-test')) {
return NextResponse.json({
locale: req.nextUrl.locale,
pathname: req.nextUrl.pathname,
})
}

if (pathname.includes('docs') || pathname.includes('chained-rewrite')) {
if (pathname.startsWith('/en')) {
pathname = pathname.replace(/^\/en/, '') || '/'
Expand Down
10 changes: 10 additions & 0 deletions test/e2e/skip-trailing-slash-redirect/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ describe('skip-trailing-slash-redirect', () => {
})
afterAll(() => next.destroy())

it('should parse locale info for data request correctly', async () => {
const pathname = `/_next/data/${next.buildId}/ja-jp/locale-test.json`
const res = await next.fetch(pathname)

expect(await res.json()).toEqual({
locale: 'ja-jp',
pathname,
})
})

it.each(['EN', 'JA-JP'])(
'should be able to redirect locale casing $1',
async (locale) => {
Expand Down

0 comments on commit d54ed07

Please sign in to comment.