Skip to content

Fix i18n fallback: false collision #82136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2025
Merged
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: 2 additions & 2 deletions packages/next/src/server/lib/i18n-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ export class I18NProvider {
// query and strip it from the pathname.
if (analysis.detectedLocale) {
if (analysis.detectedLocale !== detectedLocale) {
throw new Error(
`Invariant: The detected locale does not match the locale in the query. Expected to find '${detectedLocale}' in '${pathname}' but found '${analysis.detectedLocale}'}`
console.warn(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how come this is no longer an invariant?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't hard error here we should be 404ing instead

`The detected locale does not match the locale in the query. Expected to find '${detectedLocale}' in '${pathname}' but found '${analysis.detectedLocale}'}`
)
}

Expand Down
6 changes: 3 additions & 3 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1095,9 +1095,9 @@ export default class NextNodeServer extends BaseServer<
throw new Error('Invariant: pathname is undefined')
}

// This is a catch-all route, there should be no fallbacks so mark it as
// such.
addRequestMeta(req, 'bubbleNoFallback', true)
// When in minimal mode we do not bubble the fallback as the
// router-server is not present to handle the error
addRequestMeta(req, 'bubbleNoFallback', this.minimalMode ? undefined : true)

// This is needed to expose render404 and nextConfig
// for environments without router-server
Expand Down
45 changes: 45 additions & 0 deletions test/e2e/i18n-fallback-collision/i18n-fallback-collision.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { nextTestSetup } from 'e2e-utils'

describe('i18n-disallow-multiple-locales', () => {
const { next } = nextTestSetup({
files: __dirname,
})

it.each([
['/non-existent'],
['/es/non-existent'],
['/first/non-existent'],
['/es/first/non-existent'],
['/first/second/non-existent'],
['/es/first/second/non-existent'],
])(
'should 404 properly for fallback: false non-prerendered %s',
async (pathname) => {
const res = await next.fetch(pathname)
expect(res.status).toBe(404)
}
)

it.each([
{ urlPath: '/first', page: '/[first]' },
{ urlPath: '/first/second', page: '/[first]/[second]' },
{ urlPath: '/first/second/third', page: '/[first]/[second]/[third]' },
{
urlPath: '/first/second/third/fourth',
page: '/[first]/[second]/[third]/[fourth]',
},
])(
'should render properly for fallback: false prerendered $urlPath',
async ({ urlPath, page }) => {
const res = await next.fetch(urlPath)
expect(res.status).toBe(200)
expect(await res.text()).toContain(page)
}
)

it('should render properly for fallback: blocking', async () => {
const res = await next.fetch('/first/second/third/another')
expect(res.status).toBe(200)
expect(await res.text()).toContain('/[first]/[second]/[third]/[fourth]')
})
})
13 changes: 13 additions & 0 deletions test/e2e/i18n-fallback-collision/next.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { NextConfig } from 'next'

const nextConfig: NextConfig = {
/* config options here */
reactStrictMode: true,

i18n: {
defaultLocale: 'en',
locales: ['en', 'es'],
},
}

export default nextConfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export default function Page() {
return (
<>
<p>page: /[first]/[second]/[third]/[fourth]</p>
</>
)
}

export function getStaticProps({ params }) {
return {
props: {
params,
now: Date.now(),
},
}
}

export function getStaticPaths() {
return {
paths: [
{
params: {
first: 'first',
second: 'second',
third: 'third',
fourth: 'fourth',
},
},
],
fallback: 'blocking',
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export default function Page() {
return (
<>
<p>page: /[first]/[second]/[third]</p>
</>
)
}

export function getStaticProps({ params }) {
return {
props: {
params,
now: Date.now(),
},
}
}

export function getStaticPaths() {
return {
paths: [
{
params: { first: 'first', second: 'second', third: 'third' },
},
],
fallback: false,
}
}
27 changes: 27 additions & 0 deletions test/e2e/i18n-fallback-collision/pages/[first]/[second]/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export default function Page() {
return (
<>
<p>page: /[first]/[second]</p>
</>
)
}

export function getStaticProps({ params }) {
return {
props: {
params,
now: Date.now(),
},
}
}

export function getStaticPaths() {
return {
paths: [
{
params: { first: 'first', second: 'second' },
},
],
fallback: false,
}
}
27 changes: 27 additions & 0 deletions test/e2e/i18n-fallback-collision/pages/[first]/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export default function Page() {
return (
<>
<p>page: /[first]</p>
</>
)
}

export function getStaticProps({ params }) {
return {
props: {
params,
now: Date.now(),
},
}
}

export function getStaticPaths() {
return {
paths: [
{
params: { first: 'first' },
},
],
fallback: false,
}
}
5 changes: 5 additions & 0 deletions test/e2e/i18n-fallback-collision/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { AppProps } from 'next/app'

export default function App({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />
}
13 changes: 13 additions & 0 deletions test/e2e/i18n-fallback-collision/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Html, Head, Main, NextScript } from 'next/document'

export default function Document() {
return (
<Html lang="en">
<Head />
<body className="antialiased">
<Main />
<NextScript />
</body>
</Html>
)
}
7 changes: 7 additions & 0 deletions test/e2e/i18n-fallback-collision/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Home() {
return (
<>
<p>index page</p>
</>
)
}
Loading