Copyright © 2021 W3C® (MIT, ERCIM, Keio, Beihang). W3C liability, trademark and permissive document license rules apply.
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 global application lifecycle and page lifecycle.
This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at https://www.w3.org/TR/.
This document was published by the MiniApps Working Group as a First Public Working Draft. This document is intended to become a W3C Recommendation.
GitHub Issues are preferred for discussion of this specification.
Publication as a First Public Working Draft does not imply endorsement by the W3C Membership.
This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.
This document was produced by a group operating under the W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
This document is governed by the 15 September 2020 W3C Process Document.
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 global application lifecycle events and MiniApp page lifecycle events. Developing MiniApp with knowledge of the MiniApp global application lifecycle states and MiniApp 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.
This spec defines what the lifecycle of a MiniApp global application is and adds definition to enable MiniApp to respond to four important global application lifecycle events commonly performed by MiniApp:
MiniApp initialization
MiniApp running in foreground
MiniApp running in background
MiniApp in error
For each MiniApp, after initialization, it will be either running in foreground or 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 running in background.
When user reopens the same MiniApp, MiniApp will switch from running in background to running in foreground.
Only when MiniApp runs in the background for more than a specific time duration (e.g., 5 minutes), or occupies too much system resources in the background, the MiniApp will be destroyed.
This spec formalizes four global application lifecycle states to support the above global application lifecycle events:
GlobalState
enum
The MiniApp global application lifecycle states are reflected in the API via the GlobalState
enum.
WebIDLenum GlobalState
{
"launched
", "shown
", "hidden
", "error
"
};
The GlobalState
enum is used to represent the
global application lifecycle states.
The "launched
" enum value represents the launched
global application lifecycle states.
The "shown
" enum value represents the shown
global application lifecycle states.
The "hidden
" enum value represents the
global application lifecycle states.
The "error
" enum value represents the error
global application lifecycle states.
WebIDL [Exposed=Window]
interface Global
{
};
callback GlobalLaunchedCallback
= undefined (
InputObject
inputObject
);
callback GlobalShownCallback
= undefined (
InputObject
inputObject
);
callback GlobalErrorCallback
= undefined (
LifecycleError
lifecycleError
);
getGlobalState()
method
When getGlobalState()
is invoked, the user agent MUST:
undefined
.
globalLaunchedCallback
If MiniApp's first page is ready to load, invoke GlobalLaunchedCallback
.
globalShownCallback
If MiniApp enters the shown global application lifecycle states, or if MiniApp switches to be running in foreground from running in background, invoke GlobalShownCallback
.
globalErrorCallback
If MiniApp MiniApp is confronted with script error, invoke GlobalErrorCallback
.
InputObject
interface
WebIDL[Exposed=Window]
interface InputObject
{
readonly attribute DOMString inputQuery
;
readonly attribute DOMString pagePath
;
readonly attribute DOMString referrerInfo
;
};
inputQuery
attribute
The inputQuery
attribute contains inputted query for the MiniApp.
pagePath
attribute
The pagePath
attribute contains 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.
LifecycleError
interface
WebIDL[Exposed=Window]
interface LifecycleError
{
readonly attribute DOMString errorDescription
;
};
errorDescription
attribute
The errorDescription
attribute is a developer-friendly textual description of the error global application lifecycle states.
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:
MiniApp page loading
MiniApp page first render ready
MiniApp page running in foreground
MiniApp page running in background
MiniApp page unloading
When user firstly opens a MiniApp, the MiniApp initialization starts. View layer and Logic layer will simultaneously start the initialization.
Once Logic layer initialization is completed, it creates a MiniApp instance. Simultaneously, once View layer initialization is completed, it starts the MiniApp page loading, to establish communication channel between View layer and Logic layer.
After the communication channel is established, Logic layer sends the initial data to View layer to start the first render.
If View layer completes the page UI update based on the inputted initial data from Logic layer, the first render is considered as completed, and then View layer notifies Logic layer, to trigger the MiniApp page first render ready.
Afterwards, user can interact with MiniApp. View layer can be triggered to deliver user event to Logic layer for further processing, then Logic layer returns result data to View layer for re-render.
If user leaves the current MiniApp page (e.g., by navigating to another MiniApp page), MiniApp page running in background is triggered. If the MiniApp page is reopened, MiniApp page running in foreground is triggered.
If user closes the current MiniApp page, MiniApp page unloading is triggered.
This spec formalizes five MiniApp page lifecycle states to support the above page lifecycle events:
PageState
enum
The MiniApp page lifecycle states are reflected in the API via the PageState
enum.
WebIDLenum PageState
{
"loaded
", "ready
", "shown
", "hidden
", "unloaded
"
};
The PageState
enum is used to represent the MiniApp page lifecycle states.
The "loaded
" enum value represents the page loaded
page lifecycle states.
The "ready
" enum value represents the page ready
page lifecycle states.
The "shown
" enum value represents the page shown
page lifecycle states.
The "hidden
" enum value represents the
page lifecycle states.
The "unloaded
" enum value represents the page unloaded
page lifecycle states.
WebIDL [Exposed=Window]
interface Page
{
};
callback PageLoadedCallback
= undefined (
PageInputObject
pageInputObject
);
callback PageReadyCallback
= undefined (
);
callback PageShownCallback
= undefined (
);
callback PageUnloadedCallback
= undefined (
);
getPageState()
method
When getPageState()
is invoked, the user agent MUST:
undefined
.
pageLoadedCallback
If a communication channel has been established between View layer and Logic layer, invoke PageLoadedCallback
.
pageReadyCallback
If MiniApp page first render is completed, invoke PageReadyCallback
.
pageShownCallback
If MiniApp page switches to be page running in foreground from page running in background, invoke PageShownCallback
.
pageUnloadedCallback
If MiniApp page is closed, invoke PageUnloadedCallback
.
PageInputObject
interface
WebIDL[Exposed=Window]
interface PageInputObject
{
readonly attribute DOMString pageInputQuery
;
};
pageInputQuery
attribute
The pageInputQuery
attribute contains inputted query for the MiniApp page.
This section is non-normative.
MiniApp running in foreground and running in background event enables developer to know when a MiniApp is visible. By use of page running in foreground event, developer can choose to process and hide the sensitive data, before MiniApp page switches to be page running in foreground; the page unloaded event provides a notification that the page is being unloaded.
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in: