export const updateCanonicalLink = ( doc , href ) => { let canonicalLinkElm = doc . head . querySelector ( 'link[rel="canonical"]' ) ; if ( typeof href === 'string' ) { if ( canonicalLinkElm == null ) { canonicalLinkElm = doc . createElement ( 'link' ) ; canonicalLinkElm . setAttribute ( 'rel' , 'canonical' ) ; doc . head . appendChild ( canonicalLinkElm ) ; } canonicalLinkElm . setAttribute ( 'href' , href ) ; } else { if ( canonicalLinkElm != null ) { const existingHref = canonicalLinkElm . getAttribute ( 'href' ) ; if ( ! existingHref ) { canonicalLinkElm . parentNode . removeChild ( canonicalLinkElm ) ; } } } } ;