blob: 302ff78faf107671ffe8a737563b93175e085f9a [file] [log] [blame] [view]
dbeamf02a25aa2017-01-24 01:58:381# Optimizing Chrome Web UIs
2
3## How do I do it?
4
5In order to build with a fast configuration, try setting these options in your
6GN args:
7
dpapad994a9392025-03-25 00:21:438```python
dpapadd0ef1a92017-09-18 19:32:129optimize_webui = true
dbeamf02a25aa2017-01-24 01:58:3810is_debug = false
11```
12
dbeamf02a25aa2017-01-24 01:58:3813## How is the code optimized?
14
dpapad468d67c2022-10-21 23:06:0015### Bundling
dbeamf02a25aa2017-01-24 01:58:3816
dpapad468d67c2022-10-21 23:06:0017[JS Modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules)
18are used heavily throughout the code. Fetching all imports and their transitive
19dependencies can be slow, especially when there are too many requests during
20initial page load.
dbeamf02a25aa2017-01-24 01:58:3821
dpapad468d67c2022-10-21 23:06:0022To reduce this latency, Chrome uses [Rollup](https://rollupjs.org) to bundle the
23code into a couple JS bundle files (usually one or two). This greatly decreases
24latency of initial load, by eliminating the overhead that is associated with
25each individual request.
dbeamf02a25aa2017-01-24 01:58:3826
27### JavaScript Minification
28
29In order to minimize disk size, we run
dpapad468d67c2022-10-21 23:06:0030[terser](https://github.com/terser/terser) on all combined JavaScript. This
dbeamf02a25aa2017-01-24 01:58:3831reduces installer and the size of resources required to load to show a UI.
32
33Code like this:
34
35```js
36function fizzBuzz() {
37 for (var i = 1; i <= 100; i++) {
38 var fizz = i % 3 == 0 ? 'fizz' : '';
39 var buzz = i % 5 == 0 ? 'buzz' : '';
40 console.log(fizz + buzz || i);
41 }
42}
43fizzBuzz();
44```
45
46would be minified to:
47
48```js
49function fizzBuzz(){for(var z=1;100>=z;z++){var f=z%3==0?"fizz":"",o=z%5==0?"buzz":"";console.log(f+o||z)}}fizzBuzz();
50```
51
52If you'd like to more easily debug minified code, click the "{}" prettify button
53in Chrome's developer tools, which will beautify the code and allow setting
54breakpoints on the un-minified version.
55
56### Gzip compression of web resources
57
dpapadb6ce7d02020-07-13 08:30:2258As of [r761031](https://chromium.googlesource.com/chromium/src/+/6b83ee683f6c545be29ee807c6d0b6ac1508a549)
59all HTML, JS, CSS and SVG resources are compressed by default with gzip
60Previously this was only happening if the `compress="gzip"` attribute was
61specified as follows in the corresponding .grd file:
dbeamf02a25aa2017-01-24 01:58:3862
63```xml
64<include name="IDR_MY_PAGE" file="my/page.html" type="BINDATA" compress="gzip" />
65```
66
dpapadb6ce7d02020-07-13 08:30:2267This is no longer necessary, and should be omitted. Only specify the `compress`
68attribute if the value is `false` or `brotli`.
dbeamf02a25aa2017-01-24 01:58:3869
dpapadb6ce7d02020-07-13 08:30:2270Compressed resources are uncompressed at a fairly low layer within
71ResourceBundle, and WebUI authors typically do not need to do anything special
72to serve those files to the UI.