Last active
          July 12, 2024 10:40 
        
      - 
      
 - 
        
Save krschmidt/9d46d1d080454b55f8cf to your computer and use it in GitHub Desktop.  
    Set Canonical URL via Javascript
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <script type='text/javascript'> | |
| var link = !!document.querySelector("link[rel='canonical']") ? document.querySelector("link[rel='canonical']") : document.createElement('link'); | |
| link.setAttribute('rel', 'canonical'); | |
| link.setAttribute('href', location.protocol + '//' + location.host + location.pathname); | |
| document.head.appendChild(link); | |
| </script> | 
@SamMakesCode  Alternative to vue-meta. I used Unhead, in VueJS 3 which works fine for the meta title, description and og tags.
@shaheeriversion out of curiousity, how did you accomplish this with unhead in VueJS 3?  I'm trying to set up a project and would love to get canonical link tags and og:url meta tags working.
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Just to add to the above statement about Vue, I believe that to do this, you need
vue-metainstalled which currently not supported for Vue 3.0 which new projects ought to be using. With that said, vue-meta looks as though it's not going to make it off the runway.