Skip to content

Commit ec8776e

Browse files
authored
fix: Handle query in <Link /> correctly when using localePrefix: 'as-needed' with domains (#1732)
Fixes #1731 Many thanks to @seanpavlov for analyzing the issue and suggesting the fix!
1 parent a881e62 commit ec8776e

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

packages/next-intl/.size-limit.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,37 @@ const config: SizeLimitConfig = [
1515
name: "import {createSharedPathnamesNavigation} from 'next-intl/navigation' (react-client)",
1616
path: 'dist/production/navigation.react-client.js',
1717
import: '{createSharedPathnamesNavigation}',
18-
limit: '4.125 KB'
18+
limit: '4.145 KB'
1919
},
2020
{
2121
name: "import {createLocalizedPathnamesNavigation} from 'next-intl/navigation' (react-client)",
2222
path: 'dist/production/navigation.react-client.js',
2323
import: '{createLocalizedPathnamesNavigation}',
24-
limit: '4.125 KB'
24+
limit: '4.145 KB'
2525
},
2626
{
2727
name: "import {createNavigation} from 'next-intl/navigation' (react-client)",
2828
path: 'dist/production/navigation.react-client.js',
2929
import: '{createNavigation}',
30-
limit: '4.125 KB'
30+
limit: '4.145 KB'
3131
},
3232
{
3333
name: "import {createSharedPathnamesNavigation} from 'next-intl/navigation' (react-server)",
3434
path: 'dist/production/navigation.react-server.js',
3535
import: '{createSharedPathnamesNavigation}',
36-
limit: '16.805 KB'
36+
limit: '16.825 KB'
3737
},
3838
{
3939
name: "import {createLocalizedPathnamesNavigation} from 'next-intl/navigation' (react-server)",
4040
path: 'dist/production/navigation.react-server.js',
4141
import: '{createLocalizedPathnamesNavigation}',
42-
limit: '16.805 KB'
42+
limit: '16.82 KB'
4343
},
4444
{
4545
name: "import {createNavigation} from 'next-intl/navigation' (react-server)",
4646
path: 'dist/production/navigation.react-server.js',
4747
import: '{createNavigation}',
48-
limit: '16.805 KB'
48+
limit: '16.82 KB'
4949
},
5050
{
5151
name: "import * from 'next-intl/server' (react-client)",

packages/next-intl/src/navigation/createNavigation.test.tsx

+42
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,15 @@ describe.each([
924924
);
925925
expect(markup).toContain('href="/de/about"');
926926
});
927+
928+
it('accepts search params', () => {
929+
render(
930+
<Link href={{pathname: '/about', query: {foo: 'bar'}}}>Test</Link>
931+
);
932+
expect(
933+
screen.getByRole('link', {name: 'Test'}).getAttribute('href')
934+
).toBe('/about?foo=bar');
935+
});
927936
});
928937

929938
describe('getPathname', () => {
@@ -978,6 +987,39 @@ describe.each([
978987
});
979988
});
980989

990+
describe("localePrefix: 'as-needed', with `domains` and `pathnames`", () => {
991+
const {Link, permanentRedirect, redirect} = createNavigation({
992+
locales,
993+
defaultLocale,
994+
domains,
995+
localePrefix: 'as-needed',
996+
pathnames
997+
});
998+
999+
describe('Link', () => {
1000+
it('accepts search params', () => {
1001+
render(
1002+
<Link href={{pathname: '/about', query: {foo: 'bar'}}}>Test</Link>
1003+
);
1004+
expect(
1005+
screen.getByRole('link', {name: 'Test'}).getAttribute('href')
1006+
).toBe('/about?foo=bar');
1007+
});
1008+
});
1009+
1010+
describe.each([
1011+
['redirect', redirect, nextRedirect],
1012+
['permanentRedirect', permanentRedirect, nextPermanentRedirect]
1013+
])('%s', (_, redirectFn, nextRedirectFn) => {
1014+
it('accepts search params', () => {
1015+
runInRender(() =>
1016+
redirectFn({href: {pathname: '/', query: {foo: 'bar'}}, locale: 'en'})
1017+
);
1018+
expect(nextRedirectFn).toHaveBeenLastCalledWith('/en?foo=bar');
1019+
});
1020+
});
1021+
});
1022+
9811023
describe("localePrefix: 'never'", () => {
9821024
const {Link, getPathname, permanentRedirect, redirect} = createNavigation({
9831025
locales,

packages/next-intl/src/navigation/shared/createSharedNavigationFns.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ export default function createSharedNavigationFns<
9898
{href, locale, ...rest}: LinkProps<Pathname>,
9999
ref: ComponentProps<typeof BaseLink>['ref']
100100
) {
101-
let pathname, params;
101+
let pathname, params, query;
102102
if (typeof href === 'object') {
103103
pathname = href.pathname;
104+
query = href.query;
104105
// @ts-expect-error -- This is ok
105106
params = href.params;
106107
} else {
@@ -159,7 +160,10 @@ export default function createSharedNavigationFns<
159160
// @ts-expect-error -- This is ok
160161
{
161162
locale: curLocale,
162-
href: pathnames == null ? pathname : {pathname, params}
163+
href:
164+
pathnames == null
165+
? {pathname, query}
166+
: {pathname, query, params}
163167
},
164168
false
165169
)

0 commit comments

Comments
 (0)