Skip to content

version_menu_pagelinks generates incorrect URLs when baseURL contains subdirectory #2305

@David-Moreau-TAS

Description

@David-Moreau-TAS

Environment

  • Version of Docsy you are using: 0.12.0
  • How are you using Docsy? As a:
    • Hugo module
    • NPM module
    • Git submodule
    • Other:
  • Version of Hugo you are using (output of hugo version): 0.147.6
  • OS:
    • Linux
    • macOS
    • Windows
    • Other:

Problem

When using version_menu_pagelinks = true with a baseURL that contains a subdirectory, the version selector dropdown generates malformed URLs with duplicated path segments.

  • Expected URL: https://example.com/v2.0/docs/guide/
  • Actual URL: https://example.com/v2.0/v2.0/docs/guide/

Steps to Reproduce

  1. Configure Hugo with a baseURL containing a subdirectory:
    baseURL = "https://example.com/v2.0/"
    
    [params]
    version_menu_pagelinks = true
    
    [[params.versions]]
    version = "v1.0"
    url = "https://example.com/v1.0/"
    
    [[params.versions]]
    version = "v2.0"
    url = "https://example.com/v2.0/"
  2. Navigate to any page (e.g., /docs/guide/)
  3. Open the version selector dropdown
  4. Observe the generated URLs

Root Cause Analysis

The issue is in layouts/partials/navbar-version-selector.html:

{{ $path := "" -}}
{{ if .Site.Params.version_menu_pagelinks -}}
  {{ $path = .Page.RelPermalink -}}
{{ end -}}
{{ range .Site.Params.versions -}}
<li><a class="dropdown-item" href="{{ .url }}{{ $path }}">{{ .version }}</a></li>
{{ end -}}

Problem: When baseURL contains a subdirectory, Hugo's .Page.RelPermalink already includes that subdirectory path.
The template then concatenates this with .url (which also contains the subdirectory), causing duplication.

Example values:
.Page.RelPermalink = /v2.0/docs/guide/
.url = https://example.com/v2.0/
Result: https://example.com/v2.0/v2.0/docs/guide/

Proposed Solution

Replace the current concatenation logic with a more robust URL construction:

{{ range .Site.Params.versions -}}
  {{ $finalURL := .url -}}
  {{ if $.Site.Params.version_menu_pagelinks -}}
    {{ $finalURL = replace $.Page.Permalink $.Site.BaseURL (printf "%s/" (strings.TrimSuffix "/" .url)) -}}
  {{ end -}}
<li><a class="dropdown-item" href="{{ $finalURL }}">{{ .version }}</a></li>
{{ end -}}

This solution:

  • Uses replace to substitute the current site's base URL with the target version's URL.
  • Normalizes URLs by ensuring consistent trailing slashes
  • Preserves the page path correctly without duplication

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions