# Data prefetched

Pre-pulling enables pre-pulling of business data to third-party servers via the WeChat background during cold startup, rendering the page faster when the code package is loaded, reducing user wait times, and improving the opening speed of the Mini Program.

# Use Procedure

# 1. Download the configuration data

When the data source is a developer server, it is supported to configure a greyscale ratio, and the greyscale data download URL can be distinguished from the data download URL. The graying ratio is irreversible, and 100% graying is considered to be the updated data address as a graying data address. If you need to test, you can change the graying proportion to 0 percent, i.e. only graying for developer experiencers.

  1. Log in to Weixin Mini Program MP management background, go to Development Management - > Development Settings - > Data Preload, click Open
  2. Personal principals Weixin Mini Program Provisioning cloud development environments only
  3. Non-personal subject Weixin Mini Program Support configuration of HTTPS data carrier address, cloud development environment

# 2. Set up a TOKEN

After logging in Weixin Mini Program, the Mini Program can call wx.setBackgroundFetchToken () Set a custom TOKEN character string, which can be associated with the user mode. TOKEN will be sent to the developer server on the next pre-pull or periodic update, so the server can verify the validity of the request.

Tips:wx.setBackgroundFetchTokenis an optional interface, not a mandatory one.

Examples:

App({
  onLaunch() {
    // 用户登录后
    wx.setBackgroundFetchToken({
      token: 'xxx'
    })
  }
})

# 3. WeChat Customer pull data in advance

When a user opens Weixin Mini Program, the WeChat server initiates an HTTP GET request to the developer server (the data download configured above), containing the following query parameters, and caches the entire HTTP body locally when the data is obtained.

There is only one token and code in the parameter, which identifies the user. **Note: If you choose to use code, the user login state may be refreshed when triggering the data prepull, as described in checkSessionKey ** 。

parameter type Required to fill in Introductions
appid String yes Weixin Mini Program Identification marks.
token String no TOKEN set earlier.
code String no Login credentials, not set TOKEN by WeChat side pre-generated, developers can call auth.code2Session background, in exchange for openid and other information.
timestamp Number yes Timestamp, WeChat The time at which the client initiates the request
path String no Open the path to Weixin Mini Program.
query String no Open the query for Weixin Mini Program.
scene Number no Open the scene value of Weixin Mini Program.
customMiniprogramVersion String no Weixin Mini Program Version number

Query parameters are processed using urlencode

The data type returned by the DSI should be a character string and should not exceed 256KB, otherwise the data cannot be cached

# 4. Read the data

When the user starts Weixin Mini Program, call wx.getBackgroundFetchData and [wx.onBackgroundFetchData ( Get data that has been cached locally.

Examples:

App({
  onLaunch() {
    wx.onBackgroundFetchData(() => {
      console.log(res.fetchedData) // 缓存数据
      console.log(res.timeStamp) // 客户端拿到缓存数据的时间戳
    })
    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) // 场景值
      }
    })
  }
})

# Debugging methods

To facilitate debugging data pre-pulling, the tool provides the following debugging capabilities to developers, including pre-pull data debugging .