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

Master-slave synchronization configuration of Mysql database

Table of contents Mysql master-slave synchronizat...

MySQL foreign key (FOREIGN KEY) usage case detailed explanation

Introduction: The disadvantages of storing all da...

How to deploy the crownblog project to Alibaba Cloud using docker

Front-end project packaging Find .env.production ...

How to change the tomcat port number in Linux

I have several tomcats here. If I use them at the...

Implementation code for operating mysql database in golang

Preface Golang provides the database/sql package ...

Case study of dynamic data binding of this.$set in Vue

I feel that the explanation of this.$set on the I...

Detailed example of mysql similar to oracle rownum writing

Rownum is a unique way of writing in Oracle. In O...

Binary Type Operations in MySQL

This article mainly introduces the binary type op...

JavaScript lazy loading detailed explanation

Table of contents Lazy Loading CSS styles: HTML p...

Some ways to eliminate duplicate rows in MySQL

SQL statement /* Some methods of eliminating dupl...

Pure CSS to achieve hover image pop-out pop-up effect example code

Implementation principle The main graphics are co...

Solution to the problem that Docker cannot stop or delete container services

Preface Today, a developer gave me feedback that ...