Turning It up to Eleven

Rate this content
Bookmark

More often than not, performance starts with how data is loaded in your app. Which data has dependencies? Which data is critical to the page? Which data is variable? Static? Personalized? Available offline? In this talk, we'll use Remix to explore how different data loading strategies can improve your user’s experience.

This talk has been presented at React Advanced 2024, check out the latest edition of this React Conference.

FAQ

To optimize data fetching in Remix, use 'promise.all' to parallelize requests, identify critical data for immediate loading, and utilize caching strategies for static data.

A 'waterfall' in data loading refers to sequential requests where each request waits for the previous one to complete, making the process slower than necessary.

Hydrogen is a framework on top of Remix that adds eCommerce-specific features to help quickly build an eCommerce site on Remix.

'Turning It Up to 11' is a metaphor for pushing performance and optimization in web development to the maximum, inspired by a humorous moment from an old movie.

An app is usually slow due to inefficient data loading rather than the framework itself. To improve performance, parallelize data fetching and consider using caching techniques.

A service worker in a PWA (Progressive Web App) helps manage offline capabilities and caching, allowing the app to function without an internet connection.

Caching improves performance by storing static data that doesn't change frequently, reducing the need to fetch it repeatedly, thus speeding up page load times.

Client-side caching in Remix can be implemented using client loaders and packages like Remix client cache to store and retrieve data efficiently, reducing server requests.

Critical data is essential for the initial page load and determines the page's response status, while deferred data can be loaded later, enhancing performance by prioritizing essential content.

Consider the value and user experience of offline functionality, including how to display offline content, syncing data states, and indicating connectivity status to users.

Bret Little
Bret Little
21 min
28 Oct, 2024

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Welcome to Turning It Up to 11 at React Advanced. Hydrogen, an eCommerce framework built on top of Remix, focuses on efficient data loading. Loading data in parallel using promise.all is four times faster than loading in a waterfall manner. Split promise.all into two when handling data dependencies. Caching static data can significantly improve page load time. Optimize product pages by reducing awaits and prioritizing essential content. Use suspense boundaries and skeleton UI for optimal data loading. Place non-blocking requests before primary data request for better performance. Remix client cache handles caching automatically. Consider data dependencies and prioritize critical content.
Available in Español: Turning It up to Eleven

1. Introduction to Turning It Up to 11

Short description:

Welcome to Turning It Up to 11 at React Advanced. I'm Brett Little, a developer at Shopify, working on the Hydrogen team. Hydrogen is an eCommerce framework built on top of Remix. The web development community often focuses on framework debates, but the speed of your app is determined by how you load data. Data fetching in Remix is done through file-based routing, allowing you to make async requests to APIs and render the data.

Welcome to Turning It Up to 11 at React Advanced. My name is Brett Little, and I'm really excited to be here today.

Again, my name is Brett. I live in Maine. I grow flowers, and I work at Shopify on the Hydrogen team. Hydrogen is a framework on top of Remix where we add all sorts of eCommerce-specific opinions that helps you get started in building an eCommerce site really quickly on Remix.

You can follow along at littlebrett on Twitter. But Turning It Up to 11, what do I mean by Turning It Up to 11? There's this old, old movie. You maybe have never heard of it, but here we got these two old rockers that are talking about rocking it out on the stage. Specifically, they talk about how their amps all go to 11, all the numbers go up to 11. But one of the guys asked, well, why put it up to 11? Why not just make 10 the highest number and make it louder instead of having this extra 11 notch on the dial? Which is kind of interesting. It's kind of this funny sort of moment of this sort of play on the scale of numbers. Does it even matter? Well, I kind of feel like the web development community is sometimes like that a little bit. We argue there's all sorts of drama over this framework versus that framework. This is better, that's worse. And everyone gets really upset, and there's all this sort of drama. But I'm here to say your app isn't slow due to your framework. Usually your app is slow by the way that you're loading data.

So when I say loading data, what do I mean? Well, most apps display a lot on the page and getting all the data to display on the page takes time. So in this case, maybe it's an e-commerce app. An e-commerce app might have tons of products. Those products might have reviews on them. They might have availability, price images, and a lot of this data might be coming from different services, sometimes even third-party services. And you got to get all that together, get it on the page so you can render something, and that takes time sometimes. So data fetching in Remix specifically. Let's do a little bit of a review. If you're not familiar with Remix, Remix provides routes, file-based routing. Each file for a route can have an export async function that is a loader. That export async function that is a loader is going to execute to define the data for this page. So inside here you can make any async requests to any API, get some data, return it out of the loader, and then that default export function, we can pull that data inside and we can render something.

2. Advanced Loader Techniques

Short description:

Most apps are more complicated than simply rendering what's inside the app. Loading data in a waterfall manner, one after another, can be four times slower than loading everything in parallel using promise.all. Having more than one await in an async function indicates a waterfall and may need to be reconsidered for parallelization.

Whatever is rendered here is what's going to show up inside the app. But we're turning it up to 11. That's pretty simple. Most apps are a lot more complicated than that. I see on the Hydrogen team hundreds of different apps that are built out that do some wild stuff inside their loaders.

So let's consider what might be a little bit more advanced, what might be happening. So inside our loader, maybe we're going to load some products, then maybe we're going to load some reviews, then maybe we might load recommendations. And after that, we might also load in the logged in user to display something about who's logged in. That's great. This code actually looks really, really nice. It's really simple. It's easy to understand. Load this, then this, then this. Looks awesome.

But actually, there's a serious problem with this code. Specifically, it's called what is a waterfall. A waterfall is where we have to load one thing, and it's not until that finishes loading, then we can proceed to load the next thing. So essentially, we're doing nothing in parallel. We're loading one, then the other, then the other, then the other. So this is four times slower than it needs to be. And the easiest way to solve this is just wrap everything in promise.all.

If you wrap everything in promise.all, now there's one await at the top, and we load everything in parallel, so now the waterfall is flat. There's not any waiting on anything, and once everything's done, we can render the UI. But if I'm frank, this isn't as intuitive as this. Like this whole promise.all thing, it's kind of unfortunate JavaScript, I feel like, the primitives that we have, this just doesn't look as nice to a lot of developers, and a lot of developers, I feel like, would rather see something like this.

And the key here, though, is if you ever have more than one await in an async function, not even just in Remix, just in any async function in JavaScript, if you have more than one await, you have a waterfall. Maybe that waterfall is necessary. Maybe it's But if you have more than one await, something is processing, you're waiting for it to finish, and then something else is going to process after it. So if you ever see more than one await in a single async function, maybe you should consider looking at it, talking through is that necessary, could it be parallelized? But again, we're tearing this up to 11, it can be, it's not always this complicated. It's not always so easy just to parallelize everything in the function.

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.
Using useEffect Effectively
React Advanced 2022React Advanced 2022
30 min
Using useEffect Effectively
Top Content
Today's Talk explores the use of the useEffect hook in React development, covering topics such as fetching data, handling race conditions and cleanup, and optimizing performance. It also discusses the correct use of useEffect in React 18, the distinction between Activity Effects and Action Effects, and the potential misuse of useEffect. The Talk highlights the benefits of using useQuery or SWR for data fetching, the problems with using useEffect for initializing global singletons, and the use of state machines for handling effects. The speaker also recommends exploring the beta React docs and using tools like the stately.ai editor for visualizing state machines.
React Query: It’s Time to Break up with your "Global State”!
React Summit Remote Edition 2020React Summit Remote Edition 2020
30 min
React Query: It’s Time to Break up with your "Global State”!
Top Content
Global state management and the challenges of placing server state in global state are discussed. React Query is introduced as a solution for handling asynchronous server state. The Talk demonstrates the process of extracting logic into custom hooks and fixing issues with state and fetching logic. Optimistic updates with mutation are showcased, along with the benefits of using React Query for data fetching and mutations. The future of global state management is discussed, along with user feedback on React Query. The Talk concludes with an invitation to explore React Query for server state management.
Jotai Atoms Are Just Functions
React Day Berlin 2022React Day Berlin 2022
22 min
Jotai Atoms Are Just Functions
Top Content
State management in React is a highly discussed topic with many libraries and solutions. Jotai is a new library based on atoms, which represent pieces of state. Atoms in Jotai are used to define state without holding values and can be used for global, semi-global, or local states. Jotai atoms are reusable definitions that are independent from React and can be used without React in an experimental library called Jotajsx.
Announcing Starbeam: Universal Reactivity
JSNation 2022JSNation 2022
27 min
Announcing Starbeam: Universal Reactivity
Starbeam is a library for building reactive user interfaces with JavaScript, similar to Svelte, Vue, and Ember. It provides a data structure and query feature for filtering and sorting. The useStarBeam function ensures JSX reconciliation only occurs when reactive dependencies change. Starbeam tracks every read and write operation to update the component accordingly. It can be used with React and other frameworks, and offers debugging tools and locale integration.
Thinking in React Query
React Summit 2023React Summit 2023
22 min
Thinking in React Query
Top Content
Watch video: Thinking in React Query
React Query is not a data fetching library, but an Asian state manager. It helps keep data up to date and manage agent life cycles efficiently. React Query provides fine-grained subscriptions and allows for adjusting stale time to control data fetching behavior. Defining stale time and managing dependencies are important aspects of working with React Query. Using the URL as a state manager and Zustand for managing filters in React Query can be powerful.

Workshops on related topic

Rethinking Server State with React Query
React Summit 2020React Summit 2020
96 min
Rethinking Server State with React Query
Top Content
Featured Workshop
Tanner Linsley
Tanner Linsley
The distinction between server state and client state in our applications might be a new concept for some, but it is very important to understand when delivering a top-notch user experience. Server state comes with unique problems that often sneak into our applications surprise like:
- Sharing Data across apps- Caching & Persistence- Deduping Requests- Background Updates- Managing “Stale” Data- Pagination & Incremental fetching- Memory & Garbage Collection- Optimistic Updates
Traditional “Global State” managers pretend these challenges don’t exist and this ultimately results in developers building their own on-the-fly attempts to mitigate them.
In this workshop, we will build an application that exposes these issues, allows us to understand them better, and finally turn them from challenges into features using a library designed for managing server-state called React Query.
By the end of the workshop, you will have a better understanding of server state, client state, syncing asynchronous data (mouthful, I know), and React Query.
State Management in React with Context and Hooks
React Summit Remote Edition 2021React Summit Remote Edition 2021
71 min
State Management in React with Context and Hooks
WorkshopFree
Roy Derks
Roy Derks
A lot has changed in the world of state management in React the last few years. Where Redux used to be the main library for this, the introduction of the React Context and Hook APIs has shaken things up. No longer do you need external libraries to handle both component and global state in your applications. In this workshop you'll learn the different approaches to state management in the post-Redux era of React, all based on Hooks! And as a bonus, we'll explore two upcoming state management libraries in the React ecosystem.