Data Loaders - Elevating Data Fetching in Vue

Rate this content
Bookmark
The video dives into the concept of data loaders in Vue.js, highlighting how they address the complexities of data fetching. Data loaders streamline the process by integrating with the navigation cycle, deduplicating requests, and handling loading states and errors. The speaker discusses the benefits of using data loaders over traditional methods, such as reducing repetitive code and ensuring updates only happen once all data is resolved. Examples include an application using the Art Institute of Chicago's public API and the implementation of Pina Colada for caching. The talk covers topics like file-based routing, semantic sequential fetching, and lazy loading, offering a comprehensive guide on migrating applications to use data loaders.

From Author:

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.

This talk has been presented at Vue.js Live 2024, check out the latest edition of this Tech Conference.

FAQ

Eduardo is a Vue.js core team member, freelance front-end developer, author of uRouter and Pina, and can be found in person at meetups in Paris or online as Pozva.

Eduardo discusses data loaders, emphasizing their importance beyond just data fetching and how they can solve common data fetching problems in applications.

Data loaders aim to solve the problem of data fetching, which can be complex due to handling loading states, errors, and repetitive code. They integrate with the navigation cycle to provide a more streamlined and efficient data fetching process.

Data loaders integrate with the navigation cycle, can block or abort navigation, deduplicate requests, support semantic sequential fetching, delay updates until all data is resolved, and support rollback in case of failures.

Data loaders provide a more integrated and efficient approach to data fetching by reducing repetitive code, handling loading states and errors more effectively, and ensuring that updates are delayed until all data is resolved.

Interested individuals can read the RFC (Request for Comments) document that Eduardo has provided, which covers all the technical details and edge cases of data loaders.

Eduardo uses an application that communicates with the Art Institute of Chicago's public API to show how to migrate an application to use data loaders.

Pina Colada is an experimental layer that adds caching to data loaders, allowing for more efficient data fetching by reducing the need to refetch data frequently.

Eduardo invites teams or companies interested in implementing data loaders or contributing to their development to contact him for freelance work. He is looking for opportunities to balance his open-source work and life.

More information can be found on the GitHub repository and documentation for unplugging Vue Router, and the work-in-progress details for Pina Colada.

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 Transcription

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
State management is not limited to complex applications and transitioning to a store offers significant benefits. Pinia is a centralized state management solution compatible with Vue 2 and Vue 3, providing advanced devtools support and extensibility with plugins. The core API of Pinia is similar to Vuex, but with a less verbose version of stores and powerful plugins. Pinia allows for easy state inspection, error handling, and testing. It is recommended to create one file per store for better organization and Pinia offers a more efficient performance compared to V-rex.
Welcome to Nuxt 3
Vue.js London Live 2021Vue.js London Live 2021
29 min
Welcome to Nuxt 3
Top Content
Nux3 has made significant improvements in performance, output optimization, and serverless support. Nuxt Bridge brings the Nitro engine for enhanced performance and easier transition between Nuxt 2 and Nuxt Read. Nuxt 3 supports Webpack 5, Bytes, and Vue 3. NextLab has developed brand new websites using Docus technology. Nuxt.js is recommended for building apps faster and simpler, and Nuxt 2 should be used before migrating to Nuxt 3 for stability. DOCUS is a new project that combines Nuxt with additional features like content modules and an admin panel.
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 has seen significant adoption and improvements in performance, bundle size, architecture, and TypeScript integration. The ecosystem around Vue 3 is catching up, with new tools and frameworks being developed. The Vue.js.org documentation is undergoing a complete overhaul. PNIA is emerging as the go-to state management solution for Vue 3. The options API and composition API are both viable options in Vue 3, with the choice depending on factors such as complexity and familiarity with TypeScript. Vue 3 continues to support CDN installation and is recommended for new projects.
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
In this Talk, the speaker demonstrates how to use Rust with WebAssembly in a Vue.js project. They explain that WebAssembly is a binary format that allows for high-performance code and less memory usage in the browser. The speaker shows how to build a Rust example using the WasmPack tool and integrate it into a Vue template. They also demonstrate how to call Rust code from a Vue component and deploy the resulting package to npm for easy sharing and consumption.
Vue: Feature Updates
Vue.js London 2023Vue.js London 2023
44 min
Vue: Feature Updates
Top Content
The Talk discusses the recent feature updates in Vue 3.3, focusing on script setup and TypeScript support. It covers improvements in defining props using imported types and complex types support. The introduction of generic components and reworked signatures for defined components provides more flexibility and better type support. Other features include automatic inference of runtime props, improved define emits and defined slots, and experimental features like reactive props destructure and define model. The Talk also mentions future plans for Vue, including stabilizing suspense and enhancing computer invalidations.
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
This Talk discusses handling local state in software development, particularly when dealing with asynchronous behavior and API requests. It explores the challenges of managing global state and the need for actions when handling server data. The Talk also highlights the issue of fetching data not in Vuex and the challenges of keeping data up-to-date in Vuex. It mentions alternative tools like Apollo Client and React Query for handling local state. The Talk concludes with a discussion on GitLab going public and the celebration that followed.

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.
Monitoring 101 for React Developers
React Summit US 2023React Summit US 2023
107 min
Monitoring 101 for React Developers
Top Content
WorkshopFree
Lazar Nikolov
Sarah Guthals
2 authors
If finding errors in your frontend project is like searching for a needle in a code haystack, then Sentry error monitoring can be your metal detector. Learn the basics of error monitoring with Sentry. Whether you are running a React, Angular, Vue, or just “vanilla” JavaScript, see how Sentry can help you find the who, what, when and where behind errors in your frontend project. 
Workshop level: Intermediate
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
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.
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