-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
feat(vue-app): new fetch #5118
feat(vue-app): new fetch #5118
Conversation
Just so I understand correctly does this resolve #32? |
@jagreehal Yes. |
@atinux These changes in fetch seem great, I always missed the ability to put fetch of stuff in components not just pages.
UPDATE: I just managed to find more details about one of your examples and seems like fetch does indeed react to params change (with the post id) - which is great news! So just to double check - is this functionality also new? Or can this be achieved in Nuxt 2 as well ? |
Actually, when a component is re-used and a route params change, Vue will call Also, for sub components, you can directly |
@hartmut-co-uk there is this example of @atinux I like @hartmut-co-uk's idea of waiting for the "route specific" API response. Maybe we could await |
Thank you for your feedbacks @hartmut-co-uk and @henriqemalheiros I just created an RFC for it so we can discuss about this situation: nuxt/rfcs#27 |
I'm just starting to learn Nuxt so I have feedback from that perspective. I'm stoked on Vue for bringing more simplicity and fun to front end development. I was stoked on Nuxt for similar reasons, until I got to Vuex which is when it became less fun. I think React developers are figuring out Redux was an important step in the development of state management, but want less boilerplate and are moving to alternatives like MobX-State-Tree. So, I would hate to see Nuxt continue to follow what has turned out to be not such a developer friendly solution to state management in Redux/Vuex if there are better alternatives. See https://hackernoon.com/the-react-state-museum-a278c726315 for a rundown. To me the most exciting is https://github.com/jamiebuilds/unstated, I wonder if it could help inspire a way to simplify state management and fetching data with Nuxt, especially universal which gets confusing. |
Thank you for your feedback @tsongas Simplicity is part of Nuxt vision, this is why we have the Actually, |
@tsongas it might be slightly bumpy to get to understand vuex at first, but it is not even closely as complex as Redux. In fact, with Vuex they managed to simplify what normally is a very complex concept - State management. Nuxt takes it to next level by providing it hassle and setup free. What I could suggest is to use it with vuex-class (via nuxt-property-decorator or directly). Rather than moving away from Vuex, which has become a standard in Vue community, we should seek to make it richer and simpler to use. I am really looking forward to Vuex 4 and wish it could be prioritised, as it covers many of your concerns: Here is some of the coming features from Vue.js roadmap Vuex 4.x
|
This would allow keep-alive to not wait for the asyncData but loading it 'on-the-fly' - am I right? So getting back to the page before won't trigger fetch? |
Thanks for your contribution to Nuxt.js!
Issues that are labeled as |
Thanks for your contribution to Nuxt.js!
Issues that are labeled as |
I am closing this PR in favour of #6880 since it does not remove the actual behaviour 😄 |
RFC
Please carefully read our RFC for it: nuxt/rfcs#27
Description
This PR will allow to use
fetch
in every Vue components with access tothis
inside the method. I've been waiting since the creation of Nuxt.js to have this feature, and I am really happy that we worked on a workaround with the core team (thanks Evan & Guillaume Chau) and now shipped with Vue 2.6:serverPrefetch
.serverPrefetch
is great for the SSR part. The complicated part is when navigating on client-side since Nuxt 1 & 2 wait for theasyncData/fetch
calls to be done before rendering the next route (by usingrouter.beforeEach
).It introduces at the component level:
fetch
hook in the page/component to be used while asyncData is called, it should be astring
for a global component or directly a component objectthis.$isFetching
in the component to display a placeholder whilefetch
is being called (on client-side or whenfetchOnServer
isfalse
when rendering the component on the server).fetchOnServer
(default totrue
) to call or notfetch
hook when server rendering the componentthis.$fetch()
in the component to re-callfetch
hook ($isFetching
will becometrue
again)this._lastFetchAt
(exists iffetch
hook is present): timestamp of the last fetch call made (ssr included), useful for caching combined withkeep-alive
on<nuxt>
and<nuxt-child>
componentsthis._hydrated
to know if this component has been hydrated andfetch
has not been called on client-side.It also introduces at the app level:
this.$nuxt.state
(before:nuxtState
given inbeforeNuxtRender
) that you can overwrite and will be rendered aswindow.__NUXT__
. The value is also setup on client side to hydrate your plugins.this.$nuxt.isFetching
to know if any component is actually callingfetch
(useful for a global loading page)this.$nuxt.nbFetchin
to know how manyfetch
are pendingIt removes:
asyncData
: Since you can mutate the components data right insidefetch
, you don't need to haveasyncData
anymore. Also, Nuxt smartly detects what properties has been mutated in order to returned the minimum JSON with the HTML.What about waiting
fetch
calls before navigating to the next page?I found a way to wait before rendering the next route (like actual Nuxt UX), but it was too tricky:
Using
<transition>
listenersbeforeEnter + enter
andleave
to wait before transitioning the component while initializing the virtual DOM and calling fetch.->
keep-alive
and page transitions were broken (since I was hacking ).I also tried to overwrite the
render
method of the component by giving a component to aplaceholder
property. I also worked on a<placeholder>
block next to the<template>
block (I worked with @znck on it).-> It looked more messy that simply deal with
$isFetching
and it did not fix the way to "wait" for the components to be fetched before showing the next route.Some people may say that it's the same UX of React Suspense, I did not know, I had this feature in mind since a long time (I was calling it "Reactive Loading" 😄).
Resolves #3776, #32 and #127
TODO:
$emit('hook:fetch')
, see https://vuedose.tips/tips/10/preFetch(ctx)
to be able to add some cache inthis.$options
?Checklist: