Skip to content

Commit c65bc21

Browse files
committed
🐛 Fixed share modal metadata lookup to use parent page
ref #26866 Portal runs in an iframe so share metadata must be read from the host page context.
1 parent 1a510f5 commit c65bc21

1 file changed

Lines changed: 31 additions & 13 deletions

File tree

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

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -321,28 +321,46 @@ export const ShareModalStyles = `
321321
322322
`;
323323

324+
const getHostWindow = () => {
325+
try {
326+
if (window.parent && window.parent !== window) {
327+
// Accessing parent.location can throw when cross-origin.
328+
void window.parent.location.href;
329+
return window.parent;
330+
}
331+
} catch (_) {
332+
// Ignore and use the iframe context.
333+
}
334+
335+
return window;
336+
};
337+
338+
const getHostDocument = () => {
339+
return getHostWindow().document;
340+
};
341+
324342
const getCanonicalUrl = () => {
325-
return document.querySelector('link[rel="canonical"]')?.href || '';
343+
return getHostDocument().querySelector('link[rel="canonical"]')?.href || '';
326344
};
327345

328346
const getOgTitle = () => {
329-
return document.querySelector('meta[property="og:title"]')?.content || '';
347+
return getHostDocument().querySelector('meta[property="og:title"]')?.content || '';
330348
};
331349

332350
const getOgDescription = () => {
333-
return document.querySelector('meta[property="og:description"]')?.content || '';
351+
return getHostDocument().querySelector('meta[property="og:description"]')?.content || '';
334352
};
335353

336354
const getMetaDescription = () => {
337-
return document.querySelector('meta[name="description"]')?.content || '';
355+
return getHostDocument().querySelector('meta[name="description"]')?.content || '';
338356
};
339357

340358
const getOgImage = () => {
341-
return document.querySelector('meta[property="og:image"]')?.content || '';
359+
return getHostDocument().querySelector('meta[property="og:image"]')?.content || '';
342360
};
343361

344362
const getTwitterImage = () => {
345-
return document.querySelector('meta[name="twitter:image"]')?.content || '';
363+
return getHostDocument().querySelector('meta[name="twitter:image"]')?.content || '';
346364
};
347365

348366
const getFavicon = () => {
@@ -352,7 +370,7 @@ const getFavicon = () => {
352370
'link[rel="apple-touch-icon"]'
353371
];
354372
for (const selector of selectors) {
355-
const faviconLink = document.querySelector(selector);
373+
const faviconLink = getHostDocument().querySelector(selector);
356374
if (faviconLink?.href) {
357375
return faviconLink.href;
358376
}
@@ -361,31 +379,31 @@ const getFavicon = () => {
361379
};
362380

363381
const getOgSiteName = () => {
364-
return document.querySelector('meta[property="og:site_name"]')?.content || '';
382+
return getHostDocument().querySelector('meta[property="og:site_name"]')?.content || '';
365383
};
366384

367385
const getApplicationName = () => {
368-
return document.querySelector('meta[name="application-name"]')?.content || '';
386+
return getHostDocument().querySelector('meta[name="application-name"]')?.content || '';
369387
};
370388

371389
const getMetaAuthor = () => {
372-
const author = document.querySelector('meta[name="author"]')?.content || '';
390+
const author = getHostDocument().querySelector('meta[name="author"]')?.content || '';
373391
if (author && !/^https?:\/\//i.test(author)) {
374392
return author;
375393
}
376394
return '';
377395
};
378396

379397
const getTwitterCreator = () => {
380-
return document.querySelector('meta[name="twitter:creator"]')?.content || '';
398+
return getHostDocument().querySelector('meta[name="twitter:creator"]')?.content || '';
381399
};
382400

383401
const getShareUrl = () => {
384-
return getCanonicalUrl() || window.location.href;
402+
return getCanonicalUrl() || getHostWindow().location.href;
385403
};
386404

387405
const getShareTitle = () => {
388-
return getOgTitle() || document.title || '';
406+
return getOgTitle() || getHostDocument().title || '';
389407
};
390408

391409
const getShareExcerpt = () => {

0 commit comments

Comments
 (0)