docs(app): improve framework hydration (#2780)

Co-authored-by: Sébastien Chopin <seb@nuxt.com>
This commit is contained in:
Benjamin Canac
2024-11-26 18:23:26 +01:00
committed by GitHub
parent 15ca2f5701
commit e7995e7a0b
7 changed files with 78 additions and 21 deletions

View File

@@ -0,0 +1,35 @@
export default defineNuxtPlugin({
enforce: 'post',
setup() {
const { framework } = useSharedData()
if (import.meta.client) {
useHead({
htmlAttrs: {
'data-framework': framework
}
})
}
if (import.meta.server) {
useHead({
script: [{
innerHTML: `
function getCookie(name) {
var value = '; ' + window.document.cookie;
var parts = value.split('; ' + name + '=');
if (parts.length === 2) {
return parts.pop()?.split(';').shift();
}
}
var framework = getCookie('nuxt-ui-framework');
document.documentElement.setAttribute('data-framework', framework || 'nuxt');
`.replace(/\s+/g, ' '),
type: 'text/javascript',
tagPriority: -1
}]
})
}
}
})