Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/next/src/server/route-modules/route-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,9 +763,7 @@ export abstract class RouteModule<
}

const defaultLocale =
getRequestMeta(req, 'defaultLocale') ||
domainLocale?.defaultLocale ||
i18n?.defaultLocale
getRequestMeta(req, 'defaultLocale') || i18n?.defaultLocale

// Ensure parsedUrl.pathname includes locale before processing
// rewrites or they won't match correctly.
Expand Down
76 changes: 76 additions & 0 deletions test/e2e/i18n-support/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2645,6 +2645,82 @@ export function runTests(ctx) {
expect(JSON.parse($2('#router-locales').text())).toEqual(locales)
})

it('should not use domain defaultLocale when visiting domain locale on default host', async () => {
// Visit /go/another on localhost (no host header matching a configured domain)
// defaultLocale should be en-US (main config), not go (example.com domain)
const res = await fetchViaHTTP(
ctx.appPort,
`${ctx.basePath}/go/another`,
undefined,
{
redirect: 'manual',
}
)

expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
expect($('#router-locale').text()).toBe('go')
expect($('#router-default-locale').text()).toBe('en-US')
expect(JSON.parse($('#props').text()).defaultLocale).toBe('en-US')

// Also test with the other domain locale (do)
const res2 = await fetchViaHTTP(
ctx.appPort,
`${ctx.basePath}/do/another`,
undefined,
{
redirect: 'manual',
}
)

expect(res2.status).toBe(200)
const html2 = await res2.text()
const $2 = cheerio.load(html2)
expect($2('#router-locale').text()).toBe('do')
expect($2('#router-default-locale').text()).toBe('en-US')
expect(JSON.parse($2('#props').text()).defaultLocale).toBe('en-US')
})

it("should not use domain defaultLocale when visiting a domain's secondary locale on default host", async () => {
// `go-BE`/`do-BE` are not a domain's defaultLocale; they only appear in a
// domain's `locales` array. This exercises the `item.locales` match branch
// of detectDomainLocale (distinct from the defaultLocale match above), and
// the request's defaultLocale should still be en-US (main config).
const res = await fetchViaHTTP(
ctx.appPort,
`${ctx.basePath}/go-BE/another`,
undefined,
{
redirect: 'manual',
}
)

expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
expect($('#router-locale').text()).toBe('go-BE')
expect($('#router-default-locale').text()).toBe('en-US')
expect(JSON.parse($('#props').text()).defaultLocale).toBe('en-US')

// Also test with the other domain's secondary locale (do-BE)
const res2 = await fetchViaHTTP(
ctx.appPort,
`${ctx.basePath}/do-BE/another`,
undefined,
{
redirect: 'manual',
}
)

expect(res2.status).toBe(200)
const html2 = await res2.text()
const $2 = cheerio.load(html2)
expect($2('#router-locale').text()).toBe('do-BE')
expect($2('#router-default-locale').text()).toBe('en-US')
expect(JSON.parse($2('#props').text()).defaultLocale).toBe('en-US')
})

it('should not strip locale prefix for default locale with locale domains', async () => {
const res = await fetchViaHTTP(
ctx.appPort,
Expand Down
Loading