From d033b8ed124ada654be8ec2cfc230842b683bb9e Mon Sep 17 00:00:00 2001 From: Declan de Wet Date: Wed, 2 Nov 2016 06:36:25 +0200 Subject: [PATCH] reuse same html tag reference --- src/updateClientMetaInfo.js | 8 +++++--- src/updateHtmlTagAttributes.js | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/updateClientMetaInfo.js b/src/updateClientMetaInfo.js index c57c510..3c51384 100644 --- a/src/updateClientMetaInfo.js +++ b/src/updateClientMetaInfo.js @@ -2,14 +2,16 @@ import updateTitleTag from './updateTitleTag' import updateHtmlTagAttributes from './updateHtmlTagAttributes' import { SERVER_RENDERED_ATTRIBUTE } from './constants' +if (typeof window !== 'undefined' && window !== null) { + var htmlTag = document.getElementsByTagName('html')[0] +} + /** * Performs client-side updates when new meta info is received * * @param {Object} newInfo - the meta info to update to */ export default function updateClientMetaInfo (newInfo) { - const htmlTag = document.getElementsByTagName('html')[0] - // if this is not a server render, then update if (htmlTag.getAttribute(SERVER_RENDERED_ATTRIBUTE) === null) { if (newInfo.title) { @@ -17,7 +19,7 @@ export default function updateClientMetaInfo (newInfo) { } if (newInfo.htmlAttrs) { - updateHtmlTagAttributes(newInfo.htmlAttrs) + updateHtmlTagAttributes(newInfo.htmlAttrs, htmlTag) } } else { htmlTag.removeAttribute(SERVER_RENDERED_ATTRIBUTE) diff --git a/src/updateHtmlTagAttributes.js b/src/updateHtmlTagAttributes.js index bce573a..d5993be 100644 --- a/src/updateHtmlTagAttributes.js +++ b/src/updateHtmlTagAttributes.js @@ -4,9 +4,9 @@ import { VUE_META_ATTRIBUTE } from './constants' * updates the document's html tag attributes * * @param {Object} attrs - the new document html attributes + * @param {HTMLElement} [tag=] - the tag */ -export default function updateHtmlTagAttributes (attrs) { - const tag = document.getElementsByTagName('html')[0] +export default function updateHtmlTagAttributes (attrs, tag = document.getElementsByTagName('html')[0]) { const vueMetaAttrString = tag.getAttribute(VUE_META_ATTRIBUTE) const vueMetaAttrs = vueMetaAttrString ? vueMetaAttrString.split(',') : [] const toRemove = [].concat(vueMetaAttrs)