title: Component prefetching with prefetchComponents
date: 2024/8/17
updated: 2024/8/17
author: cmdragon
excerpt:
Abstract: This article describes the prefetchComponents feature for prefetching components to improve the user experience. By downloading and caching components in the client-side background, it ensures that they are loaded quickly when users need them. The article covers the basic concepts of prefetchComponents, the difference with preloading, how to use and how to configure and apply this feature in the project, and ultimately achieve the purpose of optimizing the application loading speed.
categories:
- front-end development
tags:
- subassemblies
- pre-select
- (computing) cache
- subscribers
- experience for oneself
- client (computing)
scanningtwo-dimensional barcodeFollow or microsoft search:Programming Intelligence Front-End to Full-Stack Communication and Growth
utilizationprefetchComponents
Perform component prefetching
In theprefetchComponents
is a tool that helps you improve the user experience by downloading and caching components in advance while your application is running. When you know that certain components are likely to be used by users, you can reduce latency and improve loading speed by prefetching them.
What is it?prefetchComponents
?
prefetchComponents
is a function provided to manually prefetch components that are globally registered in the application. This means that a component is downloaded and cached in the background before the user needs it, thus avoiding the user having to wait for the component to download when they need it.
take note of:prefetchComponents
Only takes effect on the client side, nothing happens during server-side rendering.
Prefetching vs.
prefetchComponents
together withpreloadComponents
The functions are similar, but there are some differences:
- Prefetch: Downloading and caching components in the background allows them to be loaded faster when the user really needs them.
- Preload: Load components more proactively to ensure that they are ready when the user needs them.
How to useprefetchComponents
basic usage
You can do this byprefetchComponents
Prefetch a single component or multiple components. Component names must use PascalCase nomenclature.
Prefetching individual components
await prefetchComponents('MyGlobalComponent');
Prefetching multiple components
await prefetchComponents(['MyGlobalComponent1', 'MyGlobalComponent2']);
Example: Component prefetching
Here's a real-world example of how to use theprefetchComponents
Prefetch components.
1. Creating components
First, create two simple components in thecomponents
Catalog.
components/
<template>
<div>
<h1>Component 1</h1>
</div>
</template>
<script setup>
('MyGlobalComponent1 loaded.');
</script>
components/
<template>
<div>
<h1>Component 2</h1>
</div>
</template>
<script setup>
('MyGlobalComponent2 loaded.');
</script>
2. UtilizationprefetchComponents
In a page or plugin, use theprefetchComponents
to prefetch these components. For example, thepages/
Page:
pages/
<template>
<div>
<h1>Home Page</h1>
</div>
</template>
<script setup>
import {onMounted} from 'vue';
onMounted(async () => {
await prefetchComponents(['MyGlobalComponent1', 'MyGlobalComponent2']);
});
</script>
3. Configuration
Make sure your component is globally registered in the In the Center:
export default defineNuxtConfig({
components: true, // ensure components are imported automatically
}).
4. Running the project
Launch your application:
npm run dev
Verify Prefetch
Open your browser and check the web panel of the developer tools. When the page loads, you should see theMyGlobalComponent1
cap (a poem)MyGlobalComponent2
of the associated web request has been triggered. Thus, the component will be prefetched and cached before the user actually requests it.
summarize
prefetchComponents
is a powerful tool to enhance
The application's user experience reduces latency by downloading and caching components in advance. Components load faster when users need to use them. This is achieved through the judicious use ofprefetchComponents
, you can optimize your app for smoother user interactions.
For the rest of the article, please click to jump to the personal blog page or scan the code to follow or WeChat search:Programming Intelligence Front-End to Full-Stack Communication and Growth
, read the full article:Component prefetching with prefetchComponents | cmdragon's Blog
Past articles are archived:
- Asynchronous initialization with onNuxtReady | cmdragon's Blog
- Using onBeforeRouteUpdate Combinatorial Functions to Enhance Your Application's User Experience | cmdragon's Blog
- Using onBeforeRouteLeave Combinatorial Functions to Enhance Your App's User Experience | cmdragon's Blog
- Flexible Routing with navigateTo | cmdragon's Blog
- Page-level hybrid rendering with Nuxt 3's defineRouteRules | cmdragon's Blog
- Getting to grips with Nuxt 3's page metadata: custom configurations with definePageMeta | cmdragon's Blog
- Creating Route Middleware with defineNuxtRouteMiddleware | cmdragon's Blog
- Defining Vue Components with defineNuxtComponent` | cmdragon's Blog
- Detailed Guide to Creating Error Objects with createError | cmdragon's Blog
- Clear Nuxt state cache: clearNuxtState | cmdragon's Blog
- Clear Nuxt Data Cache: clearNuxtData | cmdragon's Blog
- Clearing processed errors with clearError | cmdragon's Blog
- Dynamically Adding Middleware with addRouteMiddleware | cmdragon's Blog
- Blocking Navigation with abortNavigation | cmdragon's Blog
- HTTP request using $fetch | cmdragon's Blog
- Managing Responsive State with useState | cmdragon's Blog
- Optimize your website for SEO with useServerSeoMeta | cmdragon's Blog
- SEO Configuration with useSeoMeta | cmdragon's Blog
- Must Read: Runtime Configuration Made Easy with useRuntimeConfig | cmdragon's Blog