简体   繁体   English

Vue.js 预取数据与客户端渲染

[英]Vue.js prefetch data with client side rendering

I know about ssr (server side rendering) in Vue, such as nuxt.我知道Vue中的ssr(服务端渲染),比如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 .它在serverPrefetch()函数中serverPrefetch()数据并在服务器端呈现内容,然后请求将数据返回给用户并且他开始下载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.但是我们是否可以在用户请求后立即从后端开始加载数据,而不是等待 vue 脚本的下载,并且在所有数据加载之前不停止请求。 So user is downloading app.js , while our server is doing work with sql requests and forming response.所以用户正在下载app.js ,而我们的服务器正在处理 sql 请求并形成响应。

用户请求模式

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 .Nuxt而言 - 您可以在以下文章中找到有关SSR (和客户端)选项的非常好的总结。 Spoiler alert - I think SSR is still the best shot with what you are trying to achieve.剧透警报 - 我认为 SSR 仍然是您想要实现的最佳目标。 In the Nuxt world - NuxtServerInit and AsyncData are the men for the job.Nuxt世界中 - NuxtServerInitAsyncData是这项工作的AsyncData

Say you decided to stay away from SSR - what options do you have?假设你决定远离 SSR - 你有什么选择?

  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 (eg - saving it to the local storage).在 Vue 应用程序之前加载并运行一些超轻量级 js,该应用程序将获取数据并以某种方式与应用程序共享(例如 - 将其保存到本地存储)。 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.我真的很怀疑,尤其是考虑到 Vue 应用程序在客户端浏览器中缓存时的加载速度。

  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.我的意思是,您可以预取所有繁重的内容并将其作为 json 编码对象粘贴到您的页面中。 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.这些是您应该根据您的特定用例回答的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM