Git Product home page Git Product logo

Comments (8)

OrlS15 avatar OrlS15 commented on July 18, 2024 4

Digging into the source code I discovered that I was right. refreshNuxtData only re-fetches the data when useAsyncData is in scope:

const off = nuxtApp.hook('app:data:refresh', async (keys) => {

When it goes out of scope, the hook is removed:

I don't think it's a bug at this point, but at the same time it's not documented anywhere. In my opinion it should work even if useAsyncData is not in scope (how can i "invalidate" the cache from another page?). πŸ€”

from nuxt.

edomod avatar edomod commented on July 18, 2024 2

Hello!

The thing with v-if is that it completely removes the component from the tree and as such vue will not update that component anymore since the component is gone. Now when the component reappears, because vue previously destroys that component, a new instance will need to be created using "initial state", where the "initial state" is the data previously fetched on the server side.

So what can u do to fix it? Well here are some suggestions:

  1. First is to utilize the v-show attribute instead as it will not remove the component from the tree and only changes the display property.
  2. Use the KeepAlive built in component provided by vue so that the component deactivate instead of unmounted.
  3. Utilize onMounted lifecycle hooks provided by vue to call refresh, although this means it will be triggered on initial render as well.

Hope this helps!

The problem is that refreshNuxtData doesn’t re-fetch the data as stated in the docs:
https://nuxt.com/docs/api/utils/refresh-nuxt-data

from nuxt.

stackblitz avatar stackblitz commented on July 18, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

from nuxt.

edomod avatar edomod commented on July 18, 2024

Oh, i have the same problem

from nuxt.

cerella17 avatar cerella17 commented on July 18, 2024

What's the solution?

from nuxt.

Amar-Gill avatar Amar-Gill commented on July 18, 2024

I've got a similar problem. Some times refreshNuxtData doesn't trigger a fetch. Confirmed by reviewing network tab in browser dev tools.

It's mysterious because:

  • I checked that the nuxt payload exists in nuxt devtools, under payload tab.
  • And I checked that I am passing in the correct key into refreshNuxtData that corresponds to the keyed state I want to refresh.

Presently thinking through the best way to share a reproduction based on my use case.

from nuxt.

Amar-Gill avatar Amar-Gill commented on July 18, 2024

Got a work around I can use for now. Which is to do a fully manual refresh for the given key:

const { refresh } = await useFetch('/some/resource', { key: 'unique-key', watch: false, immediate: false })

Then I would replace the call to refreshNuxtData('unique-key') with a call to refresh()

from nuxt.

MrMarble avatar MrMarble commented on July 18, 2024

I'm also having this problem using a wrapper around useFetch to use te cached value

export default async <T>(url: string) => {
  // Use nuxtData to cache data
  const { data: cached } = useNuxtData<T>(url);

  if (!cached.value) {
    const { data, error } = await useFetch<T>(url, { key: url });

    if (error.value) {
      throw createError({
        ...error.value,
        statusMessage: `Could not fetch data from ${url}`,
      });
    }

    // Update the cache
    cached.value = data.value as T;
  } else {
    console.log(`Getting value from cache for ${url}`);
  }

  return cached;
};

The idea is to use my wrapper and refresh the data when needed

const data = useCachedFetch<UserProfile>('/api/profile')

// somewere else after updating the profile
refreshNuxtData('/api/profile')

I can see that the network request is not made when using my wrapper but using useFetch directly it does.
The same happens if the useFetch is wrapped in callOnce

from nuxt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.