Skip to content

Commit

Permalink
perf: avoid using dataset entirely
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Feb 23, 2022
1 parent 2191dda commit d75b83f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
18 changes: 7 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,18 @@ export const BindOnceDirective: Directive = {
created(el, binding) {
for (const key in binding.value) {
const k = kebabCase(key)
el.setAttribute(k, el.dataset[k] || binding.value[key])
el.setAttribute(k, el.getAttribute(k) || binding.value[key])
}
},
getSSRProps(binding) {
if (!binding.value) {
return {}
}
/* c8 ignore next */
if (!binding.value) return {}

return Object.fromEntries(
Object.entries(binding.value).flatMap(([key, value]) => {
const k = kebabCase(key)
return [
[k, value],
[`data-${k}`, value],
]
})
Object.entries(binding.value).map(([key, value]) => [
kebabCase(key),
value,
])
)
},
}
Expand Down
4 changes: 2 additions & 2 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ describe('directive', () => {
app.directive('bind-once', BindOnceDirective)
const result = await renderToString(app)
expect(result).toBe(
`<div id="test-value" data-id="test-value" num="42" data-num="42" bool="true" data-bool="true" camel-case="camel" data-camel-case="camel"></div>`
`<div id="test-value" num="42" bool="true" camel-case="camel"></div>`
)
})

it('hydrates server-rendered data properly', async () => {
const body = `<body id="app"><div id="test-value" data-id="test-value" num="42" data-num="42" bool="true" data-bool="true" camel-case="camel" data-camel-case="camel"></div></body>`
const body = `<body id="app"><div id="test-value" num="42" bool="true" camel-case="camel"></div></body>`
const html = `<html>${body}</html>`
document.write(html)
const app = createSSRApp({
Expand Down

0 comments on commit d75b83f

Please sign in to comment.