Skip to content

Adding the same page multiple times results in strange navigation #3428

Closed as not planned

Description

@johnthagen

Bug description

If the user adds a page twice to their project:

nav:
  - index.md
  - Other:
      - other/new.md
      - other/new2.md
  - Other2:
      - other/new.md
      - other/new2.md

Then the navigation gets broken in two ways:

  1. The title headings are None
  2. Clicking on the None headings does not highlight them, only the "last" in the list.
Screenshot 2023-10-16 at 8 07 33 AM

I'm unsure if this is supposed to be supported, but I think that at a minimum a better error message during build could be useful to officially disallow this if it's not supported.

Related links

Reproduction

9.4.6-duplicate-nav.zip

Steps to reproduce

  1. poetry install
  2. nox -s docs_serve

Browser

Chrome

Context

Originally reported at:

Material for Mkdocs author said that this was an upstream issue with MkDocs:

Activity

  1. oprypin commented on Oct 16, 2023

    @oprypin
  2. rachel-rigdon-sp commented on Dec 21, 2023

    @rachel-rigdon-sp

    My team is also experiencing this issue. Is there a resolution?

  3. oprypin commented on Dec 21, 2023

    @oprypin
  4. rachel-rigdon-sp commented on Dec 23, 2023

    @rachel-rigdon-sp

    Yeah I suppose, don't add the same page multiple times?

    That is a workaround, not a resolution. If it is not technically feasible, then perhaps this issue should be closed to reflect that.

  5. lovelydinosaur commented on Apr 15, 2024

    @lovelydinosaur
    Collaborator

    It's not obvious eg. how "next"/"prev" could possibly work in this context, so it seems like a broken case to me.

    My expected optimal behavior here is probably one of...

    • Ignore it. Edge cases might do edgy things. C'est la vie. - The "don't add the same page multiple times" comment above might be taken as a little flippant, but it's actually sensible advice.
    • Raise an error.

    Is there a different behavior here that I'm missing, that we should prefer?

  6. johnthagen commented on Apr 15, 2024

    @johnthagen
    Author

    It's been a while since I reported this, but I think raising an error (or warning since users can opt into treating warnings as errors) rather than silently producing inconsistent results would be best for the user IMO. That helps the user catch mistakes like this in CI.

  7. trel commented on Apr 15, 2024

    @trel
    Contributor

    Raise an error.

    Or at minimum, a warning. There could be a real use case that would be prevented by raising an error.

  8. pawamoy commented on Apr 17, 2024

    @pawamoy
    Contributor

    A solution is to use pymdownx.snippets to re-use/include the same content in every page. Example:

    • create foo.md, bar.md, baz.md
    • write real contents in foo.md
    • write --8<-- "foo.md" in bar.md and baz.md (update path to foo depending on your snippets config, see the extension docs)

    @squidfunk let me know that this could cause ranking issue with search engines since the contents of the three pages is duplicated. He suggests the following to bypass it:

    • use a custom canonical front-matter property in bar.md and baz.md like:

      ---
      canonical: foo.md
      ---
    • use the on_page_context hook to resolve "foo.md" to the right URL using the Files collection

    • set the Page.canonical_url of the bar and baz pages

    • IIUC themes then handle the rest, setting the right header metadata on the HTML page for search engines and their ranking algorithms

    We consider this use-case to be out of scope for MkDocs core, so we'll recommend the solution above, but we could maybe indeed also add a warning when the same page is referenced more than once in the navigation. However that opens us up to broken builds if some users are already referencing the same page multiple times and fixing the resulting issue their own way 🤔 Then we'll have to add a validation setting to tune down the warning into an info log. That's more work than what we can put into MkDocs right now for a use-case that seems rare enough, so we'll close as out of scope, but feel free to comment further and ask to re-open if you'd like to work on this (adding a warning/validation).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions