Conditional canonical link in head custom code

Hey @jvictormendes

I’m not 100% sure if that would work (as it’s generated only after document is loaded), but this nifty little script I wrote adds that link conditionally:

const curl = "CANONICAL URL"
if (curl != "") {
  link=document.createElement('link');
  link.href='CANONICAL URL';
  link.rel='canonical';
  document.getElementsByTagName('head')[0].appendChild(link);
}

Should do the trick. The condition here works as following: If you haven’t set any Canonical URL inside of the Canonical Url field in your collection, then it won’t do anything. If you add a link, it would add it after document loads.


Please note that Webflow doesn’t support Custom Code help. If you run into any troubles with this script, feel free to ask on forum.

9 Likes