11import { isUndefined } from '../../utils/is-type'
2+ import { toArray , includes } from '../../utils/array'
23
34/**
45 * Updates meta tags inside <head> and <body> on the client. Borrowed from `react-helmet`:
@@ -9,8 +10,9 @@ import { isUndefined } from '../../utils/is-type'
910 * @return {Object } - a representation of what tags changed
1011 */
1112export default function updateTag ( { attribute, tagIDKeyName } = { } , type , tags , headTag , bodyTag ) {
12- const oldHeadTags = Array . from ( headTag . querySelectorAll ( `${ type } [${ attribute } ]` ) )
13- const oldBodyTags = Array . from ( bodyTag . querySelectorAll ( `${ type } [${ attribute } ][data-body="true"]` ) )
13+ const oldHeadTags = toArray ( headTag . querySelectorAll ( `${ type } [${ attribute } ]` ) )
14+ const oldBodyTags = toArray ( bodyTag . querySelectorAll ( `${ type } [${ attribute } ][data-body="true"]` ) )
15+ const dataAttributes = [ tagIDKeyName , 'body' ]
1416 const newTags = [ ]
1517
1618 if ( tags . length > 1 ) {
@@ -20,7 +22,7 @@ export default function updateTag({ attribute, tagIDKeyName } = {}, type, tags,
2022 const found = [ ]
2123 tags = tags . filter ( ( x ) => {
2224 const k = JSON . stringify ( x )
23- const res = ! found . includes ( k )
25+ const res = ! includes ( found , k )
2426 found . push ( k )
2527 return res
2628 } )
@@ -44,13 +46,12 @@ export default function updateTag({ attribute, tagIDKeyName } = {}, type, tags,
4446 } else {
4547 newElement . appendChild ( document . createTextNode ( tag . cssText ) )
4648 }
47- } else if ( [ tagIDKeyName , 'body' ] . includes ( attr ) ) {
48- const _attr = `data-${ attr } `
49- const value = isUndefined ( tag [ attr ] ) ? '' : tag [ attr ]
50- newElement . setAttribute ( _attr , value )
5149 } else {
50+ const _attr = includes ( dataAttributes , attr )
51+ ? `data-${ attr } `
52+ : attr
5253 const value = isUndefined ( tag [ attr ] ) ? '' : tag [ attr ]
53- newElement . setAttribute ( attr , value )
54+ newElement . setAttribute ( _attr , value )
5455 }
5556 }
5657 }
0 commit comments