Data Loaders - Elevating Data Fetching in Vue

Rate this content
Bookmark

Data fetching is a critical part of modern web applications. It's a complex problem that has been (partially) solved in many different ways. In this talk, we'll explore the new Data Loaders API for Vue Router, how it compares to existing solutions, and how it can greatly simplify data fetching in your Vue applications.

Eduardo San Martin Morote
Eduardo San Martin Morote
30 min
25 Apr, 2024

Comments

Sign in or register to post your comment.
  • Krzysztof
    Krzysztof
    Roche
    https://github.com/posva/data-loaders-vuejs-live

Video Summary and Transcription

Data loaders provide a solution for complex and repetitive data fetching in Vue.js applications. Using data loaders allows for more independent data fetching and integrates with the navigation cycle. The data loader plug-in adds a navigation guard for data fetching and loading. Lazy loading and caching can be implemented using Pina Colada and Glada loaders. These loaders can improve the performance and speed of data fetching in applications.

1. Introduction to Data Loaders

Short description:

Hello, I'm Eduardo, a Vue.js core team member and the author of uRouter and Pina. Today, I want to talk about data loaders, which solve the problem of data fetching. We have various ways to fetch data, but it can become complex and repetitive. Data loaders offer a solution by integrating with the navigation cycle and providing a more independent data fetching experience.

Hello, I'm Eduardo, I'm a Vue.js core team member, I work as a freelance front-end developer and I'm the author of uRouter and Pina, among other libraries, and you can find me in person in Paris in some of the meetups or online as Pozva.

And today I want to talk to you about a topic that is very dear to me because it relates a lot to the router, which is data loaders. This is way more than just data fetching, actually, but I think data fetching is going to be the most common use case for this feature.

And currently, if you are interested or keen into learning all the technical details about this feature, you can already do so by reading the RFC. I even have a website up for it. But as you can see, this is pretty long. It's really going through every single technical detail that I could think of. There is really a lot, but it does cover more than documentation would even cover because it also covers some edge cases. Of course, this is a little bit boring to explain, so my goal today is to kind of make you skip the whole RFC by just showing you how to use it. Of course, I won't be able to go in all the details of the implementation of data loaders, but I will still show you what problem we are solving and how we are solving.

So the problem that we're solving is data fetching, which is hard. There are many ways of doing it, and I'm going to show you some ways of doing it. We have an initial data container that we're going to fill with the data that we want. We have some ref, and then we can call some API on mounted if we want a way that we're going to data and then we can display it. But then sometimes we also want to update based on the row, like an ID on the params. So then we move that to a watcher, we watch the params, and then we fetch again. Then what we might think about is, well, we still need to handle the loading state because initially the data is empty, so I might want to display some loading state. So we add that and then we have to toggle that loading state to true then to false. And then we think, oh, wait, but maybe there is an error, so I need to handle that as well. So then you have an error, which is again another ref, and you handle the error as well, and then you update the template.

Now, as you can imagine and probably experience, these get reposed very quickly. And because it's something so common in applications, you end up repeating these a lot. So you're usually abstracting to a composable, and you can even use already existing composables. So for example, you could use a view query from task stack, and so you have just four lines for what we're doing in 20 maybe, or 15 lines. And this is really nice because it also offers all the features like cache, at the cost of some, of course, heavier library, but it does move apart from the route layer. So it doesn't integrate into navigation anymore. And as a result, we have a more independent data fetching, which has its own benefits, but also some inconveniences. So data loaders are here to close the gap. And so the goals of the data loaders is to integrate with the navigation cycle, so it will block or not. You can choose whatever you want.

2. Data Loader Functionality

Short description:

The navigation until all the data is ready, it can change or abort the navigation. It deduplicates all requests and data access. We want to delay all the updates until all data loaders resolve. We don't want to display the new data until everything has completed. We want to roll back and avoid setting the new data if anything fails. We have two scripts to define the loader.

The navigation until all the data is ready, it can change or abort the navigation. So for example, in a navigation guard, you can return false to abort the navigation. So it will block or not. It can change or abort the navigation. So for example, in a navigation guard, you can return false to abort the navigation. Or it can change or abort the navigation. So for example, in a navigation guard, you can return false to abort the navigation. So you could also do that.

It deduplicates all requests and data access. So that means that all the requests are going to go in parallel and there will always be only one instance of one of the requests going. And then we have semantic sequential fetching on it. That means that by default, things are parallel, but if we want, we can make some sequential fetching. And also, we want to delay all the updates until all data loaders resolve.

So that means that in complex applications, we might have data that is being resolved from different IPA endpoints. And in our page, we might display that in different parts of the application. And so, some of the APIs might be faster than others, and some might resolve faster than others. And we don't want to display the new data until everything has completed, because otherwise, we're going to display an old page with the new data. And also, we want to roll back. So we want to avoid setting the new data if anything fails, because in the end, we're in a navigation. So we're staying in the old page. We want to update the data.

So to give you a glimpse of what this API looks like, we have two scripts. And I know you're going to think, why would I want to add another script? There is a reason for this. We cannot put everything in script setup, because the things you put inside script setup are inside of the setup function of the component. So they are not exposed outside the same way. They are not shared among components instances. And also, they happen when the component mounts. They are within the setup function, so you need to mount the component to access that. So the script, the regular script, gives us access to properties that are static and are not related to mounting the component. So that's where we're going to define the loader.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Everything Beyond State Management in Stores with Pinia
Vue.js London Live 2021Vue.js London Live 2021
34 min
Everything Beyond State Management in Stores with Pinia
Top Content
When we think about Vuex, Pinia, or stores in general we often think about state management and the Flux patterns but not only do stores not always follow the Flux pattern, there is so much more about stores that make them worth using! Plugins, Devtools, server-side rendering, TypeScript integrations... Let's dive into everything beyond state management with Pinia with practical examples about plugins and Devtools to get the most out of your stores.
Welcome to Nuxt 3
Vue.js London Live 2021Vue.js London Live 2021
29 min
Welcome to Nuxt 3
Top Content
Explain about NuxtJS codebase refactor and challenges facing to implement Vue 3, Vite and other packages.
One Year Into Vue 3
Vue.js London Live 2021Vue.js London Live 2021
20 min
One Year Into Vue 3
Top Content
Vue 3 may still sound new to many users, but it's actually been released for over a year already. How did Vue 3 evolve during this period? Why did it take so long for the ecosystem to catch up? What did we learn from this process? What's coming next? We will discuss these questions in this talk!
Utilising Rust from Vue with WebAssembly
Vue.js London Live 2021Vue.js London Live 2021
8 min
Utilising Rust from Vue with WebAssembly
Top Content
Rust is a new language for writing high-performance code, that can be compiled to WebAssembly, and run within the browser. In this talk you will be taken through how you can integrate Rust, within a Vue application, in a way that's painless and easy. With examples on how to interact with Rust from JavaScript, and some of the gotchas to be aware of.
Vue: Feature Updates
Vue.js London 2023Vue.js London 2023
44 min
Vue: Feature Updates
Top Content
The creator of Vue js gives an update on the new features of the technology.
Local State and Server Cache: Finding a Balance
Vue.js London Live 2021Vue.js London Live 2021
24 min
Local State and Server Cache: Finding a Balance
Top Content
How many times did you implement the same flow in your application: check, if data is already fetched from the server, if yes - render the data, if not - fetch this data and then render it? I think I've done it more than ten times myself and I've seen the question about this flow more than fifty times. Unfortunately, our go-to state management library, Vuex, doesn't provide any solution for this.For GraphQL-based application, there was an alternative to use Apollo client that provided tools for working with the cache. But what if you use REST? Luckily, now we have a Vue alternative to a react-query library that provides a nice solution for working with server cache. In this talk, I will explain the distinction between local application state and local server cache and do some live coding to show how to work with the latter.

Workshops on related topic

Vue3: Modern Frontend App Development
Vue.js London Live 2021Vue.js London Live 2021
169 min
Vue3: Modern Frontend App Development
Top Content
Featured WorkshopFree
Mikhail Kuznetcov
Mikhail Kuznetcov
The Vue3 has been released in mid-2020. Besides many improvements and optimizations, the main feature of Vue3 brings is the Composition API – a new way to write and reuse reactive code. Let's learn more about how to use Composition API efficiently.

Besides core Vue3 features we'll explain examples of how to use popular libraries with Vue3.

Table of contents:
- Introduction to Vue3
- Composition API
- Core libraries
- Vue3 ecosystem

Prerequisites:
IDE of choice (Inellij or VSC) installed
Nodejs + NPM
Using Nitro – Building an App with the Latest Nuxt Rendering Engine
Vue.js London Live 2021Vue.js London Live 2021
117 min
Using Nitro – Building an App with the Latest Nuxt Rendering Engine
Top Content
Workshop
Daniel Roe
Daniel Roe
We'll build a Nuxt project together from scratch using Nitro, the new Nuxt rendering engine, and Nuxt Bridge. We'll explore some of the ways that you can use and deploy Nitro, whilst building a application together with some of the real-world constraints you'd face when deploying an app for your enterprise. Along the way, fire your questions at me and I'll do my best to answer them.
Building GraphQL APIs on top of Ethereum with The Graph
GraphQL Galaxy 2021GraphQL Galaxy 2021
48 min
Building GraphQL APIs on top of Ethereum with The Graph
WorkshopFree
Nader Dabit
Nader Dabit
The Graph is an indexing protocol for querying networks like Ethereum, IPFS, and other blockchains. Anyone can build and publish open APIs, called subgraphs, making data easily accessible.

In this workshop you’ll learn how to build a subgraph that indexes NFT blockchain data from the Foundation smart contract. We’ll deploy the API, and learn how to perform queries to retrieve data using various types of data access patterns, implementing filters and sorting.

By the end of the workshop, you should understand how to build and deploy performant APIs to The Graph to index data from any smart contract deployed to Ethereum.
Hands-on with AG Grid's React Data Grid
React Summit 2022React Summit 2022
147 min
Hands-on with AG Grid's React Data Grid
WorkshopFree
Sean Landsman
Sean Landsman
Get started with AG Grid React Data Grid with a hands-on tutorial from the core team that will take you through the steps of creating your first grid, including how to configure the grid with simple properties and custom components. AG Grid community edition is completely free to use in commercial applications, so you'll learn a powerful tool that you can immediately add to your projects. You'll also discover how to load data into the grid and different ways to add custom rendering to the grid. By the end of the workshop, you will have created an AG Grid React Data Grid and customized with functional React components.- Getting started and installing AG Grid- Configuring sorting, filtering, pagination- Loading data into the grid- The grid API- Using hooks and functional components with AG Grid- Capabilities of the free community edition of AG Grid- Customizing the grid with React Components
TresJS create 3D experiences declaratively with Vue Components
Vue.js London 2023Vue.js London 2023
137 min
TresJS create 3D experiences declaratively with Vue Components
Workshop
Alvaro Saburido
Alvaro Saburido
- Intro 3D - Intro WebGL- ThreeJS- Why TresJS- Installation or Stackblitz setup - Core Basics- Setting up the Canvas- Scene- Camera- Adding an object- Geometries- Arguments- Props- Slots- The Loop- UseRenderLoop composable- Before and After rendering callbacks- Basic Animations- Materials- Basic Material- Normal Material- Toon Material- Lambert Material- Standard and Physical Material- Metalness, roughness - Lights- AmbientLight- DirectionalLight- PointLights- Shadows- Textures- Loading textures with useTextures- Tips and tricks- Misc- Orbit Controls- Loading models with Cientos- Debugging your scene- Performance
Building Vue forms with VeeValidate
Vue.js London Live 2021Vue.js London Live 2021
176 min
Building Vue forms with VeeValidate
Workshop
Abdelrahman Awad
Abdelrahman Awad
In this workshop, you will learn how to use vee-validate to handle form validation, manage form values and handle submissions effectively. We will start from the basics with a simple login form all the way to using the composition API and building repeatable and multistep forms.

Table of contents:
- Introduction to vee-validate
- Building a basic form with vee-validate components
- Handling validation and form submissions
- Building validatable input components with the composition API
- Field Arrays and repeatable inputs
- Building a multistep form
Prerequisites:
VSCode setup and an empty Vite + Vue project.