Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(meta_tags): add special case for dealing with json-ld scripts #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/meta_tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ function (_Component) {
} else if (tag === 'link' && child.rel === 'canonical') {
var link = (0, _utils.getDuplicateCanonical)(child);
if (link) (0, _utils.removeChild)(head, link);
} else if (tag === 'script' && child.type === 'application/ld+json') {
var script = (0, _utils.getDuplicateJsonLd)(child);
if (script) (0, _utils.removeChild)(head, script);
}
});
(0, _utils.appendChild)(document.head, childNodes);
Expand Down
7 changes: 6 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
exports.filterAndArrangeTags = filterAndArrangeTags;
exports.getDuplicateTitle = getDuplicateTitle;
exports.getDuplicateCanonical = getDuplicateCanonical;
exports.getDuplicateJsonLd = getDuplicateJsonLd;
exports.getDuplicateMeta = getDuplicateMeta;
exports.appendChild = appendChild;
exports.removeChild = removeChild;
Expand Down Expand Up @@ -83,7 +84,7 @@ function removeDuplicateMetas(metas) {
}

if (addMeta) {
filteredMetas.unshift(meta); //add meta as added
filteredMetas.unshift(meta); //add meta as added

uniqueIdentifiersAll.forEach(function (identifier) {
var identifierValue = meta.props[identifier];
Expand All @@ -107,6 +108,10 @@ function getDuplicateCanonical() {
return document.head.querySelectorAll('link[rel="canonical"]');
}

function getDuplicateJsonLd() {
return document.head.querySelectorAll('script[type="application/ld+json"]');
}

function getDuplicateMeta(meta) {
var head = document.head;
var id = meta.id; //if has id and element with id is not present than return the element
Expand Down
5 changes: 4 additions & 1 deletion src/meta_tags.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import {getDuplicateTitle, getDuplicateCanonical, getDuplicateMeta, appendChild, removeChild} from './utils';
import {getDuplicateTitle, getDuplicateCanonical, getDuplicateMeta, getDuplicateJsonLd, appendChild, removeChild} from './utils';


/** An wrapper component to wrap element which need to shifted to head **/
Expand Down Expand Up @@ -85,6 +85,9 @@ class MetaTags extends Component {
} else if (tag === 'link' && child.rel === 'canonical') {
const link = getDuplicateCanonical(child);
if (link) removeChild(head, link);
} else if (tag === 'script' && child.type === 'application/ld+json') {
const script = getDuplicateJsonLd(child);
if (script) removeChild(head, script);
}
});

Expand Down
18 changes: 11 additions & 7 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const camelCaseProps = ['itemProp'];
const uniqueIdentifiersI = ['property', 'name', 'itemprop'];
const camelCaseProps = ['itemProp'];
const uniqueIdentifiersI = ['property', 'name', 'itemprop'];
const uniqueIdentifiers = uniqueIdentifiersI.concat(camelCaseProps); //case sensitive props is defined in case anyone defined the lowercase prop
const uniqueIdentifiersAll = uniqueIdentifiers.concat(['id']);

Expand Down Expand Up @@ -39,7 +39,7 @@ export function filterAndArrangeTags(headElms) {

function removeDuplicateMetas(metas) {
const addedMeta = {};

//initialize all the identifiers with empty array
uniqueIdentifiersAll.forEach((identifier) => {
addedMeta[identifier] = [];
Expand Down Expand Up @@ -68,10 +68,10 @@ function removeDuplicateMetas(metas) {
if (addMeta) {
filteredMetas.unshift(meta);

//add meta as added
//add meta as added
uniqueIdentifiersAll.forEach((identifier) => {
const identifierValue = meta.props[identifier];
if (identifierValue) addedMeta[identifier][identifierValue] = meta;
if (identifierValue) addedMeta[identifier][identifierValue] = meta;
});
}
}
Expand All @@ -87,19 +87,23 @@ export function getDuplicateCanonical() {
return document.head.querySelectorAll('link[rel="canonical"]');
}

export function getDuplicateJsonLd() {
return document.head.querySelectorAll('script[type="application/ld+json"]');
}

export function getDuplicateMeta(meta) {
const head = document.head;
const { id } = meta;

//if has id and element with id is not present than return the element
if (id) {
return id && head.querySelector(`#${id}`);
}
}

//for any other unique identifier check if metas already available with same identifier which doesn't have id
return uniqueIdentifiersI.reduce((duplicates, identifier) => {
const identifierValue = meta.getAttribute(identifier);
return (identifierValue ?
return (identifierValue ?
duplicates.concat(filterOutMetaWithId(head.querySelectorAll(`[${identifier} = "${identifierValue}"]`))) :
duplicates);
}, []);
Expand Down