Detailed explanation of the WeChat applet request pre-processing method

Detailed explanation of the WeChat applet request pre-processing method

question

Because some of our pages request data in onload and then render the view, if we can move the data request step forward before the mini program page jumps, we can request the data earlier. The effect of the optimization depends on the time required for the page jump.

need

A request pre-processing method is needed to make full use of the jump time and request interface data in advance, but to minimize the cost of modifying old projects. Because the small program project I am responsible for now uses axios to request interface data, here I will only give an example of the transformation idea of ​​post in axios. Here I rewrite the post method to cache the interfaces that need to be requested in advance when requesting, and return the promise of the first request when the second request is made, so that there is no need to initiate a new request.

Specific steps

1. Modify the post method

http file

let instance = axios.create({ // Create an axios request instance // Omit some code});

let { post } = instance; // Save the original post method let cache = {}; // Request cache instance.post = function(...list) {

 let [url, data = {}] = list;

 if (cache[url]) { // Return the pre-requested promise
 let pre = cache[url];
 delete cache[url];
 return pre;
 }

 if (data.pre) { //Use pre as a pre-request to determine if it is a pre-request delete data.pre;
 cache[url] = post(...list)
 return cache[url];
 }

 return post(...list); //normal request}

2. Use

The page before the jump, that is, the previous page

// Omit some code...

// This is the data to be requested from the interface on the next page. Request it in advance before wx.navigateTo jumps and store it.
$http.post('/act/activities/lucky:search', { activityId: 12 , pre: true })

wx.nextTick(() => { //Using wx.nextTick allows the above request to be sent first and then jump wx.navigateTo({
 url: `${TYPE_TO_ROUTE_MAP[templateType]}?id=${activity.activityId}`
 });
})

// Omit some code...

Effect

Not using preloading

Using Preload

The width difference of the red boxes indicates how much time is required to request the interface data in advance, which is about 100ms. Because the static resource address below comes from the interface data, it is equivalent to reducing the resource loading time after the congestion by about 100ms.

Summarize

  • The principle is to use the time of the applet jump to request data in advance, so it will benefit more for mobile phones with poor performance. Although it seems to save about 100ms in the developer tool, there are the following two limitations:
  • The page is loaded and the data in the pre-requested interface data is required
  • It is necessary to initiate a pre-request on the previous page and perform nextTick processing on the jump

As a result, the benefits of this optimization are indeed somewhat useless for the entire project.

This is the end of this article about WeChat Mini Program request pre-positioning. For more relevant Mini Program request pre-positioning content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed steps for setting up HTTP requests in WeChat mini program
  • WeChat applet GET request example detailed explanation
  • Detailed explanation of WeChat applet synchronization request authorization
  • WeChat applet network request (GET request) detailed explanation
  • WeChat applet POST request example detailed explanation
  • WeChat applet network request (post request, get request)
  • WeChat applet http request encapsulation detailed explanation and example code
  • WeChat applet HTTP interface request encapsulation code example
  • WeChat applet network request encapsulation example

<<:  Detailed tutorial on how to install MySQL 5.7.18 in Linux (CentOS 7) using YUM

>>:  Linux dual network card binding script method example

Recommend

Detailed explanation of the loop form item example in Vue

Sometimes we may encounter such a requirement, th...

React's reconciliation algorithm Diffing algorithm strategy detailed explanation

Table of contents Algorithmic Strategy Single-nod...

The leftmost matching principle of MySQL database index

Table of contents 1. Joint index description 2. C...

VMware kali virtual machine environment configuration method

1|0 Compile the kernel (1) Run the uname -r comma...

MySQL 8.0.12 installation graphic tutorial

MySQL8.0.12 installation tutorial, share with eve...

Detailed explanation of MySQL backup and recovery practice of mysqlbackup

1. Introduction to mysqlbackup mysqlbackup is the...

Example of how to configure nginx to implement SSL

Environmental Description Server system: Ubuntu 1...

Tutorial on building file sharing service Samba under CentOS6.5

Samba Services: This content is for reference of ...

How to copy MySQL table

Table of contents 1.mysqldump Execution process: ...

The use of v-model in vue3 components and in-depth explanation

Table of contents Use two-way binding data in v-m...

mysql5.5 installation graphic tutorial under win7

MySQL installation is relatively simple, usually ...

How to connect XShell and network configuration in CentOS7

1. Linux network configuration Before configuring...

Vue Page Stack Manager Details

Table of contents 2. Tried methods 2.1 keep-alive...

How to configure Nginx virtual host in CentOS 7.3

Experimental environment A minimally installed Ce...