Skip to content

Commit 00f0998

Browse files
committed
Throw for missing params in localized pathnames
1 parent dc91509 commit 00f0998

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

packages/next-intl/src/navigation/utils.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ export function compileLocalizedPathname<Locales extends AllLocales, Pathname>({
106106
function compilePath(
107107
namedPath: Pathnames<Locales>[keyof Pathnames<Locales>]
108108
) {
109-
let compiled =
109+
const template =
110110
typeof namedPath === 'string' ? namedPath : namedPath[locale];
111+
let compiled = template;
111112

112113
if (params) {
113114
Object.entries(params).forEach(([key, value]) => {
@@ -122,6 +123,15 @@ export function compileLocalizedPathname<Locales extends AllLocales, Pathname>({
122123
});
123124
}
124125

126+
if (process.env.NODE_ENV !== 'production' && compiled.includes('[')) {
127+
// Next.js throws anyway, therefore better provide a more helpful error message
128+
throw new Error(
129+
`Insufficient params provided for localized pathname.\nTemplate: ${template}\nParams: ${JSON.stringify(
130+
params
131+
)}`
132+
);
133+
}
134+
125135
if (query) {
126136
compiled += serializeSearchParams(query);
127137
}

packages/next-intl/test/navigation/utils.test.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import {describe, expect, it} from 'vitest';
2-
import {serializeSearchParams} from '../../src/navigation/utils';
2+
import {
3+
compileLocalizedPathname,
4+
serializeSearchParams
5+
} from '../../src/navigation/utils';
36

47
describe('serializeSearchParams', () => {
58
it('handles strings', () => {
@@ -18,3 +21,24 @@ describe('serializeSearchParams', () => {
1821
expect(serializeSearchParams({v: ['a', 'b']})).toEqual('?v=a&v=b');
1922
});
2023
});
24+
25+
describe('compileLocalizedPathname', () => {
26+
it('throws when params were not resolved', () => {
27+
const locales: ReadonlyArray<string> = ['en'];
28+
expect(() =>
29+
// @ts-expect-error -- Purposefully miss a param
30+
compileLocalizedPathname<typeof locales, '/test/[one]/[two]'>({
31+
locale: 'en',
32+
pathname: '/test/[one]/[two]',
33+
pathnames: '/test/[one]/[two]',
34+
params: {one: '1'}
35+
})
36+
).toThrow(
37+
[
38+
'Insufficient params provided for localized pathname.',
39+
'Template: /test/[one]/[two]',
40+
'Params: {"one":"1"}'
41+
].join('\n')
42+
);
43+
});
44+
});

0 commit comments

Comments
 (0)