-
-
Notifications
You must be signed in to change notification settings - Fork 41
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
Retreive head data #455
Comments
My current workaround. export default function (title: string) {
const state = useState('state', () => '');
if (title) {
state.value = title;
useSeoMeta({
title,
});
}
return { title: state };
} So in my page I do: <script lang="ts" setup>
useTitle(t('My Page Title'));
</script> And in my AppHeader, I do: <script lang="ts" setup>
const { title } = useTitle()
</script>
<template>
<header>
...
<h1>{{ title }}</h1>
...
</header>
</template> |
The specific issue in why the head data can't just be retrieved immediately is that it's resolved async after the component tree is finished updating. If it was to give you all of the data that was rendered it could lead to recursive reactivity issues or just triggering flashes of the title. The solution you have here is good, Nuxt Scripts exports a similar function to use the title but for the context of analytic reporting. Not too sure how else to solve this at the Unhead level while it's async (running it sync will be a big performance loss and lead to reactivity issues). export function useScriptEventPage(onChange?: (payload: TrackedPage) => void) {
const nuxt = useNuxtApp()
const route = useRoute()
const head = injectHead()
const payload = ref<TrackedPage>({
path: route.fullPath,
title: import.meta.client ? document.title : '',
})
// no know to know the title on the server until the page is rendered
if (import.meta.server)
return payload
let lastPayload: TrackedPage = { path: '', title: '' }
let stopDomWatcher = () => {}
// TODO make sure useAsyncData isn't running
nuxt.hooks.hook('page:finish', () => {
Promise.race([
// possibly no head update is needed
new Promise(resolve => setTimeout(resolve, 100)),
new Promise<void>((resolve) => {
stopDomWatcher = head.hooks.hook('dom:rendered', () => resolve())
}),
])
.finally(stopDomWatcher)
.then(() => {
payload.value = {
path: route.fullPath,
title: document.title,
}
if (lastPayload.path !== payload.value.path || lastPayload.title !== payload.value.title) {
if (onChange) {
onChange(payload.value)
}
lastPayload = payload.value
}
})
})
return payload
} |
Describe the feature
Currently, if I set the title in my Pages using
useSeoMeta()
oruseHead()
.I have no way of accessing this value inside my Layouts.
To work around this, I use
definePageMeta
.But this requires me to move my translations to the global scope.
Maybe
useHead()
with no arguments should return thehead
object like so:The alternative would be to use
useState
or apinia
store, but maybe this should be baked-in.If this is implemented, there should be a way to get both the original
title
, and the title after ran intotitleTemplate
.For exemple, with this config:
Maybe:
Additional information
The text was updated successfully, but these errors were encountered: