Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat programs are updated periodically


May 18, 2021 WeChat Mini Program Development Document


Table of contents


Periodic updates

Base library 2.8.0 starts to support, and low versions need to be compatible.
Conditions in effect: Small programs that the user has used within seven days

Periodic updates can pull data from the server in advance without the user opening the small program, rendering the page faster when the user opens the small program, reducing user wait times, and enhancing availability under weak network conditions.

Use the process

1. Configure the data download address

Log in to the small program MP management background, go to the settings -gt; development settings -gt; data periodic updates, click on on, fill in the data download address.

WeChat programs are updated periodically

2. Set up TOKEN

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'
    })
  }
})

3. WeChat clients regularly pull data

WeChat clients initiate an HTTP GET request to the configured data download address every 12 hours under certain network conditions, whichever is the time for the previous successful update, which contains the query parameters below, and the entire HTTP body is cached locally when the data is acquired.

Parameters Type Description
appid String The small program identity
token String TOKEN set earlier
timestamp Number Timestamp, the time at which the WeChat client initiated the request
Query parameters use urlencode processing
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

4. Read the data

When the user starts the program, call wx.getBackgroundFetchData() to get the data that has been cached locally.

Example:

App({
  onLaunch() {
    wx.getBackgroundFetchData({
      fetchType: 'periodic',
      success(res) {
        console.log(res.fetchedData) // 缓存数据
        console.log(res.timeStamp) // 客户端拿到缓存数据的时间戳
      }
    })
  }
})

Debug method

Because WeChat clients make requests every 12 hours, debugging periodic updates can be inconvenient. Therefore, in order to facilitate debugging periodic data, the tool provides the following debugging capabilities to developers.