Skip to content

Commit

Permalink
feat: remove nested div from dummy element
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlexLichter committed Jul 22, 2018
1 parent fa44dac commit 34e8180
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
export default {
functional: true,
render (h, { children, props: { regex, invert } }) {
render (h, { children, slots, props: { regex, invert } }) {
const dummyEl = h('div', {}, [])
if (process.server) {
return dummyEl
}

const botRegex = regex || /bot|googlebot|crawler|spider|robot|crawling/i
const isBot = process.server ? true : navigator.userAgent && botRegex.test(navigator.userAgent)
const isBot = navigator.userAgent && botRegex.test(navigator.userAgent)
const shouldShow = invert ? isBot : !isBot

return h('div', {}, shouldShow ? children : [h('div')])
if (!shouldShow) {
return dummyEl
}

return h('div', {}, slots().default)
}
}
4 changes: 2 additions & 2 deletions test/VueIsBot.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ const mockUserAgent = userAgent => {

const testCanSeeOrNot = (can, wrapper) => {
expect(wrapper.isVueInstance()).toBe(true)
expect(wrapper.html()).toBe(`<div>${can ? slotContent : '<div></div>'}</div>`)
expect(wrapper.html()).toBe(`<div>${can ? slotContent : ''}</div>`)
}

const testCanSee = curry(w => testCanSeeOrNot(true, w))
const testCanNotSee = curry(w => testCanSeeOrNot(false, w))

const testSsr = html => { expect(html).toBe(`<div data-server-rendered="true"><div></div></div>`) }
const testSsr = html => { expect(html).toBe(`<div data-server-rendered="true"></div>`) }

describe('VueIfBot', () => {
afterEach(() => { mockUserAgent('Normal user here') })
Expand Down

0 comments on commit 34e8180

Please sign in to comment.