Skip to content

Commit 34e8180

Browse files
feat: remove nested div from dummy element
1 parent fa44dac commit 34e8180

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/index.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
export default {
22
functional: true,
3-
render (h, { children, props: { regex, invert } }) {
3+
render (h, { children, slots, props: { regex, invert } }) {
4+
const dummyEl = h('div', {}, [])
5+
if (process.server) {
6+
return dummyEl
7+
}
8+
49
const botRegex = regex || /bot|googlebot|crawler|spider|robot|crawling/i
5-
const isBot = process.server ? true : navigator.userAgent && botRegex.test(navigator.userAgent)
10+
const isBot = navigator.userAgent && botRegex.test(navigator.userAgent)
611
const shouldShow = invert ? isBot : !isBot
712

8-
return h('div', {}, shouldShow ? children : [h('div')])
13+
if (!shouldShow) {
14+
return dummyEl
15+
}
16+
17+
return h('div', {}, slots().default)
918
}
1019
}

test/VueIsBot.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ const mockUserAgent = userAgent => {
1010

1111
const testCanSeeOrNot = (can, wrapper) => {
1212
expect(wrapper.isVueInstance()).toBe(true)
13-
expect(wrapper.html()).toBe(`<div>${can ? slotContent : '<div></div>'}</div>`)
13+
expect(wrapper.html()).toBe(`<div>${can ? slotContent : ''}</div>`)
1414
}
1515

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

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

2121
describe('VueIfBot', () => {
2222
afterEach(() => { mockUserAgent('Normal user here') })

0 commit comments

Comments
 (0)