This specification defines the MiniApp lifecycle events and the process to manage MiniApp and each page's lifecycle. Implementing this specification enables the user agent to manage the lifecycle events of both the global application lifecycle and the page lifecycle.

Background

As described in MiniApp Standardization White Paper, in a miniapp, the View layer is separated from the Logic layer. The View layer is responsible for rendering MiniApp pages, including Web rendering and native rendering, which can be considered as hybrid rendering. The Logic layer is implemented with JavaScript Worker. The Logic layer is responsible for MiniApp’s event processing, API calling and lifecycle management.

MiniApp lifecycle mechanism provides a means to manage MiniApp's View layer and Logic layer through the MiniApp [=Document/global application lifecycle events=] and MiniApp [=Document/page lifecycle events=]. Developing MiniApp with knowledge of the MiniApp [=Document/global application lifecycle states=] and MiniApp [=Document/page lifecycle states=] can lead to improved user experiences. MiniApp lifecycle includes a set of events, with which MiniApp can choose to alter its behavior based on its state.

MiniApp Global Application Lifecycle

MiniApp Global Application Lifecycle Events

This spec defines what the lifecycle of a MiniApp global application is and adds definition to enable MiniApp to respond to five important global application lifecycle events commonly performed by MiniApp:

For each MiniApp, after [=Document/initialization=], it will be either [=Document/running in foreground=] or [=Document/running in background=].

When user chooses to close the MiniApp by clicking the close button on MiniApp, or go to the mobile phone’s home screen, the MiniApp will not be destroyed immediately, but switch to be [=Document/running in background=].

When user reopens the same MiniApp, MiniApp will switch from [=Document/running in background=] to [=Document/running in foreground=].

[=Document/unloading=] marks the end of the MiniApp session, and the removal of all the temporary resources from the cache. MiniApp user agents perform this removal once the MiniApp meets the criteria. When MiniApp runs in the background for more than a specific time duration (e.g., 5 minutes), or occupies too many system resources in the background, the MiniApp should be unloaded.

MiniApp Global Application Lifecycle States

This spec formalizes five global application lifecycle states to support the above [=Document/global application lifecycle events=]:

launched
Lifecycle state for MiniApp [=Document/initialization=]. This means that the MiniApp [=Document/initialization=] is completed, and it is triggered only once. Through this event, developer can obtain the information of the MiniApp, such as URI, source info, etc.
shown
Lifecycle state for MiniApp [=Document/running in foreground=]. It is triggered once the MiniApp enters the [=Document/launched=] state, or once the MiniApp switches to be [=Document/running in foreground=] from [=Document/running in background=].
hidden
Lifecycle state for MiniApp [=Document/running in background=]. It is triggered once the MiniApp switches to be [=Document/running in background=] from [=Document/running in foreground=].
error
Lifecycle state for MiniApp [=Document/in error=]. It is triggered once the MiniApp is confronted with script error.
unloaded
Lifecycle state for MiniApp [=Document/unloading=]. It is triggered once the MiniApp is unloaded.

The mapping to existing Web specifications such as Service Workers is listed in MiniApp Lifecycle Explainer.

GlobalState enum

The MiniApp [=Document/global application lifecycle states=] are reflected in the API via the {{GlobalState}} enum.

          enum GlobalState {
              "launched", "shown", "hidden", "error", "unloaded"
          };
          

The GlobalState enum is used to represent the [=Document/global application lifecycle states=].

The "launched" enum value represents the [=Document/launched=] [=Document/global application lifecycle states=].

The "shown" enum value represents the [=Document/shown=] [=Document/global application lifecycle states=].

The "hidden" enum value represents the [=Document/hidden=] [=Document/global application lifecycle states=].

The "error" enum value represents the [=Document/error=] [=Document/global application lifecycle states=].

The "unloaded" enum value represents the [=Document/unloaded=] [=Document/global application lifecycle states=].

MiniApp Global Application Lifecycle interface

           [Exposed=Window]
           interface Global {
            readonly attribute GlobalState globalState;
            readonly attribute InputObject inputObject;
            readonly attribute LifecycleError lifecycleError;
            attribute EventHandler ongloballaunched;
            attribute EventHandler onglobalshown;
            attribute EventHandler onglobalhidden;
            attribute EventHandler onglobalerror;
            attribute EventHandler onglobalunloaded;
           };

         

`globalState` attribute

On getting, the globalState attribute MUST return {{GlobalState}}.

`inputObject` attribute

The inputObject contains the basic information of the MiniApp, such as page path for the MiniApp, source info for the MiniApp.

`lifecycleError` attribute

The lifecycleError contains error indication information for [=Document/in error=] event type .

`ongloballaunched` event handler attribute

The ongloballaunched attribute is an event handler IDL attribute for the [=Document/initialization=] event type. Once this event is triggered, globalState will be set to [=Document/launched=].

`onglobalshown` event handler attribute

The onglobalshown attribute is an event handler IDL attribute for the [=Document/running in foreground=] event type. Once this event is triggered, globalState will be set to [=Document/shown=].

`onglobalhidden` event handler attribute

The onglobalhidden attribute is an event handler IDL attribute for the [=Document/running in background=] event type. Once this event is triggered, globalState will be set to [=Document/hidden=].

`onglobalerror` event handler attribute

The onglobalerror attribute is an event handler IDL attribute for the [=Document/in error=] event type. Once this event is triggered, globalState will be set to [=Document/error=].

`onglobalunloaded` event handler attribute

The onglobalunloaded attribute is an event handler IDL attribute for the [=Document/unloading=] event type. Once this event is triggered, globalState will be set to [=Document/unloaded=].

`InputObject` interface

                [Exposed=Window]
                interface InputObject {
                    readonly attribute DOMString pagePath;
                    readonly attribute DOMString referrerInfo;
                    readonly attribute DOMString lang;
                    readonly attribute TextDirection dir;
                };
            
TextDirection enum

                    enum TextDirection {    
                        "auto",
                        "ltr",    
                        "rtl"
                    };
                

The text-direction values are the following, implying that the value of the human-readable members is by default:

auto
Directionality is determined by the Unicode Bidirectional Algorithm [[UAX9]] algorithm.
ltr
Left-to-right text.
rtl
Right-to-left text.

`pagePath` attribute

On getting, the pagePath attribute MUST return the page path for current MiniApp.

`referrerInfo` attribute

The referrerInfo attribute contains the source info for the MiniApp, including MiniApp ID, and optional extra data including human readable value.

`lang` attribute

The value of the lang attribute MUST be a well-formed language tag as defined by [[BCP47]].

`dir` attribute

The dir attribute specifies the base direction for the values of the `referrerInfo` attribute if it contains human readable value.

`LifecycleError` interface

                [Exposed=Window]
                interface LifecycleError {
                    readonly attribute DOMString errorDescription;
                    readonly attribute DOMString lang;
                    readonly attribute TextDirection dir;
                };
            

`errorDescription` attribute

The errorDescription attribute is a developer-friendly textual description of the [=Document/error=] [=Document/global application lifecycle states=].

`lang` attribute

The value of the lang attribute MUST be a well-formed language tag as defined by [[BCP47]].

`dir` attribute

The dir attribute specifies the base direction for the values of the `errorDescription` attribute.

MiniApp Page Lifecycle

MiniApp Page Lifecycle Events

This spec defines what the lifecycle of a MiniApp page is and adds definition to enable MiniApp to respond to five important page lifecycle events commonly performed by MiniApp:

  1. When user firstly opens a MiniApp, the MiniApp [=Document/initialization=] starts. [=Document/View layer=] and [=Document/Logic layer=] will simultaneously start the initialization.

  2. Once [=Document/Logic layer=] initialization is completed, it creates a MiniApp instance. Simultaneously, once [=Document/View layer=] initialization is completed, it starts the MiniApp [=Document/page loading=], to establish communication channel between [=Document/View layer=] and [=Document/Logic layer=].

  3. After the communication channel is established, [=Document/Logic layer=] sends the initial data to [=Document/View layer=] to start the [=Document/first render=].

  4. If [=Document/View layer=] completes the page UI update based on the inputted initial data from [=Document/Logic layer=], the [=Document/first render=] is considered as completed, and then [=Document/View layer=] notifies [=Document/Logic layer=], to trigger the MiniApp [=Document/page first render ready=].

  5. Afterwards, user can interact with MiniApp. [=Document/View layer=] can be triggered to deliver user event to [=Document/Logic layer=] for further processing, then [=Document/Logic layer=] returns result data to [=Document/View layer=] for re-render.

  6. If user leaves the current MiniApp page (e.g., by navigating to another MiniApp page), MiniApp [=Document/page running in background=] is triggered. If the MiniApp page is reopened, MiniApp [=Document/page running in foreground=] is triggered.

  7. If user closes the current MiniApp page, MiniApp [=Document/page unloading=] is triggered.

MiniApp Page Lifecycle States

This spec formalizes five MiniApp page lifecycle states to support the above [=Document/page lifecycle events=]:

page loaded
Lifecycle state for MiniApp [=Document/page loading=]. This means that MiniApp [=Document/page loading=] is completed. At this moment, [=Document/Logic layer=] has obtained initialization data, and developer can obtain the path of current MiniApp page as well as the path’s query.
page ready
Lifecycle state for MiniApp [=Document/page first render ready=]. It is triggered once the MiniApp page [=Document/first render=] is completed. At this moment, the page UI can be configured.
page shown
Lifecycle state for MiniApp [=Document/page running in foreground=]. It is triggered once the page switches to be [=Document/page running in foreground=] from [=Document/page running in background=]. At this moment, developer can update data and refresh page.
page hidden
Lifecycle state for MiniApp [=Document/page running in background=]. It is triggered once the MiniApp page switches to be [=Document/page running in foreground=] to [=Document/page running in background=].
page unloaded
Lifecycle state for MiniApp [=Document/page unloading=]. It is triggered once the MiniApp page is closed.

The mapping to existing Web specifications such as Page Visibility is listed in MiniApp Lifecycle Explainer.

PageState enum

The MiniApp [=Document/page lifecycle states=] are reflected in the API via the {{PageState}} enum.

          enum PageState {
              "loaded", "ready", "shown", "hidden", "unloaded"
          };
          

The PageState enum is used to represent the MiniApp [=Document/page lifecycle states=].

The "loaded" enum value represents the [=Document/page loaded=] [=Document/page lifecycle states=].

The "ready" enum value represents the [=Document/page ready=] [=Document/page lifecycle states=].

The "shown" enum value represents the [=Document/page shown=] [=Document/page lifecycle states=].

The "hidden" enum value represents the [=Document/page hidden=] [=Document/page lifecycle states=].

The "unloaded" enum value represents the [=Document/page unloaded=] [=Document/page lifecycle states=].

MiniApp Page Lifecycle interface

           [Exposed=Window]
           interface Page {
               readonly attribute PageState pageState;
               readonly attribute PageInputObject pageInputObject;
               attribute EventHandler onpageloaded;
               attribute EventHandler onpageready;
               attribute EventHandler onpageshown;
               attribute EventHandler onpagehidden;
               attribute EventHandler onpageunloaded;
           };

         

`pageState` attribute

On getting, the pageState attribute MUST return {{PageState}}.

`pageInputObject` attribute

The pageInputObject contains pageInputQuery for loading the MiniApp page.

`onpageloaded` event handler attribute

The onpageloaded attribute is an event handler IDL attribute for the [=Document/page loading=] event type. Once this event is triggered, pageState will be set to [=Document/page loaded=].

`onpageready` event handler attribute

The onpageready attribute is an event handler IDL attribute for the [=Document/page first render ready=] event type. Once this event is triggered, pageState will be set to [=Document/page ready=].

`onpageshown` event handler attribute

The onpageshown attribute is an event handler IDL attribute for the [=Document/page running in foreground=] event type. Once this event is triggered, pageState will be set to [=Document/page shown=].

`onpagehidden` event handler attribute

The onpagehidden attribute is an event handler IDL attribute for the [=Document/page running in background=] event type. Once this event is triggered, pageState will be set to [=Document/page hidden=].

`onpageunloaded` event handler attribute

The onpageunloaded attribute is an event handler IDL attribute for the [=Document/page unloading=] event type. Once this event is triggered, pageState will be set to [=Document/page unloaded=].

`PageInputObject` interface

                [Exposed=Window]
                interface PageInputObject {
                    readonly attribute DOMString pageInputQuery;
                };
            

`pageInputQuery` attribute

The pageInputQuery attribute contains inputted query for the MiniApp page.

Usage example

Example of handling MiniApp global lifecycles:

      
            const doGlobalLaunched = (inputObject) => {
                console.log(inputObject.pagePath);
            };

            global.addEventListener('globallaunched', doGlobalLaunched);
            
      

Example of handling MiniApp page lifecycles:

      
            const doPageLoaded = (pageInputObject) => {
                console.log(pageInputObject.pageInputQuery);
            };

            page.addEventListener('pageloaded', doPageLoaded);
            
      

Security Considerations

MiniApp [=Document/running in foreground=] and [=Document/running in background=] event enables developer to know when a MiniApp is visible. By use of [=Document/page running in foreground=] event, developer can choose to process and hide the sensitive data, before MiniApp page switches to be [=Document/page running in foreground=]; the [=Document/page unloaded=] event provides a notification that the page is being unloaded.

Privacy Considerations

MiniApp Global Application Lifecycle interface {{Global}} introduces {{inputObject}} which involves the processing of the inputted query for the MiniApp, the page path for current MiniApp, and the source info. MiniApp Page Lifecycle interface {{Page}} introduces {{pageInputObject}} which involves the processing of the inputted query for the MiniApp page. In order to protect users from any potential unsanctioned tracking threat, it is not recommended to store these values locally. If these values are stored, these storages should be cleared when users intend to clear them.

If the inputted query for the MiniApp or the inputted query for the MiniApp page contains privacy-sensitive information (e.g. user personal data), the privacy-sensitive information shall not be in cleartext.

Accessibility Considerations

MiniApp Lifecycle involves (sometimes) responding to user interaction, e.g., {{GlobalState}} indicates whether the MiniApp is [=Document/shown=] or [=Document/hidden=]; {{PageState}} indicates whether the MiniApp page is [=Document/page shown=] or [=Document/page hidden=]. The user agent should adequately communicate with the accessibility services of the MiniApp lifecycle states, e.g., on getting, the {{GlobalState}} and the {{PageState}} should return MiniApp Global Application Lifecycle states and MiniApp Page Lifecycle states correspondingly to the accessibility services.