Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure empty attrs reset attrs #130

Merged
merged 2 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/nuxt3/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@ useHead({
<nuxt-link to="/second">
second page
</nuxt-link>
<nuxt-link to="/red">
red
</nuxt-link>
</div>
</template>
17 changes: 17 additions & 0 deletions examples/nuxt3/pages/red.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script setup lang="ts">
useHead({
htmlAttrs: {
style: 'background: red',
},
title: 'red',
})
</script>

<template>
<div>
<h2>red bg</h2>
<NuxtLink to="/">
back
</NuxtLink>
</div>
</template>
15 changes: 10 additions & 5 deletions src/dom/update-dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,21 @@ export const updateDOM = async (head: HeadClient, previousTags: Set<string>, doc
if (typeof tag._runtime.textContent !== 'undefined')
document.title = tag._runtime.textContent
break
case 'htmlAttrs':
case 'bodyAttrs':
setAttrs(document[tag.tag === 'htmlAttrs' ? 'documentElement' : 'body'], tag.props)
break
default:

case 'base':
case 'meta':
case 'link':
case 'style':
case 'script':
case 'noscript':
tags[tag.tag] = tags[tag.tag] || []
tags[tag.tag].push(tag)
break
}
}

setAttrs(document.documentElement, headTags.find(t => t.tag === 'htmlAttrs')?.props || {})
setAttrs(document.body, headTags.find(t => t.tag === 'bodyAttrs')?.props || {})
const tagKeys = new Set([...Object.keys(tags), ...previousTags])
for (const tag of tagKeys)
updateElements(document, tag, tags[tag] || [])
Expand Down