![Thomas Steiner](https://web.dev/images/authors/thomassteiner.jpg) Thomas Steiner [GitHub](https://github.com/tomayac) [LinkedIn](https://www.linkedin.com/in/thomassteinerlinkedin) [Mastodon](https://toot.cafe/@tomayac) [Bluesky](https://bsky.app/profile/tomayac.com) [Homepage](https://blog.tomayac.com/)

<br />

> [!NOTE]
> **Note:** This post is part of an article collection where each article builds upon previous articles. If you just landed here, you may want to start reading from the [beginning](https://web.dev/articles/mini-apps/mini-app-super-apps.md.txt).

## Markup languages

As outlined before, rather than with plain HTML, mini apps are written with dialects of HTML. If you
have ever dealt with [Vue.js](https://vuejs.org/) text interpolation and directives, you will feel
immediately at home, but similar concepts existed way before that in XML Transformations
([XSLT](https://www.w3.org/TR/xslt-30/)). Below, you can see code samples from WeChat's
[WXML](https://developers.weixin.qq.com/miniprogram/en/dev/framework/view/wxml/), but the concept is
the same for all mini apps platforms, namely Alipay's
[AXML](https://opendocs.alipay.com/mini/framework/axml), Baidu's
[Swan Element](https://smartprogram.baidu.com/docs/develop/framework/dev/), ByteDance's
[TTML](https://microapp.bytedance.com/docs/zh-CN/mini-app/develop/guide/mini-app-framework/view/ttml)
(despite the DevTools calling it Bxml), and Quick App's
[HTML](https://doc.quickapp.cn/tutorial/framework/for.html). Just like with Vue.js, the underlying
mini app programming concept is the
[model-view-viewmodel](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel) (MVVM).

### Data binding

Data binding corresponds to Vue.js'
[text interpolation](https://vuejs.org/v2/guide/syntax.html#Text).

```html
<!-- wxml -->
<view>{{message}}</view>
```

```javascript
// page.js
Page({
  data: {
    message: "Hello World!",
  },
});
```

### List rendering

List rendering works like Vue.js [`v-for` directive](https://vuejs.org/v2/guide/list.html).

```html
<!-- wxml -->
<view wx:for="{{array}}">{{item}}</view>
```

```javascript
// page.js
Page({
  data: {
    array: [1, 2, 3, 4, 5],
  },
});
```

### Conditional rendering

Conditional rendering works like Vue.js'
[`v-if` directive](https://vuejs.org/v2/guide/conditional.html).

```html
<!-- wxml -->
<view wx:if="{{view == 'one'}}">One</view>
<view wx:elif="{{view == 'two'}}">Two</view>
<view wx:else="{{view == 'three'}}">Three</view>
```

```javascript
// page.js
Page({
  data: {
    view: "three",
  },
});
```

### Templates

Rather than requiring the imperative
[cloning of the `content` of an HTML template](https://developer.mozilla.org/docs/Web/API/HTMLTemplateElement/content),
WXML templates can be used declaratively via the `is` attribute that links to a template definition.

```html
<!-- wxml -->
<template name="person">
  <view>
    First Name: {{firstName}}, Last Name: {{lastName}}
  </view>
</template>
```

```html
<template is="person" data="{{...personA}}"></template>
<template is="person" data="{{...personB}}"></template>
<template is="person" data="{{...personC}}"></template>
```

```javascript
// page.js
Page({
  data: {
    personA: { firstName: "Alice", lastName: "Foo" },
    personB: { firstName: "Bob", lastName: "Bar" },
    personC: { firstName: "Charly", lastName: "Baz" },
  },
});
```

## Styling

Styling happens with dialects of CSS. WeChat's is named
[WXSS](https://developers.weixin.qq.com/miniprogram/en/dev/framework/quickstart/code.html#WXSS-Style).
For Alipay, theirs is called [ACSS](https://opendocs.alipay.com/mini/framework/acss), Baidu's simply
[CSS](https://smartprogram.baidu.com/docs/develop/framework/view_css/), and for ByteDance, their
dialect is referred to as
[TTSS](https://microapp.bytedance.com/docs/zh-CN/mini-app/develop/guide/mini-app-framework/view/ttss).
What they have in common is that they extend CSS with responsive pixels. When writing regular CSS,
developers need to convert all pixel units to adapt to different mobile device screens with
different widths and pixel ratios. TTSS supports the `rpx` unit as its underlying layer, which means
the mini app takes over the job from the developer and converts the units on their behalf, based on
a specified screen width of `750rpx`. For example, on a Pixel 3a phone with a screen width of
`393px` (and a device pixel ratio of `2.75`), responsive `200rpx` become `104px` on the real device
when inspected with Chrome DevTools (393px / 750rpx \* 200rpx ≈ 104px). In Android, the same concept
is called
[density-independent pixel](https://developer.android.com/training/multiscreen/screendensities#TaskUseDP).
![Inspecting a view with Chrome DevTools whose responsive pixel padding was specified with `200rpx` shows that it is actually `104px` on a Pixel 3a device.](https://web.dev/static/articles/mini-apps/mini-app-markup-styling-and-scripting/image/inspecting-view-chrome.png) Inspecting the actual padding on a Pixel 3a device with Chrome DevTools.

```css
/* app.wxss */
.container {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 200rpx 0; /* ← responsive pixels */
  box-sizing: border-box;
}
```

Since components (see [later](https://web.dev/articles/mini-apps/mini-app-components.md.txt)) do not use shadow DOM, styles declared on a page reach
into all components. The common stylesheet file organization is to have one root stylesheet for
global styles, and individual per-page stylesheets specific to each page of the mini app. Styles can
be imported with an `@import` rule that behaves like the
[`@import`](https://developer.mozilla.org/docs/Web/CSS/@import) CSS at-rule. Like in HTML,
styles can also be declared inline, including dynamic text interpolation (see
[before](https://web.dev/articles/mini-apps/mini-app-markup-styling-and-scripting.md.txt#data_binding)).

```html
<view style="color:{{color}};" />
```

## Scripting

Mini apps support a "safe subset" of JavaScript that includes support for modules using varying
syntaxes that remind of [CommonJS](http://www.commonjs.org/) or [RequireJS](https://requirejs.org/).
JavaScript code cannot be executed via `eval()` and no functions can be created with
`new Function()`. The scripting execution context is [V8](https://v8.dev/) or
[JavaScriptCore](https://developer.apple.com/documentation/javascriptcore) on devices, and V8 or
[NW.js](https://nwjs.io/) in the simulator. Coding with ES6 or newer syntax is usually possible,
since the particular DevTools automatically transpile the code to ES5 if the build target is an
operating system with an older WebView implementation (see [later](https://web.dev/articles/mini-apps/mini-app-project-structure-lifecycle-and-bundling.md.txt#the_build_process)). The
documentation of the super app providers explicitly mentions that their scripting languages are not
to be confused with and are distinct from JavaScript. This statement, however, refers mostly just to
the way modules work, that is, that they do not support standard
[ES Modules](https://developer.mozilla.org/docs/Web/JavaScript/Guide/Modules) yet.

As mentioned [before](https://web.dev/articles/mini-apps/mini-app-markup-styling-and-scripting.md.txt#markup_languages), the mini app programming concept is the
[model-view-viewmodel](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel) (MVVM).
The logic layer and the view layer run on different threads, which means the user interface does not
get blocked by long-running operations. In web terms, you can think of scripts running in a
[Web Worker](https://developer.mozilla.org/docs/Web/API/Web_Workers_API/Using_web_workers).

WeChat's scripting language is called
[WXS](https://developers.weixin.qq.com/miniprogram/en/dev/reference/wxs/), Alipay's
[SJS](https://opendocs.alipay.com/mini/framework/sjs), ByteDance's likewise
[SJS](https://microapp.bytedance.com/docs/zh-CN/mini-app/develop/framework/sjs-syntax-reference/sjs-introduction/).
Baidu speaks of [JS](https://smartprogram.baidu.com/docs/develop/framework/devjs/) when referencing
theirs. These scripts need to be included using a special kind of tag, for example, `<wxs>` in
WeChat. In contrast, Quick App uses regular `<script>` tags and the ES6
[JS](https://doc.quickapp.cn/framework/script.html) syntax.

```html
<wxs module="m1">
  var msg = "hello world";
  module.exports.message = msg;
</wxs>

<view>{{m1.message}}</view>
```

Modules can also be loaded via a `src` attribute, or imported via `require()`.

```javascript
// /pages/tools.wxs
var foo = "'hello world' from tools.wxs";
var bar = function (d) {
  return d;
};
module.exports = {
  FOO: foo,
  bar: bar,
};
module.exports.msg = "some msg";
```

```html
<!-- page/index/index.wxml -->
<wxs src="./../tools.wxs" module="tools" />
<view>{{tools.msg}}</view>
<view>{{tools.bar(tools.FOO)}}</view>
```

```javascript
// /pages/logic.wxs
var tools = require("./tools.wxs");

console.log(tools.FOO);
console.log(tools.bar("logic.wxs"));
console.log(tools.msg);
```

### JavaScript bridge API

The JavaScript bridge that connects mini apps with the operating system makes it possible
to use OS capabilities (see [Access to powerful features](https://web.dev/articles/mini-apps/mini-app-about.md.txt#access_to_powerful_features). It
also offers a number of convenience methods. For an overview, you can check out the different APIs
of [WeChat](https://developers.weixin.qq.com/miniprogram/en/dev/api/),
[Alipay](https://opendocs.alipay.com/mini/api),
[Baidu](https://smartprogram.baidu.com/docs/develop/api/apilist/),
[ByteDance](https://microapp.bytedance.com/docs/zh-CN/mini-app/develop/api/foundation/tt-can-i-use),
and [Quick App](https://doc.quickapp.cn/features/).

Feature detection is straightforward, since all platforms provide a (literally called like this)
`canIUse()` method whose name seems inspired by the website [caniuse.com](https://caniuse.com/). For
example, ByteDance's
[`tt.canIUse()`](https://microapp.bytedance.com/docs/zh-CN/mini-app/develop/api/foundation/tt-can-i-use)
allows for support checks for APIs, methods, parameters, options, components, and attributes.

```javascript
// Testing if the `<swiper>` component is supported.
tt.canIUse("swiper");
// Testing if a particular field is supported.
tt.canIUse("request.success.data");
```

> [!TIP]
> **Success:** The next chapter introduces [mini app components](https://web.dev/articles/mini-apps/mini-app-components.md.txt).

## Updates

Mini apps do not have a standardized update mechanism ([discussion around potential standardization](https://github.com/w3c/miniapp-packaging/issues/28)).
All mini app platforms have a backend system, which allows mini app developers to upload new versions of
their mini apps. A super app then uses that backend system to check for and download updates. Some super apps
perform updates entirely in the background, without any way for the mini app itself to influence the update
flow. Other super apps give more control to the mini apps themselves.

As an example of a sophisticated process, the following paragraphs describe [WeChat's update mechanism for mini apps](https://developers.weixin.qq.com/miniprogram/dev/framework/runtime/update-mechanism.html)
in more detail. WeChat checks for available updates in the following two scenarios:

1. WeChat will regularly check for updates of recently used mini apps as long as WeChat is running. If an update is found, the update is downloaded, and synchronously applied the next time the user cold-starts the mini app. Cold-starting a mini apps occurs when the mini app was not currently running when the user opened it (WeChat force-closes mini apps after being in the background for 5 minutes).
2. WeChat also checks for updates when a mini app is cold-started. For mini apps that the user has not opened for a long time, the update is checked for and downloaded synchronously. While the update is downloading, the user has to wait. Once the download finishes, the update is applied, and the mini app opens. If the download fails, e.g., due to bad network connectivity, the mini app opens regardless. For mini apps that the user has opened recently, any potential update is downloaded asynchronously in the background and will be applied the next time the user cold-starts the mini app.

Mini apps can opt-in to earlier updates by using the [`UpdateManager`](https://developers.weixin.qq.com/miniprogram/en/dev/api/base/update/UpdateManager.html) API. It provides the following functionality:

- Notifies the mini app when a check for updates is made. ([`onCheckForUpdate`](https://developers.weixin.qq.com/miniprogram/en/dev/api/base/update/UpdateManager.onCheckForUpdate.html))
- Notifies the mini app when an update has been downloaded and is available. ([`onUpdateReady`](https://developers.weixin.qq.com/miniprogram/en/dev/api/base/update/UpdateManager.onUpdateReady.html))
- Notifies the mini app when an update could not be downloaded. ([`onUpdateFailed`](https://developers.weixin.qq.com/miniprogram/en/dev/api/base/update/UpdateManager.onUpdateFailed.html))
- Allows the mini app to force-install an available update, which will restart the app. ([`applyUpdate`](https://developers.weixin.qq.com/miniprogram/en/dev/api/base/update/UpdateManager.applyUpdate.html))

WeChat also provides additional update customization options for mini app developers in its backend system:
1. One option allows developers to opt-out from synchronous updates for users that already have a certain
minimum version of the mini app installed, and instead forces the updates to be asynchronous.
2. Another option allows developers to set a minimum required version of their mini app. This will make
asynchronous updates from a version lower than the minimum required version force-reload the mini app after
applying the update. It will also block opening an older version of the mini app if downloading of the
update fails.

## Acknowledgements

This article was reviewed by
[Joe Medley](https://github.com/jpmedley),
[Kayce Basques](https://github.com/kaycebasques),
[Milica Mihajlija](https://github.com/mihajlija),
[Alan Kent](https://github.com/alankent),
and Keith Gu.