Skip to content
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

Pinia and useFetch not working together in component #601

Open
Paalt opened this issue Jan 27, 2022 · 5 comments
Open

Pinia and useFetch not working together in component #601

Paalt opened this issue Jan 27, 2022 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@Paalt
Copy link

Paalt commented Jan 27, 2022

I am using Pinia for state management but there seem to be a compatibility issue with composition-api. Specifically the useFetch function.

The error happens when I am using useFetch in my setup function and return an imported Pinia store to the template and refreshes the page. (It works fine when navigating to the page)
This will result in:

TypeError
Converting circular structure to JSON --> starting at object with constructor 'Object' --- property '_renderProxy' closes the circle 

node_modules/@nuxtjs/composition-api/dist/runtime/index.mjs
      if (val instanceof Function || val instanceof Promise) return
      data[key] = isRef(val) ? val.value : val
    }
  )
  const content = vm.$fetchState.error
    ? { _error: vm.$fetchState.error }
    : JSON.parse(JSON.stringify(data))
  if ('push' in vm.$ssrContext.nuxt.fetch) {
    vm.$ssrContext.nuxt.fetch.push(content)
  } else {
    vm.$ssrContext.nuxt.fetch[vm._fetchKey] = content
              

Steps to reproduce the behavior:
@pinia/nuxt": "0.1.8",
"@nuxtjs/composition-api": "0.31.0",
"nuxt": "2.15.8",

In a component:

import { defineComponent, useFetch } from '@nuxtjs/composition-api';
import { useStore } from '@/stores/';

export default defineComponent({
  setup() {
    const store = useStore();
    const { fetch, fetchState } = useFetch(async () => {
     await fetch...
    }
    return {
      store,
    }
  }
})
<template>
<h1> {{ store.hello }} </h1>
</template>

Removing store from the return statement makes everything work fine again. So you can have both pinia store and useFetch in setup function as long as you dont use store in the template. But you often have the need for both.

@Paalt Paalt added the bug Something isn't working label Jan 27, 2022
@Paalt Paalt changed the title fix: Pinia and useFetch not working together in component Jan 27, 2022
@P4sca1
Copy link

P4sca1 commented Mar 12, 2022

A workaround is to not access the store in the template directly, but to use computed properties.

@P4sca1
Copy link

P4sca1 commented Mar 15, 2022

@danielroe
Copy link
Member

Yes, this is an unavoidable consequence of the Nuxt 2 fetch implementation (which entails stringifying the entire component state). I'd recommend avoiding useFetch and using useAsync instead for more granular control of what is added to the Nuxt payload...

@dygaomarques
Copy link

@Paalt I have used the onServerPrefetch hook, and it's work for me.

@ChristopherWirtOfficial

Can someone more knowledgeable explain what the downsides of using onServerPrefetch instead of useFetch? Is useFetch just capable of being either server-side or client-side, as well as adhoc instead of just server prefetch?

Basically, am I safe in assuming that if onServerPrefetch is a totally fine place for my fetch in my use case, then I'm fine using it instead of useFetch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants