Skip to content

Improvement (v2): Allow authors to define a canonical URL to help with SEO #2603

Closed

Description

@jcomack

🚀 Feature

Allow users to define an optional canonical URL per doc item.

Have you read the Contributing Guidelines on issues?

Yes

Motivation

Allowing users to define a canonical URL will greatly help with pointing search engines into the right direction, especially when multiple versions of the same doc item exist.

Pitch

As mentioned in the motivation, allowing users to define a canonical URL will greatly help with efforts regarding SEO. Not only will this ensure that search engines know what version of a particular doc item is most relevant, it will also ensure that other versions of the same doc item are known and are pointing to the most relevant / newest version. Also see https://yoast.com/rel-canonical/ for more information.

How this feature could work, is by adding an additional (optional) Markdown property that can defined in the header of the particular doc item. This property can then be used and outputted in the HTML as a <link rel="canonical" href="https://example.com/mydocitem/" />.

If this property is omitted from the header, it should still be outputted in the resulting HTML, but must point to itself.

Activity

  1. added
    feature
    status: needs triage
    on Apr 14, 2020
  2. removed
    status: needs triage
    on Jun 5, 2020
  3. slorber commented on Oct 1, 2020

    @slorber
    Collaborator

    Hey,

    It is possible to use custom SEO components directly inside a markdown file, thanks to MDX. It is not the best DX but is quite flexible in what it can do to override any meta we set by default.

    # Markdown title
    
    Some markdown text
    
    import Head from '@docusaurus/Head';
    
    <Head>
        <title>My custom doc title</title>
        <link rel="canonical" href="http://mysite.com/example" />
    </Head>

    The drawback is that you have to do this for all the docs you want to modify metadata (but that would be similar if we provided a frontmatter option anyway)

    Do you think we can close this issue?

  4. maxee commented on Jan 11, 2021

    @maxee

    @slorber In my case, i'd like to add custom tags to some pages (excluding robots, for example).

    I adopted the snippet you've posted, but I cannot get it working.
    The MDX syntax is ok, the page is rendering, but the custom header tags don't appear at all.

    Here is my markdown page:

    ---
    title: About us
    hide_table_of_contents: true
    ---
    
    # About us
    
    import Head from '@docusaurus/Head';
    
    <Head>
        <meta name="robots" content="noindex,nofollow,noarchive" />
    </Head>
    
    Further text is going here
    

    I'm using the latest beta 70, versions 68/69 showed the same behavior.

    Is there anything I might have missed?

  5. jcomack commented on Jan 12, 2021

    @jcomack
    ContributorAuthor

    Hey,

    It is possible to use custom SEO components directly inside a markdown file, thanks to MDX. It is not the best DX but is quite flexible in what it can do to override any meta we set by default.

    # Markdown title
    
    Some markdown text
    
    import Head from '@docusaurus/Head';
    
    <Head>
        <title>My custom doc title</title>
        <link rel="canonical" href="http://mysite.com/example" />
    </Head>

    The drawback is that you have to do this for all the docs you want to modify metadata (but that would be similar if we provided a frontmatter option anyway)

    Do you think we can close this issue?

    Hi @slorber, for some reason I'm only just not getting a notification that you responded.

    Sadly, this would mean the doc maintainer (whom might not be the handiest when it comes to writing HTML/MDX), would need to be very aware of the proper syntax, would could easily lead to mistakes IMHO. Having a single property would indeed still mean the author needs to define it for every page that needs it customized, but only having to manipulate an option seems far less cumbersome, from a UX standpoint

  6. slorber commented on Jan 12, 2021

    @slorber
    Collaborator

    @maxee you are right, it does not seem to work. Will add support for this.

    @jcomack , I agree that first-class frontmatter support is simpler, but at the same time you have your usecase for canonical urls and other users have theirs for other metadata. First-class support means we'd end up with an infinite amount of frontmatter metadata we'd have to maintain in every single docusaurus plugin, cluttering the frontmatter API.

    Each person can already implement its own doc renderer by swizzling the DocItem component, and adding the required metadata.

    What if I made it so it was possible to write meta tags directly in the doc?

    ---
    title: About us
    hide_table_of_contents: true
    ---
    
    <meta name="robots" content="noindex,nofollow,noarchive" />
    
    <link rel="canonical" href="http://mysite.com/example" />
    
    # About us
    
    Further text is going here

    What would be the ideal API that would solve all the head meta usecases?

    For the canonical URL, I think it's common enough and we can make it an exception and add Yaml support, but still think it's a good idea to provide a generic solution to this problem.

    Maybe just supporting link and meta arrays in frontmatter would be enough for a large quantity of usecases? Do you think of other head meta that would be interested to support?

    What do you think of using Yaml arrays?

    ---
    title: About us
    hide_table_of_contents: true
    link:
      - rel: canonical
        href: http://mysite.com/example
    meta:
      - name: robots
        content: noindex,nofollow,noarchive
    ---
    
    # About us
    
    Further text is going here
  7. maxee commented on Jan 17, 2021

    @maxee

    @slorber

    For the head meta-usecases, i like the idea with the YAML arrays. Fits perfect for meta-tags.

    For all other use cases I think a generic solution like the one you described in your first answer (MDX) would be good.
    If the MDX-solution would work, all further use cases would be covered.

    However if realization of the MDX-solution is too complex I'd go for the YAML arrays for link-tags as well.

  8. a-parser commented on May 18, 2021

    @a-parser
  9. slorber commented on Aug 10, 2021

    @slorber
    Collaborator

    Hey

    I'm adding support for using <head> in md files as a shortcut to the Docusaurus <Head> component in PR #5330

    I prefer this solution based on <Head> because it is more flexible than yaml and cover various use-cases at once, including adding html/body attributes, and can even support evaluating the metadata at runtime thanks to MDX.

    This will now work:

    ---
    id: my-doc-id
    ---
    
    <head>
        <link rel="canonical" href="http://mysite.com/example" />
    </head>
    
    # Markdown title
    
    Some markdown text

    We may add frontmatter later: it can be useful if we support another parser than MDX, but in the end it will still remain based on <Head>, just the children would be created from the Yaml. But first I'd like to know if you feel limited with this approach.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions