{
"$type": "site.standard.document",
"canonicalUrl": "https://www.simoncox.com/short-articles/2023-02-13-11ty-conditional-canonical/",
"description": "How I created a conditional canonical tag in my 11ty nunjucks base template.",
"path": "/short-articles/2023-02-13-11ty-conditional-canonical/",
"publishedAt": "2023-02-13T14:17:55.058Z",
"site": "at://did:plc:tki7vwlanxbwrz2er67eaeqa/site.standard.publication/3mp4h4md7zv2y",
"tags": "Eleventy",
"textContent": "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. \n\n{% raw %}<link rel=\"canonical\" href=\"https://www.simoncox.com{{ page.url }}\">{% endraw %}\n\nHowever, 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: \n{% raw %}{% if canonical %}<link rel=\"canonical\" href=\"{{ canonical }}\">{% else %}<link rel=\"canonical\" href=\"https://www.simoncox.com{{ page.url }}\">{% endif %}{% endraw %}\n\nSimple but effective.\n^ to MrQuest for the tip showing me how to display raw njk code!",
"title": "11ty conditional canonical"
}