The traditional small program technology is easily affected by the network environment. When the network quality is poor, it may lead to the situation that the small program package cannot be pulled. This problem can be avoided by preset small program. This paper introduces the principle of preset small program and the realization of preset small program.

What is a preset applets

Preset applets pack static resources such as the rendering, logic, and configuration of the applets into a compressed package. The client downloads the applets to the local PC and loads resources directly from the local PC. Preset applets can get rid of the influence of network environment on mPaaS applets page to the greatest extent. Using preset packages gives clients the following advantages:

  • Enhanced user experience Static resources on a page are embedded in an application in the form of a preset package and released together with the application. This enables users to start using the application for the first time without relying on the network environment to download the resources.

  • Implementation of dynamic updates In the launch of a new release or emergency release, you can do iterative development in the small program IDE, released through the mPaaS console, the integrated small program SDK in the client side will automatically update the small program to the latest version. This release does not require app store approval and allows users to receive updates early.

The realization principle of preset small program

This paper introduces the realization principle of preset applets from the following aspects:

  • The structure of a small program preset package
  • The process of using a small program preset package

The structure of a small program preset package

The prefab package is a compressed file in. Amr format. After changing the suffix amr to zip, you can see the HTML resources and JavaScript code contained in it. After the applets are loaded, these resources and code can be rendered in the UC kernel.

Taking Android as an example, the following figure shows the directory structure of a general resource pack:

  • Level-1 directory: Generally, it is the ID of the resource package, for example, 2020121620201216_1.0.1.zip.
  • Secondary directories and subsequent customized resource files for the business. And set the current preset package default open main entry file, such as/index.html.

The process of using a small program preset package

The process of using applets presets can be divided into three steps:

  1. Request package information The process of requesting an applet package from a server and storing the package information to a local database. The package information includes the download address and version number of the package.

  2. Download applets Download applets from the server to the mobile phone.

  3. Install the small program package download directory, copy to the phone installation directory.

The premise condition

  • Applets have been added. For more information about access to applets, see Getting Started with Applets quickly.

  • The H5 container component has been added. For more information about H5 container access, see Quick Start using H5 Containers.

Procedure -Android

Preset applets.

  1. Publish the applets on the mPaaS console and download the AMR files and configuration files.
  2. Place the downloaded AMR files and configuration files in assets of the mPaaS project.
  3. Add preset code to the project to call the preset code to install the application when it starts. The following is an example of the preset code:

Description:

  • This method is a blocking call. Do not call the built-in prefab method on the main thread.
  • This method can only be called once. If the call is repeated, only the first call is valid. Therefore, you need to pass in all required preset package information at one time.
  • If multiple AMR packages are built in, you need to ensure that the files already exist. If not, other built-in preset packages will fail.

Start the applet. The sample code for starting the applets is shown below.

Update applet

By default, every time you open an app, the applets SDK tries to check if there is an updatable version. Due to server pressure, the check interval is limited. The default interval is 30 minutes. If you want to check the latest available version immediately, call the code below to request an update. Typically, it can be invoked after the application is started or the user logs in.

Verifying security signature

Applets have a signature verification mechanism to prevent malicious programs from tampering with the downloaded applets. This mechanism can be enabled by calling the MPNebula interface to set check parameters. If you use 10.1.60 or later, you need to enable container configuration. For details, see H5 Container Configuration.

Description:

  • Call before opening the offline package for the first timeMPNebulaInterface. Otherwise, public key initialization fails. For public and private keys, seeConfigure Offline Package > Key Management.
  • Regardless of whether signature verification is enabled on the client, signature verification is forcibly performed on the mobile phone identified as root.

Delete local applets

Nebula provides an interface for deleting local application information. After the local application information is deleted, the system requests the server to download and update the local applet information again when the application is restarted.

Note: The lowest baseline versions supported by this API in the 10.1.68 and 10.1.60 series are 10.1.68.8 and 10.1.60.14, respectively.

Procedure -iOS

Preset applets.

A. Publish the applets on the mPaaS console and download the AMR file and configuration file.

B. create a new independent bundle, such as DemoCustomPresetApps. Bundle, will download them from the publishing platform. The amr offline package and h5_json json file added to the bundle.

Major: Currently, only the H5_json. json configuration file of a single offline package can be downloaded on the distribution platform. If multiple packages are preset, you need to manually merge data in different H5_json. json files into one configuration file.

C. when initializing small program, in initNebulaWithCustomPresetApplistPath interface, set the preset applet offline package path as in the previous step to create the bundle.

Launch applets

Similar to the non-preset applet, when you enter the corresponding page, you load the applet by calling the interface methods provided by the Nebula container.

Update applets.

By default, every time you open an app, the applets SDK tries to check if there is an updatable version. To reduce the pressure on the server, this check has an interval of 30 minutes by default. If you want to check the latest available version immediately, call the code below to request an update. Typically, it can be invoked after the application is started or the user logs in.

Verify the security signature.

Applets have a signature verification mechanism to prevent malicious programs from tampering with the downloaded applets. This mechanism can be turned on by calling the applet interface to set check parameters.

Description:

  • Call the MPNebulaAdapterInterface interface before opening the small program package for the first time; otherwise, initialization of the public key will fail. For details about public and private keys, see Configuring Applets > Key Management.
  • Open attestation

Delete local applets.

Nebula provides an interface for deleting local application information. After the local application information is deleted, the system requests the server to download and update the local applet information again when the application is restarted.

conclusion

MPaaS small program is derived from Alipay small program framework, which is tempered by the volume of hundreds of millions of online businesses. Its security is comparable to alipay’s original ability. Not only for its own App to put small programs, but also can quickly build packaging, covering Alipay, Taobao, Dingding and other applications.

By using the scheme of preset applets, preset applets can not only get rid of the influence of network environment on mPaaS applets page to the greatest extent, but also deeply experience user experience and realize dynamic update.

Liu Qiyang, Teng Hongcai

END