question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feat: automatically track changes to `head` within `useFetch`

See original GitHub issue

🐛 The bug When you edit the meta in the useFetch, once the page is loaded the metas are erased. Note: all the metas are in place when you look at the code generated from the SSR

🛠️ To reproduce Here is the sandbox https://codesandbox.io/s/cool-glitter-ykhpv

You can have a look in the pages/posts/_id.vue to see how the metas are set. I believe we can write the code like that, but please let me know if I made a mistake. Note: the title is initialized in the nuxt.config.js

When you load a page from a post (better to test in a new window and not in the sandbox UI), you can first see that the title is right, but when the page is loaded the title goes back to Test. If you look at the source generated from the SSR everything is in place, but if you inspect the code once the page is loaded you can see that the meta twitter:title is missing for example. Also, if you go from the homepage to a post (so without reloading the entire website), you can see that the title is right.

🌈 Expected behaviour The metas are kept once the page is loaded

Thank you

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
thinhnguyen1105commented, Jul 27, 2021

I tried to run code with your suggestion but still can’t get data from API and set it to meta. it doesn’t seem to run sequentially. Please help

1reaction
danielroecommented, Feb 23, 2021

@nicolashmln This is not a bug; just make sure you don’t set the metadata within useFetch. However, I agree it could be nice if that worked as you have it and I’ll consider adding that as a feature. For now, I would avoid setting your meta within useFetch and instead do something more like this:

setup() {
    const post = ref()

    const { $http, params } = useContext()

    const { title, meta } = useMeta()

    useFetch(async () => {
      post.value = await $http.$get(
        `https://jsonplaceholder.typicode.com/posts/${params.value.id}`
      )
    })

    title.value = post.value.title
    meta.value.push({
      hid: 'ogTitle',
      name: 'og:title',
      content: post.value.title,
    })
    meta.value.push({
      hid: 'twitterTitle',
      name: 'twitter:title',
      content: post.value.title,
    })

    useMeta({
      bodyAttrs: {
        itemtype: `http://schema.org/WebPage`,
      },
      meta: [{ hid: 'description', name: 'description', content: 'page desc' }],
    })
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

fix: useFetch and useMeta together not working #530 - GitHub
The bug When using useMeta hook inside the useFetch, the title value ... feat: automatically track changes to head within useFetch #383.
Read more >
Track changes in Word - Microsoft Support
How to track changes in Word using revision marks. Accept, reject, or hide changes made by others working in the file.
Read more >
phone numbers that play music
Navigate to the artist, album, or playlist: Tap the artist name below the song title, then choose to go to the artist, album,...
Read more >
Track Changes in Word - how it works
PROBLEM 9 – When I open a document, Word automatically shows tracked changes – why? PROBLEM 10 – How can I determine how...
Read more >
Preserving Tracked Changes in PDF or Word Output
If you enable review mode in the output, Word will automatically track any additional changes you make in the output. PDF.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found