Copyright © 2021-2022 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 the global application lifecycle and the page lifecycle.
This section describes the status of this document at the time of its publication. 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 an Editor's Draft.
Publication as an Editor's Draft does not imply endorsement by W3C and its Members.
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 2 November 2021 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 :
The mapping to existing Web specifications such as Service Workers is listed in MiniApp Lifecycle Explainer .
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 {
readonly attribute GlobalState globalState;
readonly attribute InputObject inputObject;
readonly attribute LifecycleError lifecycleError;
attribute EventHandler ongloballaunched;
attribute EventHandler onglobalshown;
attribute EventHandler onglobalerror;
};
On
getting,
the
globalState
attribute
MUST
return
GlobalState
.
The
inputObject
contains
the
basic
information
of
the
MiniApp,
such
as
page
path
for
the
MiniApp,
source
info
for
the
MiniApp.
The
lifecycleError
contains
error
indication
information
for
in
error
event
type
.
The
ongloballaunched
attribute
is
an
event
handler
IDL
attribute
for
the
initialization
event
type.
Once
this
event
is
triggered,
globalState
will
be
set
to
launched
.
The
onglobalshown
attribute
is
an
event
handler
IDL
attribute
for
the
running
in
foreground
event
type.
Once
this
event
is
triggered,
globalState
will
be
set
to
shown
.
The
onglobalerror
attribute
is
an
event
handler
IDL
attribute
for
the
in
error
event
type.
Once
this
event
is
triggered,
globalState
will
be
set
to
error
.
WebIDL[Exposed=Window]
interface InputObject {
readonly attribute DOMString pagePath;
readonly attribute DOMString referrerInfo;
readonly attribute DOMString lang;
readonly attribute TextDirection dir;
};
TextDirection
enum
WebIDLenum TextDirection {
"auto",
"ltr",
"rtl"
};
The text-direction values are the following, implying that the value of the human-readable members is by default:
auto
ltr
rtl
On
getting,
the
pagePath
attribute
MUST
return
the
page
path
for
current
MiniApp.
The
referrerInfo
attribute
contains
the
source
info
for
the
MiniApp,
including
MiniApp
ID,
and
optional
extra
data
including
human
readable
value.
The
value
of
the
lang
attribute
MUST
be
a
well-formed
language
tag
as
defined
by
[
BCP47
].
The
dir
attribute
specifies
the
base
direction
for
the
values
of
the
referrerInfo
attribute
if
it
contains
human
readable
value.
WebIDL[Exposed=Window]
interface LifecycleError {
readonly attribute DOMString errorDescription;
readonly attribute DOMString lang;
readonly attribute TextDirection dir;
};
The
errorDescription
attribute
is
a
developer-friendly
textual
description
of
the
error
global
application
lifecycle
states
.
The
value
of
the
lang
attribute
MUST
be
a
well-formed
language
tag
as
defined
by
[
BCP47
].
The
dir
attribute
specifies
the
base
direction
for
the
values
of
the
errorDescription
attribute.
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 :
The mapping to existing Web specifications such as Page Visibility is listed in MiniApp Lifecycle Explainer .
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 {
readonly attribute PageState pageState;
readonly attribute PageInputObject pageInputObject;
attribute EventHandler onpageloaded;
attribute EventHandler onpageready;
attribute EventHandler onpageshown;
attribute EventHandler onpageunloaded;
};
On
getting,
the
pageState
attribute
MUST
return
PageState
.
The
pageInputObject
contains
inputted
query
pageInputQuery
for
loading
the
MiniApp
page.
The
onpageloaded
attribute
is
an
event
handler
IDL
attribute
for
the
page
loading
event
type.
Once
this
event
is
triggered,
pageState
will
be
set
to
page
loaded
.
The
onpageready
attribute
is
an
event
handler
IDL
attribute
for
the
page
first
render
ready
event
type.
Once
this
event
is
triggered,
pageState
will
be
set
to
page
ready
.
The
onpageshown
attribute
is
an
event
handler
IDL
attribute
for
the
page
running
in
foreground
event
type.
Once
this
event
is
triggered,
pageState
will
be
set
to
page
shown
.
The
onpageunloaded
attribute
is
an
event
handler
IDL
attribute
for
the
page
unloading
event
type.
Once
this
event
is
triggered,
pageState
will
be
set
to
page
unloaded
.
WebIDL[Exposed=Window]
interface PageInputObject {
readonly attribute DOMString pageInputQuery;
};
The
pageInputQuery
attribute
contains
inputted
query
for
the
MiniApp
page.
This section is non-normative.
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);
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.
This section is non-normative.
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.
This section is non-normative.
MiniApp
Lifecycle
involves
(sometimes)
responding
to
user
interaction,
e.g.,
GlobalState
indicates
whether
the
MiniApp
is
shown
or
;
PageState
indicates
whether
the
MiniApp
page
is
page
shown
or
.
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.
Referenced in:
Referenced in: