Skip to content

Commit 2d2814b

Browse files
committed
doc: add prerendering and preload mode.
1 parent 930f015 commit 2d2814b

File tree

6 files changed

+125
-6
lines changed

6 files changed

+125
-6
lines changed
160 KB
Loading
103 KB
Loading
125 KB
Loading

website/docs/tutorials/performance_optimization/multiple_thread_mode.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
---
2+
sidebar_position: 1
3+
title: Dedicated Thread Mode
4+
---
5+
16
# Dedicated Thread Mode
27

38
In previous versions of WebF, the JavaScript runtime was housed within the Flutter UI thread. Extended JavaScript
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
---
2+
sidebar_position: 2
3+
title: PreRendering and Preload Mode
4+
---
5+
6+
PreRendering and Preload Mode is the ultimate optimization method in WebF to speed up page load performance.
7+
8+
## Core Concepts
9+
10+
There are five phases in WebF for a page from loading to displaying the actual results:
11+
12+
1. Loading
13+
2. Exec
14+
3. Style
15+
4. Layout
16+
5. Paint
17+
18+
### Loading
19+
20+
The Loading phase indicates the time cost used by the network or disk to load necessary resources for page load, like HTML/CSS, JavaScript, or Images.
21+
22+
Loading from the network is much more flexible but takes more time.
23+
24+
On the other hand, loading from memory could be the fastest approach but often requires another strategy for additional management.
25+
26+
In the end, all resources including HTML/CSS and JavaScript will be loaded into memory.
27+
28+
### Exec
29+
30+
After all resources have been loaded, the WebF core will parse and execute them.
31+
32+
This includes parsing HTML/CSS/JavaScript and executing the JavaScript code. At this stage, the DOM Tree is created, and all CSS stylesheets are parsed into CSS style rules.
33+
34+
In the end, the JavaScript code will be fully executed, and both the DOM tree and CSS rules will have been constructed.
35+
36+
### Style
37+
38+
When the DOM tree and CSS rules are ready, in this stage, WebF matches each element with CSS rules to determine each element's style.
39+
40+
Matching CSS rules involves walking the DOM tree nodes to find how elements satisfy the CSS selector rules and parsing CSS functions and variables.
41+
42+
Different CSS rules directly affect the creation of the RenderObject, which is prepared for the next stage.
43+
44+
In the end, all elements will have an array of their resolved styles ready for the next stage.
45+
46+
### Layout
47+
48+
The layout phase in WebF is a subset of Flutter's layout phases.
49+
50+
Each RenderObject gets its `performLayout()` called, and each RenderObject needs to calculate the `constraints` for its children and also calculate its size for the parent.
51+
52+
When the size of a RenderObject is confirmed, determining each RenderObject's relative position is another important task in layout.
53+
54+
In the end, each RenderObject will have received its size and offset as a result.
55+
56+
### Paint
57+
58+
The paint phase in WebF is a subset of Flutter's paint phases.
59+
60+
When the size and offset are ready, the canvas painter can paint the actual contents based on the offset of RenderObjects.
61+
62+
The border-radius, opacity, and transform CSS properties will affect the behavior of the actual painting.
63+
64+
## The Standard Load Mode
65+
66+
The standard mode is the loading mode for older versions of WebF, where the WebF widget, upon mounting in the Flutter tree, will start at the loading phase and run through all the phases step by step.
67+
68+
![img](./imgs/standard_load_mode.png)
69+
70+
## The Preload Mode
71+
72+
Beginning with version 0.16.0, the preload mode has been set as the default loading mode.
73+
74+
The preload mode preloads remote resources into memory and waits for execution before the WebF widget is mounted into the Flutter tree.
75+
76+
If the entry point is an HTML file, the HTML will be parsed, and its elements will be organized into a DOM tree. CSS files loaded through `<style>` and `<link>` elements will be parsed, and the calculated styles applied to the corresponding DOM elements.
77+
78+
However, JavaScript code will not be executed in this mode.
79+
80+
If the entry point is a JavaScript file, WebF only performs loading until the WebF widget is mounted into the Flutter tree.
81+
82+
Using this mode can save up to 50% of loading time while maintaining 100% compatibility with the standard mode.
83+
84+
![preload](./imgs/preload_mode.png)
85+
86+
Here is an example of using preload mode to load the page:
87+
88+
```dart
89+
controller = WebFController(
90+
context,
91+
);
92+
controller.preload(WebFBundle.fromUrl('assets:assets/bundle.html'));
93+
```
94+
95+
## The PreRendering Mode
96+
97+
The PreRendering mode goes a step further than preloading, cutting down up to 90% of loading time for optimal performance.
98+
99+
This mode simulates the instantaneous response of native Flutter pages but may require modifications in existing web codes for compatibility.
100+
101+
In this mode, all remote resources are loaded and executed similarly to the standard mode, but with an offline-like behavior.
102+
103+
Given that JavaScript is executed in this mode, properties like clientWidth and clientHeight from the viewModule always return 0.
104+
105+
This is because no layout or paint processes occur during preRendering.
106+
107+
If your application depends on viewModule properties, ensure that the related code is placed within the `load` and `DOMContentLoaded` event callbacks of the window.
108+
109+
These callbacks are triggered once the WebF widget is mounted into the Flutter tree.
110+
111+
Apps optimized for this mode remain compatible with both standard and preloading modes.
112+
113+
![prerendering](./imgs/prerendering.png)
114+
115+
```dart
116+
controller = WebFController(
117+
context,
118+
);
119+
controller.preRendering(WebFBundle.fromUrl('assets:assets/bundle.html'));
120+
```
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +0,0 @@
1-
---
2-
sidebar_position: 1
3-
title: Zero-copy Data Transmission
4-
---
5-
6-
TODO

0 commit comments

Comments
 (0)