Skip to content
Advertisement

Vue.js prefetch data with client side rendering

I know about ssr (server side rendering) in Vue, such as nuxt. It grabs data in serverPrefetch() function and renders content on server side, only after then the request is returning data to user and he is starting to download app.js.

But can we start loading data from backend immediatelly after user request, not waiting for download of vue script, and not stalling request before all data is loaded. So user is downloading app.js, while our server is doing work with sql requests and forming response.

Schema of user request

Advertisement

Answer

As long as Nuxt is concerned – you can find a pretty good summary on the SSR (and client-side) options available in the following article. Spoiler alert – I think SSR is still the best shot with what you are trying to achieve. In the Nuxt world – NuxtServerInit and AsyncData are the men for the job.

Say you decided to stay away from SSR – what options do you have?

  1. Have some super lightweight js loaded and ran before the Vue application that would fetch the data and share it with the app somehow (e.g. – saving it to the local storage). Would it really provide a speed advantage? I really doubt it, especially considering how fast the Vue app could load when cached in the client browser.

  2. Dump the backend data into the server response itself. I mean, you could prefetch all the heavy stuff and stick it into your page as a json encoded object. That would save some time for initial requests for sure, but then – how large is that data chunk? Wouldn’t it make the initial load too heavy, destroying the initial purpose? Those are the questions you should answer based on your particular use case.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement