Skip to content

Commit 9ffa1ed

Browse files
committed
Add the website root to canonical URLs
1 parent 0c6203d commit 9ffa1ed

5 files changed

Lines changed: 13 additions & 5 deletions

File tree

code/common/constants/url.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export const WEBSITE_CANONICAL_URL = 'https://ivasilev.net'
12
export const CC0_URL = 'https://creativecommons.org/public-domain/cc0/'
23
export const EMAIL_URL = 'mailto:ianis@ivasilev.net'
34
export const TELEGRAM_URL = 'https://t.me/ianis_vasilev'

code/common/router.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export async function router(urlPath: UrlPath, env: WebsiteEnvironment): Promise
2727
pageData: undefined,
2828
preload: [{ contentType: 'image', href: '/images/home_page_photo.jpg' }],
2929
urlPath,
30+
canonicalUrlPath: urlPath.clearQueryString(),
3031
}
3132
}
3233

@@ -68,7 +69,7 @@ export async function router(urlPath: UrlPath, env: WebsiteEnvironment): Promise
6869
pageDataHydrationTag: 'pacman',
6970
pageData: await env.services.pacman.fetchRepository(),
7071
urlPath,
71-
canonicalUrlPath: urlPath.pickQueryString(),
72+
canonicalUrlPath: urlPath.clearQueryString(),
7273
}
7374
}
7475

@@ -86,7 +87,7 @@ export async function router(urlPath: UrlPath, env: WebsiteEnvironment): Promise
8687
page: playgroundPage,
8788
pageData: undefined,
8889
urlPath,
89-
canonicalUrlPath: urlPath.pickQueryString(),
90+
canonicalUrlPath: urlPath.clearQueryString(),
9091
}
9192
}
9293

@@ -104,7 +105,7 @@ export async function router(urlPath: UrlPath, env: WebsiteEnvironment): Promise
104105
ogImageName: 'playground',
105106
pageData: undefined,
106107
urlPath,
107-
canonicalUrlPath: urlPath.pickQueryString(),
108+
canonicalUrlPath: urlPath.clearQueryString(),
108109
}
109110

110111
if (env.isContentDynamic()) {

code/common/support/url_path.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ export class UrlPath {
5252
)
5353
}
5454

55+
clearQueryString() {
56+
return this.pickQueryString()
57+
}
58+
5559
getQueryString() {
5660
return Array.from(this.query.entries())
5761
.map(([key, value]) => `${key}=${value.replace('&', '%24')}`)

code/server/components/root.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { body } from '../../common/components/body.ts'
22
import { title } from '../../common/components/title.ts'
3+
import { WEBSITE_CANONICAL_URL } from '../../common/constants/url.ts'
34
import { type WebsiteEnvironment } from '../../common/environment.ts'
45
import { bcp47Encode } from '../../common/languages.ts'
56
import { map } from '../../common/observable.ts'
@@ -30,7 +31,7 @@ export function root({ pageState, rehydrationData }: IRootState, env: WebsiteEnv
3031
),
3132
c.html('link', { rel: 'icon', type: 'image/x-icon', href: '/images/favicon.png' }),
3233
c.html('link', { rel: 'stylesheet', href: '/styles/core.css' }),
33-
pageState.canonicalUrlPath && c.html('link', { rel: 'canonical', href: pageState.canonicalUrlPath.toString() }),
34+
pageState.canonicalUrlPath && c.html('link', { rel: 'canonical', href: WEBSITE_CANONICAL_URL + pageState.canonicalUrlPath.toString() }),
3435
c.html('script', { id: 'rehydrationData', type: 'application/json', text: JSON.stringify(rehydrationData) }),
3536
c.html('script', { type: 'module', src: '/code/client/core/index.js', defer: true }),
3637
// This "JavaScript-only" hack is based on https://stackoverflow.com/a/431554/2756776

code/server/meta/open_graph.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import { type IMetaTag } from './types.ts'
9+
import { WEBSITE_CANONICAL_URL } from '../../common/constants/url.ts'
910
import { WEBSITE_LANGUAGE_IDS, bcp47Encode } from '../../common/languages.ts'
1011
import { type GetText } from '../../common/translation.ts'
1112
import { type IWebsitePageState } from '../../common/types/page.ts'
@@ -26,7 +27,7 @@ export function* iterOpenGraphTags(gettext: GetText, pageState: IWebsitePageStat
2627
if (pageState.canonicalUrlPath) {
2728
yield {
2829
name: 'og:url',
29-
content: pageState.canonicalUrlPath.toString(),
30+
content: WEBSITE_CANONICAL_URL + pageState.canonicalUrlPath.toString(),
3031
}
3132
}
3233

0 commit comments

Comments
 (0)