head icon indicating copy to clipboard operation
head copied to clipboard

de-duplicate `<link rel="canonical">`

Open danielroe opened this issue 2 years ago • 3 comments

similar to https://github.com/vueuse/head/issues/32, we can deduplicate canonical links....

danielroe avatar Apr 16 '22 13:04 danielroe

Is there any way around it? It causes quite a lot of problems ...

oritwoen avatar May 05 '22 13:05 oritwoen

Is there any way around it? It causes quite a lot of problems ...

Could you find a way?

kakajansh avatar Jul 18 '22 04:07 kakajansh

Until the issue is resolved, this is how we remove manually: In layout file

const route = useRoute();
const baseUrl = useRuntimeConfig().public.baseUrl;

// Remove duplicate 
function removeDuplicateCanonical(route) {
  nextTick(() => {
    document.head.querySelectorAll('link[rel="canonical"]').forEach((el) => {
      if (el.getAttribute('href') !== baseUrl + route.fullPath) el.remove()
    }) 
  })
}

// remove on route change
watch(route, (value) => removeDuplicateCanonical(value))

// remove on mount 
onMounted(() => removeDuplicateCanonical(route))

// add canonical
useHead({
  link: [{
      rel: 'canonical',
      href: computed(() => baseUrl + route.fullPath)
  }],
})

kakajansh avatar Jul 18 '22 09:07 kakajansh

This is resolved https://github.com/vueuse/head/commit/200f1ca1417a13a265bee45289a84ad31cf87d2b

harlan-zw avatar Sep 19 '22 03:09 harlan-zw