Skip to content

Commit

Permalink
Fixing bug in headtags with children rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
stolksdorf committed Jun 26, 2020
1 parent 60d4704 commit e721b16
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
16 changes: 9 additions & 7 deletions headtags.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const React = require('react');

const obj2props = (obj)=>Object.entries(obj).map(([k,v])=>`${k}="${v}"`).join(' ')
const obj2props = (obj)=>Object.entries(obj).map(([k,v])=>`${k}="${v}"`).join(' ');
const toStr = (chld)=>Array.isArray(chld) ? chld.join('') : chld;
const onServer = (typeof window === 'undefined');

const injectTag = require('./utils/injectTag.js');

let NamedTags = {};
let UnnamedTags = [];


const HeadComponents = {
Title({ children }){
if(onServer) NamedTags.title = `<title>${children.join('')}</title>`;
React.useEffect(()=>{document.title = children.join('')}, [children]);
if(onServer) NamedTags.title = `<title>${toStr(children)}</title>`;
React.useEffect(()=>{document.title = toStr(children)}, [children]);
return null;
},
Favicon({ type = 'image/png', href = '', rel='icon', id= 'favicon'}){
Expand All @@ -21,18 +23,18 @@ const HeadComponents = {
},

Description({ children }){
if(onServer) NamedTags.description = `<meta name='description' content='${children.join('')}' />`
if(onServer) NamedTags.description = `<meta name='description' content='${toStr(children)}' />`
return null;
},

Noscript({ children }){
if(onServer) UnnamedTags.push(`<noscript>${children.join('')}<\/noscript>`);
if(onServer) UnnamedTags.push(`<noscript>${toStr(children)}<\/noscript>`);
return null;
},
Script({ children=[], ...props }){
if(onServer) {
UnnamedTags.push(children.length
? `<script ${obj2props(props)}>${children.join('')}\<\/script>`
? `<script ${obj2props(props)}>${toStr(children)}\<\/script>`
: `<script ${obj2props(props)} />`
);
}
Expand All @@ -48,7 +50,7 @@ const HeadComponents = {
return null;
},
Style({ children, type='text/css' }){
if(onServer) UnnamedTags.push(`<style type="${type}">${children.join('')}</style>`);
if(onServer) UnnamedTags.push(`<style type="${type}">${toStr(children)}</style>`);
return null;
}
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vitreum",
"version": "6.0.1",
"version": "6.0.2",
"description": "Web app build system using Browserify and React",
"main": "lib/index.js",
"repository": {
Expand Down

0 comments on commit e721b16

Please sign in to comment.