Use `rel=canonical` link to de-duplicate event permalinks

Fix https://github.com/matrix-org/matrix-public-archive/issues/251
This commit is contained in:
Eric Eastwood 2023-06-22 01:12:46 -05:00
parent 3414fcf7b2
commit fdfea1155c
2 changed files with 18 additions and 0 deletions

View File

@ -71,6 +71,11 @@ function renderPageHtml({
metaImageUrl = pageOptions.imageUrl;
}
let maybeRelCanonical = '';
if (pageOptions.canonicalUrl) {
maybeRelCanonical = sanitizeHtml(`<link rel="canonical" href="${pageOptions.canonicalUrl}">`);
}
const pageHtml = `
<!doctype html>
<html lang="en">
@ -83,6 +88,7 @@ function renderPageHtml({
${sanitizeHtml(`<meta property="og:image" content="${metaImageUrl}">`)}
<link rel="icon" href="${pageAssetUrls.faviconIco}" sizes="any">
<link rel="icon" href="${pageAssetUrls.faviconSvg}" type="image/svg+xml">
${maybeRelCanonical}
${styles
.map(
(styleUrl) =>

View File

@ -917,6 +917,18 @@ router.get(
blockedBySafeSearch: isNsfw,
entryPoint: 'client/js/entry-client-hydrogen.js',
locationHref: urlJoin(basePath, req.originalUrl),
canonicalUrl: matrixPublicArchiveURLCreator.archiveUrlForDate(
roomIdOrAlias,
new Date(toTimestamp),
{
preferredPrecision: precisionFromUrl,
// We purposely omit `scrollStartEventId` here because the canonical location
// for any given event ID is the page it resides on.
//
// We can avoid passing along the `viaServers` because we already joined the
// room above (see `ensureRoomJoined`).
}
),
shouldIndex,
cspNonce: res.locals.cspNonce,
};