diff --git a/src/templates/assets/javascripts/_/index.ts b/src/templates/assets/javascripts/_/index.ts index 94913968c..e9014a370 100644 --- a/src/templates/assets/javascripts/_/index.ts +++ b/src/templates/assets/javascripts/_/index.ts @@ -96,6 +96,7 @@ export interface Config { search: string /* Search worker URL */ tags?: Record /* Tags mapping */ version?: Versioning /* Versioning */ + canonical_version?: string /* Optional canonical version set in Mike */ } /* ---------------------------------------------------------------------------- diff --git a/src/templates/assets/javascripts/integrations/version/index.ts b/src/templates/assets/javascripts/integrations/version/index.ts index 50de29ad4..21adfde3c 100644 --- a/src/templates/assets/javascripts/integrations/version/index.ts +++ b/src/templates/assets/javascripts/integrations/version/index.ts @@ -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) }) diff --git a/src/templates/base.html b/src/templates/base.html index 2d99225cd..102a0b962 100644 --- a/src/templates/base.html +++ b/src/templates/base.html @@ -398,7 +398,10 @@ {%- if config.extra.version -%} {%- set mike = config.plugins.get("mike") -%} {%- if not mike or mike.config.version_selector -%} - {%- set _ = app.update({ "version": config.extra.version }) -%} + {%- set _ = app.update({ "version": config.extra.version}) -%} + {%- endif -%} + {%- if mike.config.canonical_version -%} + {%- set _ = app.update({ "canonical_version": mike.config.canonical_version }) -%} {%- endif -%} {%- endif -%}