In this episode, we dive into making your page <head>
elements dynamic in Nuxt 3. So far, we've just used static values in our titles and meta tags, which works well for fixed pages like a homepage. But what about when your content comes from somewhere dynamic, like an API?
We head over to a Todo details page and look at how to set the page title based on data fetched from an API. First, we use useMeta
to set a static title, just to confirm it works. Then, we swap this out to use the title from our API-fetched Todo. Since we're using Vue 3's Composition API, we remember that reactive data like todo
is a ref
, so we use todo.value.title
to get the actual value.
Next, we try to update the title reactively—maybe after a user updates the Todo title. We simulate this by changing the title via a timeout, but realize that our page title isn't updating as expected. To fix it, we use a Vue 3 computed
property for the title, making sure the document title updates any time the Todo title changes. This way, any changes—like editing a form—are automatically reflected in your page's <head>
.
By the end, you'll understand how to keep your <head>
content in sync with your page data, so your dynamic app always stays up-to-date in the browser tab and in SEO metadata.