# Periodic updates

Start from base library version 2.8.0. Please remaining backward compatible.

Effective condition: The user has used the Mini Program within 7 days

Periodic updates can also pull data from the server in advance when the user does not open the Mini Program, which can render the page faster when the user opens the Mini Program, reduce the user waiting time, and enhance the availability in weak network conditions.

# Use Process

# 1. Configuration Data Download Address

When the data source is the developer server, support the configuration of gray scale, gray scale data download address can be different from the data download address, gray scale cannot be retracted, and 100% Grayscale is regarded as update data address to grayscale data address, If you want to test, you can change the gray scale to 100% 0, that is, only grayscale for the developer experience.

  1. Login Mini Program MP Management background, into the development management -> Development settings -> Data is updated periodically, click to open
  2. The personal agent Mini Program only supports the configuration of the cloud development environment
  3. Non-personal subject Mini Program supports configuring HTTPS data underload address, Cloud development environment

# 2. Set up TOKEN

When you start the Mini Program for the first time, call the wx.setBackgroundFetchToken() Setting up a TOKEN The character string, which can be related to the user state, will be brought in the subsequent WeChat client request to the developer server to facilitate the latter to verify the legitimacy of the request.

Example:

App({
  onLaunch() {
    wx.setBackgroundFetchToken({
      token: 'xxx' 
    })
  }
})

# 3. WeChat client regularly pull data

WeChat client will be in certain network conditions, every other 12 Hours (the time of the last successful update) to the configured data download address HTTP GET Request, which contains the query Parameters are as follows. When the data is obtained, the entire HTTP body Cache to local.

parameter type Introductions
appid String Mini Program identification
Token String The front set TOKEN
timestamp Number Time stamp, the time when the WeChat client initiated the request

query The parameter will be used urlencode Handle

The data type returned by the developer server interface should be a character string and should not exceed 256KB, otherwise it will not be possible to cache the data

# 4. Read data

When the user starts the Mini Program, call the wx.getBackgroundFetchData() Gets the data that has been cached locally.

Example:

App({
  onLaunch() {
    wx.getBackgroundFetchData({
      fetchType:  'Periodic', 
      success(res) {
        console.log(res.fetchedData) // Cached data
        console.log(res.timeStamp) // The client gets the timestamp of the cached data
      }
    })
  }
})

# Debugging method

Because the WeChat client every other 12 It is not convenient to debug the periodic update feature because the request is only made once an hour. Therefore, in order to facilitate the debugging of periodic data, the tool provides the following debugging capabilities to developers.Periodic data debugging