Skip to content

Commit 52cdef5

Browse files
authored
fix: Correctly detect base path at the app root / when using a locale prefix strategy other than always. This ensures the locale cookie is set correctly. (amannn#999)
Fixes amannn#997
1 parent 67f6166 commit 52cdef5

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@
122122
},
123123
{
124124
"path": "dist/production/navigation.react-client.js",
125-
"limit": "2.94 KB"
125+
"limit": "2.96 KB"
126126
},
127127
{
128128
"path": "dist/production/navigation.react-server.js",
129-
"limit": "3.03 KB"
129+
"limit": "3.06 KB"
130130
},
131131
{
132132
"path": "dist/production/server.react-client.js",

src/navigation/shared/utils.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ export function getRoute<Locales extends AllLocales>({
180180
return template as keyof Pathnames<Locales>;
181181
}
182182

183-
export function getBasePath(pathname: string) {
184-
return window.location.pathname.replace(pathname, '');
183+
export function getBasePath(
184+
pathname: string,
185+
windowPathname = window.location.pathname
186+
) {
187+
if (pathname === '/') {
188+
return windowPathname;
189+
} else {
190+
return windowPathname.replace(pathname, '');
191+
}
185192
}

test/navigation/shared/utils.test.tsx

+19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {describe, expect, it} from 'vitest';
22
import {
33
compileLocalizedPathname,
4+
getBasePath,
45
serializeSearchParams
56
} from '../../../src/navigation/shared/utils';
67

@@ -42,3 +43,21 @@ describe('compileLocalizedPathname', () => {
4243
);
4344
});
4445
});
46+
47+
describe('getBasePath', () => {
48+
it('detects a base path when using a locale prefix and the user is at the root', () => {
49+
expect(getBasePath('/en', '/base/en')).toBe('/base');
50+
});
51+
52+
it('detects a base path when using a locale prefix and the user is at a nested path', () => {
53+
expect(getBasePath('/en/about', '/base/en/about')).toBe('/base');
54+
});
55+
56+
it('detects a base path when using no locale prefix and the user is at the root', () => {
57+
expect(getBasePath('/', '/base')).toBe('/base');
58+
});
59+
60+
it('detects a base path when using no locale prefix and the user is at a nested path', () => {
61+
expect(getBasePath('/about', '/base/about')).toBe('/base');
62+
});
63+
});

0 commit comments

Comments
 (0)