Skip to content

Commit 1997710

Browse files
authored
NOTICKET - Fix script switch linking with query params (#13608)
1 parent 7df5ad2 commit 1997710

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/app/components/Header/ScriptLink/index.test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ describe(`Script Link`, () => {
6868
});
6969
});
7070

71+
it('should set the correct alternate variant when URL has query parameters', () => {
72+
const { container } = render(<ScriptLink />, {
73+
toggles: enabledToggleState,
74+
service: 'serbian',
75+
variant: 'lat',
76+
pathname: '/serbian/articles/c805k05kr73o/lat?foo=bar&baz=qux',
77+
});
78+
79+
const scriptLink = container.querySelector(`a[data-variant]`);
80+
81+
expect(scriptLink?.getAttribute('href')).toBe(
82+
'/serbian/articles/c805k05kr73o/cyr',
83+
);
84+
});
85+
7186
it('should not render when scriptLink toggle is off', () => {
7287
const disabledToggleState = {
7388
scriptLink: {

src/app/components/Header/ScriptLink/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const ScriptLink = () => {
1717

1818
const pathPartsWithoutExtension = pathname
1919
.replace(/\.[^/.]+$/, '') // remove any extensions, we don't want to link to AMP pages directly
20-
.split('/');
20+
.split('?')?.[0] // remove any query parameters
21+
.split('/'); // split path into parts
2122

2223
const currentVariantIndex = pathPartsWithoutExtension.indexOf(
2324
currentVariant as string,

0 commit comments

Comments
 (0)