Skip to content

Instantly share code, notes, and snippets.

@furkan3ayraktar
Created March 29, 2018 16:23
Show Gist options
  • Save furkan3ayraktar/a5d67453d3680e9845269978ea89840e to your computer and use it in GitHub Desktop.
Save furkan3ayraktar/a5d67453d3680e9845269978ea89840e to your computer and use it in GitHub Desktop.
Function for fetching metadata from backend.
const fetchMetaData = (url, callback) => {
downloadContent(url, (isOk, result, headers) => {
if (!isOk) {
console.log('Error fetching meta data:', result);
callback(false);
} else {
const metaData = JSON.parse(result);
let metaTags = '';
if (metaData) {
if (metaData.title) {
metaTags += '<title>' + metaData.title + '</title>';
metaTags += '<meta property=\"og:title\" content=\"' + metaData.title + '\" />';
}
if (metaData.description) {
metaTags += '<meta name=\"description\" content=\"' + metaData.description + '\" />';
metaTags += '<meta property=\"og:description\" content=\"' + metaData.description + '\" />';
}
if (metaData.images) {
for (let i = 0; i < metaData.images.length; i++) {
const image = metaData.images[i];
metaTags += '<meta property=\"og:image\" content=\"' + image + '\" />';
}
}
}
metaTags += '<meta property=\"og:url\" content=\"https://' + domainName + originalUri + '\" />';
metaTags += '<meta property=\"og:type\" content=\"website\" />';
callback(true, metaTags, headers);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment