May 18, 2021 WeChat Mini Program Development Document
Pre-pull can pull business data from third-party servers in advance through WeChat background when the small program starts cold, and can render the page faster when the code package is loaded, reducing the user's waiting time, thus increasing the speed of opening the small program.
Log in to the small program MP management background, go to the settings -gt; development settings -gt; data preload, click on on, fill in the data download address, only httpS support.
The first time you start a small program, call wx.setBackgroundFetchToken() to set up a TOKEN string that can be related to the user state and will be brought with you when a subsequent WeChat client requests from the developer server, making it easy to verify the legitimacy of the request to the latter.
Example:
App({
onLaunch() {
wx.setBackgroundFetchToken({
token: 'xxx'
})
}
})
When the user opens the gadget, weChat servers initiate an HTTP GET request to the developer server (the data download address configured above) that contains the query parameters as follows, and when the data is obtained, the entire HTTP body is cached locally.
Parameters | Type | Required | Description |
---|---|---|---|
appid | String | Is | The small program identity. |
token | String | Whether | TOKEN set earlier. |
code | String | Whether | User login credentials, pre-generated by WeChat side when TOKEN is not set, can be called auth.code2Session in the developer background in exchange for information such as openid. |
timestamp | Number | Is | Timestamp, the time at which the WeChat client initiated the request |
path | String | no | Open the path of the applet. |
query | String | no | Open the query of the applet. |
scene | Number | Whether | Open the scene value of the small program. |
The query parameter is processed using urlencode
Token and code exist only one and are used to identify the user.
The data type returned by the developer server interface should be a string and should be no larger than 256KB in size, otherwise the data will not be cached
When the user starts the program, call wx.getBackgroundFetchData() to get the data that has been cached locally.
Example:
App({
onLaunch() {
wx.getBackgroundFetchData({
fetchType: 'pre',
success(res) {
console.log(res.fetchedData) // 缓存数据
console.log(res.timeStamp) // 客户端拿到缓存数据的时间戳
console.log(res.path) // 页面路径
console.log(res.query) // query 参数
console.log(res.scene) // 场景值
}
})
}
})
In order to facilitate debugging data pre-pull, the tool provides the following debugging capabilities to developers.