Skip to content

Commit

Permalink
Use rel=canonical link to de-duplicate event permalinks
Browse files Browse the repository at this point in the history
Fix #251
  • Loading branch information
MadLittleMods committed Jun 22, 2023
1 parent 3414fcf commit fdfea11
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions server/hydrogen-render/render-page-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand All @@ -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) =>
Expand Down
12 changes: 12 additions & 0 deletions server/routes/room-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down

0 comments on commit fdfea11

Please sign in to comment.