11ty conditional canonical

GSC was flagging some Canonical issue on this site so I decided to add the canonical using the page.url parameter in my base.njk nunjucks include.

<link rel="canonical" href="https://www.simoncox.com{{ page.url }}">

However, I have already included a Canonical in the frontmatter of some of my posts and there may be reasons why i'd want the canonical to be on a different url. So I added a conditional to state that if the frontmatter for canonical exists then use it else fall back to the standard page url version. And that gave me this:
{% if canonical %}<link rel="canonical" href="{{ canonical }}">{% else %}<link rel="canonical" href="https://www.simoncox.com{{ page.url }}">{% endif %}

Simple but effective. ^ to MrQuest for the tip showing me how to display raw njk code!

By Simon Cox Published: Mon, Feb 13, 2023

Next short: GSC Page with redirect issue

Previous short: Making my Stadia controller work on bluetooth

Latest Shorts

Or all the short articles

Latest articles

Or all the articles