# Data prepull

Pre-pull can pull the business data from the third-party server through the WeChat background in advance when the Mini Program is cold started, and the page can be rendered faster when the code package is loaded, reducing the user waiting time, thereby improving the opening speed of the Mini Program. 。

# 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 Preload, Click 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 pull data in advance

When the user opens the Mini Program, the WeChat server will initiate a message to the developer server (the data download address configured above). HTTP GET Request, which contains the query Parameters are as follows. When the data is obtained, the entire HTTP body Cache to local.

In the parameter Token and Code Only one will exist to identify the user.Note: If you choose to use Code, which may refresh the user login state when triggering the data prepull, see checkSessionKey

parameter type Required Introductions
appid String yes Mini Program identification.
Token String no The front set TOKEN。
Code String no Login credentials, not set TOKEN by WeChat side pre-generated, can be invoked in the developer background Auth.code2Session, in exchange for Openid Wait for information.
timestamp Number yes Time stamp, the time when the WeChat client initiated the request
path String no Open the path to the Mini Program.
query String no Open the Mini Program's query.
scene Number no Open the scene value of the Mini Program.
customMiniprogramVersion String no Mini Program version number

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 and wx.onBackgroundFetchData Gets the data that has been cached locally.

Example:

App({
  onLaunch() {
    wx.onBackgroundFetchData(() => {
      console.log(res.fetchedData) // Cached data
      console.log(res.timeStamp) // The client gets the timestamp of the cached data
    })
    wx.getBackgroundFetchData({
      fetchType:  'pre', 
      success(res) {
        console.log(res.fetchedData) // Cached data
        console.log(res.timeStamp) // The client gets the timestamp of the cached data
        console.log(res.path) // Page Path
        console.log(res.query) // query parameter
        console.log(res.scene ) // Scene value
      }
    })
  }
})

# Debugging method

In order to facilitate the debugging data prepull, the tool provides the following debugging capabilities to the developer.Prepull data debugging