initial idea for canonical version logic

This commit is contained in:
Kevin Hendel 2024-05-31 16:42:15 +02:00
parent 2412a1b7bc
commit 7d0087a82a
3 changed files with 23 additions and 2 deletions

View File

@ -96,6 +96,7 @@ export interface Config {
search: string /* Search worker URL */
tags?: Record<string, string> /* Tags mapping */
version?: Versioning /* Versioning */
canonical_version?: string /* Optional canonical version set in Mike */
}
/* ----------------------------------------------------------------------------

View File

@ -132,8 +132,25 @@ export function setupVersionSelector(
.pipe(
map(sitemap => {
const location = getLocation()
// checkPath is used to check if the current page is in the sitemap even if it is the canonical version
let checkPath
// path is the actual path of the page we want to redirect to
const path = location.href.replace(config.base, url)
return sitemap.has(path.split("#")[0])
// If the canonical version is set, we replace the current version with the canonical version in the checkPath
if (config.canonical_version) {
const baseUrl = new URL(config.base)
// Make sure the canonical version has a trailing slash if the base URL has one
if (config.base.endsWith("/") && !config.canonical_version.endsWith("/")) {
baseUrl.pathname = `${config.canonical_version}/`
} else {
baseUrl.pathname = config.canonical_version
}
checkPath = location.href.replace(config.base, baseUrl.href)
} else {
checkPath = path
}
return sitemap.has(checkPath.split("#")[0])
? new URL(path)
: new URL(url)
})

View File

@ -400,6 +400,9 @@
{%- if not mike or mike.config.version_selector -%}
{%- set _ = app.update({ "version": config.extra.version}) -%}
{%- endif -%}
{%- if mike.config.canonical_version -%}
{%- set _ = app.update({ "canonical_version": mike.config.canonical_version }) -%}
{%- endif -%}
{%- endif -%}
<!-- Tags -->