WebView Site
A WebView site is a static file site published alongside the miniapp. It reduces the burden of deploying static HTML files for developers and supports offline mode. Developers can use the WebView component in the miniapp to load the WebView site, enabling seamless integration between the miniapp and the WebView site.
Features
- Supports offline mode for improved access speed
- Supports bidirectional communication with the miniapp logic layer
Usage
1. Declare the WebView site directory
Declare the webviewRoot field in the tuya export of src/global.config.ts to specify the WebView site directory:
The miniapp will deploy using the directory specified by webviewRoot to serve the WebView site files.
Directory structure:
Note: The
webviewRootfield can only be declared once, using a relative path. Absolute paths are not supported.
2. Use the WebView component to load the WebView site
The site protocol is webview://. Use the WebView component in a Ray page:
The src attribute value starts with webview://, followed by the directory specified by webviewRoot and the path to the WebView site file.
onMessage: Communication between the WebView site and the miniapp logic layeronLoad: Triggered when the WebView site loads successfullyonError: Triggered when the WebView site fails to load
3. Communication between the WebView site and the logic layer
Communication between the WebView site and the miniapp logic layer is implemented via @tuya-miniapp/jssdk.
Use ty.createWebviewContext in a Ray page to send messages to the WebView site:
Supported File Types
Supported file extensions: .jpg .jpeg .png .gif .bmp .ico .tiff .svg .ttf .woff .woff2 .json .html .htm .js .css .mp3 .mp4
While the WebView site supports many file types, not all files are suitable for inclusion. Developers should select appropriate file types and sizes based on actual requirements. Excessively large files may slow down miniapp loading speeds and degrade the user experience, or may fail to upload to the miniapp platform.
Requirements
- Tuya MiniApp IDE
>= 0.5.10 - Base library version
>= 2.14.2 - Container version
>= 3.12.0 - @tuya-miniapp/jssdk
>= 0.1.2
Notes
- Files in the WebView site directory do not support miniapp components such as
ViewandScrollView. Only static HTML tags are supported. - Developers must ensure the legality of WebView site content. Content that is illegal, pornographic, or violent is prohibited, or the miniapp may be banned from the platform.
- JavaScript files must be secure and must not contain malicious code, or the miniapp may be banned from the platform.
- JavaScript files must ensure compatibility, for example by using ES5 syntax instead of ES6 syntax, to avoid compatibility issues on mobile browsers.