Skip to content

Commit 9c8ebe8

Browse files
committed
🐛 Removed /share suffix from shared URLs
1 parent 2a7f8fe commit 9c8ebe8

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

apps/portal/src/components/pages/share-modal.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,22 @@ const getTwitterCreator = () => {
398398
return getHostDocument().querySelector('meta[name="twitter:creator"]')?.content || '';
399399
};
400400

401+
const normalizeShareUrl = (url) => {
402+
if (!url) {
403+
return '';
404+
}
405+
406+
try {
407+
const normalized = new URL(url, getHostWindow().location.href);
408+
normalized.pathname = normalized.pathname.replace(/\/share\/?$/, '/');
409+
return normalized.href;
410+
} catch (_) {
411+
return url;
412+
}
413+
};
414+
401415
const getShareUrl = () => {
402-
return getCanonicalUrl() || getHostWindow().location.href;
416+
return normalizeShareUrl(getCanonicalUrl()) || normalizeShareUrl(getHostWindow().location.href);
403417
};
404418

405419
const getShareTitle = () => {

apps/portal/test/unit/components/pages/share-modal.test.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe('ShareModal', () => {
2424
vi.clearAllMocks();
2525
document.title = '';
2626
document.head.innerHTML = '';
27+
window.history.pushState({}, '', '/post/test/share/?ref=mail');
2728
});
2829

2930
afterEach(() => {
@@ -190,8 +191,9 @@ describe('ShareModal', () => {
190191

191192
const linkedInLink = getByRole('link', {name: 'LinkedIn'});
192193
const linkedInUrl = new URL(linkedInLink.getAttribute('href'));
194+
const expectedShareUrl = `${window.location.origin}/post/test/?ref=mail`;
193195

194-
expect(linkedInUrl.searchParams.get('url')).toBe(window.location.href);
196+
expect(linkedInUrl.searchParams.get('url')).toBe(expectedShareUrl);
195197

196198
const twitterLink = getByRole('link', {name: 'X (Twitter)'});
197199
const twitterUrl = new URL(twitterLink.getAttribute('href'));
@@ -245,7 +247,7 @@ describe('ShareModal', () => {
245247
fireEvent.click(copyButton);
246248

247249
await waitFor(() => {
248-
expect(copyTextToClipboard).toHaveBeenCalledWith(window.location.href);
250+
expect(copyTextToClipboard).toHaveBeenCalledWith(`${window.location.origin}/post/test/?ref=mail`);
249251
expect(getByRole('button', {name: 'Copied'})).toBeInTheDocument();
250252
});
251253

@@ -255,4 +257,20 @@ describe('ShareModal', () => {
255257
expect(getByRole('button', {name: 'Copy link'})).toBeInTheDocument();
256258
});
257259
});
260+
261+
test('normalizes canonical share URL by removing /share suffix', () => {
262+
addHeadTag({
263+
tagName: 'link',
264+
attrs: {
265+
rel: 'canonical',
266+
href: 'https://example.com/post/share/?ref=test'
267+
}
268+
});
269+
270+
const {getByRole} = setup();
271+
const twitterLink = getByRole('link', {name: 'X (Twitter)'});
272+
const twitterUrl = new URL(twitterLink.getAttribute('href'));
273+
274+
expect(twitterUrl.searchParams.get('url')).toBe('https://example.com/post/?ref=test');
275+
});
258276
});

0 commit comments

Comments
 (0)