Displaying Web Pages in an Instant Manner
Overview
This sample shows how to display the web pages in an instant manner by using pre-rendering.
Preview
| web |
|---|
![]() |
Instructions:
- Tap the title to open the dialog box and load the web page.
Project Directory
├──entry/src/main/ets/
│ ├──common
│ │ └──Constants.ets // Common constant class
│ ├──entryability
│ │ └──EntryAbility.ets // Entry ability
│ └──pages
│ └──Index.ets // Home page
│ └──WebPage.ets // Pre-rendering web page
└──entry/src/main/resources // Static resources
How to Implement
This sample leverages the NodeController and BuilderNode capabilities of ArkUI to create and load the Web component in the background right after the home page (Index) is loaded but before the user taps, so that the Web page can be displayed instantly when the dialog opens.
- Define the
WebBuilderbuilder function inWebPage.ets, which creates aWebcomponent and associates it with aWebviewControllerto host the page to be pre-rendered. - Wrap
WebBuilderwithwrapBuilderso that it can be called by thebuildmethod ofBuilderNode. - Customize
MyNodeController, which extendsNodeController, and override themakeNodemethod to return theFrameNodeof theBuilderNodeas the rendering carrier of the node. - In
EntryAbility.onWindowStageCreate, afterloadContentfinishes loading the home page, callcreateNWeb(Constants.WEB_URL, windowStage.getMainWindowSync().getUIContext()):- Create a
BuilderNodeinstance and call itsbuildmethod to buildWebBuilderinto a real component node, completing page loading and rendering in advance (pre-rendering). - Store the corresponding
MyNodeControllerandWebviewControllerinto the globalNodeMapandcontrollerMapkeyed by URL.
- Create a
- On the home page
Index.ets, bind a dialog withbindSheet, and useNodeContainer(getNWeb(Constants.WEB_URL))as the dialog content:getNWebretrieves the pre-renderedMyNodeControllerfromNodeMapand mounts it ontoNodeContainer, displaying the already-rendered Web page without any loading wait.
- Tap the text area on the home page to toggle the
isShowSheetstate and open the dialog, and the pre-rendered Web page is displayed instantly.
Key API Description
- NodeController: An imperative view controller provided by ArkUI for creating and managing custom component nodes. Extend this class and override
makeNode(uiContext: UIContext): FrameNode | nullto return the node to be mounted. In this sample, it returns theFrameNodeof theBuilderNode, allowing the pre-rendered Web page to be dynamically mounted anywhere in the component tree. - BuilderNode: A node that hosts the component node created by a
@Builderbuilder function. Create an instance withnew BuilderNode(uiContext), then callbuild(wrapperBuilder, params)with thewrapBuilder-wrapped builder function and parameters to build the component in advance in non-UI code (such as an Ability), which is the key capability for pre-rendering. - NodeContainer: Used to mount the node returned by a
NodeControllerin the component tree. PlaceNodeContainer(controller)anywhere in a page to display the pre-rendered content, achieving "render first, display later". - wrapBuilder: A wrapper for
@Builderbuilder functions so that they can be passed to and called byBuilderNode.build, enabling cross-context passing and invocation. - WebviewController: A controller that manages a
Webcomponent, used for navigation control such as back, forward, and refresh. In this sample, it is created and associated with theWebcomponent during pre-rendering.
Required Permissions
- Internet access permission: ohos.permission.INTERNET.
Dependencies
None
Constraints
-
The sample app is only supported on Bar phones with standard systems.
-
The HarmonyOS version must be HarmonyOS 5.0.5 Release or later.
-
The DevEco Studio version must be DevEco Studio 5.0.5 Release or later.
-
The HarmonyOS SDK version must be HarmonyOS 5.0.5 Release or later.
