Skip to content

Commit

Permalink
fix: Handle overlapping locale prefixes correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
amannn committed Sep 17, 2024
1 parent 172e535 commit dab7dae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/next-intl/src/middleware/utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
formatPathnameTemplate,
getInternalTemplate,
getNormalizedPathname,
getPathnameMatch,
getRouteParams
} from './utils';

Expand Down Expand Up @@ -168,3 +169,22 @@ describe('getInternalTemplate', () => {
]);
});
});

describe('getPathnameMatch', () => {
it('prioritizes more specific prefixes for overlapping locales', () => {
expect(
getPathnameMatch('/de/at/test', ['de', 'de-at'], {
mode: 'always',
prefixes: {
'de-at': '/de/at',
de: '/de'
}
})
).toEqual({
locale: 'de-at',
prefix: '/de/at',
exact: true,
matchedPrefix: '/de/at'
});
});
});
3 changes: 3 additions & 0 deletions packages/next-intl/src/middleware/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ export function getPathnameMatch<AppLocales extends Locales>(
| undefined {
const localePrefixes = getLocalePrefixes(locales, localePrefix);

// More specific ones first
localePrefixes.sort((a, b) => b[1].length - a[1].length);

for (const [locale, prefix] of localePrefixes) {
let exact, matches;
if (pathname === prefix || pathname.startsWith(prefix + '/')) {
Expand Down

0 comments on commit dab7dae

Please sign in to comment.