How to fix "//*" canonical error in GatsbyJS

Gatsby creates a "/*" canonical URL when using the "path" from a programmatically created page. Use the page URI to remove this error.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 12/22/2023 at 07:03
Last Updated on 12/22/2023 at 13:47
<p>A flower that represents Gatsby with the text "Fix "//*" canonical error" beneath it.</p>

Gatsby creates a "/*" canonical URL when using the "path" from a programmatically created page. Use the page URI to remove this error.

SubscribeDownload Open Source Gatsby Project
<p>A screenshot of Chrome showing how the canonical url of a page has a "//*" in it.</p>

The following article addresses the issue above by which Gatsby creates pages with a "/*" in the canonical URL instead of the correct URI.

<p>A screenshot of VSCode highlighting the path that is passed into a programatically created page in Gatsby. Highlighted is also how we use it to generate the SEO for the page, through the react helmet NPM package.</p>

This occurs when an individual programmatically create pages using the "gatsby-node." script and subsequently uses the "path" that is produced by the "gatsby-node" script to create the canonical URL.

To avoid it follow the steps outlined below.

How to programmatically create pages using the GraphQL & Gatsby

Step One: Remove the Path

<p>A screenshot of VSCode highlighting how we removed the "path" from the props of a page that was created programmatically using the gatsby-node script.</p>

In all the templates that use the path, remove the path from the props.

Step Two: Add the URI to the GraphQL Query

<p>A screenshot of VSCode highlighting how we added a URI to a GraphQL query.</p>

Add the uri to the GraphQL query and associated typescript types.

How to make a front-end GraphQL query on a page in GatsbyJS

Step Three: Update the Canonical URL

<p>A screenshot of VSCode showing how we pass the URI to the metadata of a page.</p>

Wherever you had previously used the path, use the uri.

Please note this might require you updating the logic.

Step Four: Test

<p>A screenshot of Chrome highlighting that the canonical url has been fixed.</p>

Build, deploy the website and confirm the canonical error has been resolved.

Looking to learn more about ReactJS, GatsbyJS or SEO ?

Search our blog to find educational content on learning SEO as well as how to use ReactJS and GatsbyJS.

Search our Blog

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe