dbeam | f02a25aa | 2017-01-24 01:58:38 | [diff] [blame] | 1 | # Optimizing Chrome Web UIs |
| 2 | |
| 3 | ## How do I do it? |
| 4 | |
| 5 | In order to build with a fast configuration, try setting these options in your |
| 6 | GN args: |
| 7 | |
dpapad | 994a939 | 2025-03-25 00:21:43 | [diff] [blame] | 8 | ```python |
dpapad | d0ef1a9 | 2017-09-18 19:32:12 | [diff] [blame] | 9 | optimize_webui = true |
dbeam | f02a25aa | 2017-01-24 01:58:38 | [diff] [blame] | 10 | is_debug = false |
| 11 | ``` |
| 12 | |
dbeam | f02a25aa | 2017-01-24 01:58:38 | [diff] [blame] | 13 | ## How is the code optimized? |
| 14 | |
dpapad | 468d67c | 2022-10-21 23:06:00 | [diff] [blame] | 15 | ### Bundling |
dbeam | f02a25aa | 2017-01-24 01:58:38 | [diff] [blame] | 16 | |
dpapad | 468d67c | 2022-10-21 23:06:00 | [diff] [blame] | 17 | [JS Modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) |
| 18 | are used heavily throughout the code. Fetching all imports and their transitive |
| 19 | dependencies can be slow, especially when there are too many requests during |
| 20 | initial page load. |
dbeam | f02a25aa | 2017-01-24 01:58:38 | [diff] [blame] | 21 | |
dpapad | 468d67c | 2022-10-21 23:06:00 | [diff] [blame] | 22 | To reduce this latency, Chrome uses [Rollup](https://rollupjs.org) to bundle the |
| 23 | code into a couple JS bundle files (usually one or two). This greatly decreases |
| 24 | latency of initial load, by eliminating the overhead that is associated with |
| 25 | each individual request. |
dbeam | f02a25aa | 2017-01-24 01:58:38 | [diff] [blame] | 26 | |
| 27 | ### JavaScript Minification |
| 28 | |
| 29 | In order to minimize disk size, we run |
dpapad | 468d67c | 2022-10-21 23:06:00 | [diff] [blame] | 30 | [terser](https://github.com/terser/terser) on all combined JavaScript. This |
dbeam | f02a25aa | 2017-01-24 01:58:38 | [diff] [blame] | 31 | reduces installer and the size of resources required to load to show a UI. |
| 32 | |
| 33 | Code like this: |
| 34 | |
| 35 | ```js |
| 36 | function 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 | } |
| 43 | fizzBuzz(); |
| 44 | ``` |
| 45 | |
| 46 | would be minified to: |
| 47 | |
| 48 | ```js |
| 49 | function 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 | |
| 52 | If you'd like to more easily debug minified code, click the "{}" prettify button |
| 53 | in Chrome's developer tools, which will beautify the code and allow setting |
| 54 | breakpoints on the un-minified version. |
| 55 | |
| 56 | ### Gzip compression of web resources |
| 57 | |
dpapad | b6ce7d0 | 2020-07-13 08:30:22 | [diff] [blame] | 58 | As of [r761031](https://chromium.googlesource.com/chromium/src/+/6b83ee683f6c545be29ee807c6d0b6ac1508a549) |
| 59 | all HTML, JS, CSS and SVG resources are compressed by default with gzip |
| 60 | Previously this was only happening if the `compress="gzip"` attribute was |
| 61 | specified as follows in the corresponding .grd file: |
dbeam | f02a25aa | 2017-01-24 01:58:38 | [diff] [blame] | 62 | |
| 63 | ```xml |
| 64 | <include name="IDR_MY_PAGE" file="my/page.html" type="BINDATA" compress="gzip" /> |
| 65 | ``` |
| 66 | |
dpapad | b6ce7d0 | 2020-07-13 08:30:22 | [diff] [blame] | 67 | This is no longer necessary, and should be omitted. Only specify the `compress` |
| 68 | attribute if the value is `false` or `brotli`. |
dbeam | f02a25aa | 2017-01-24 01:58:38 | [diff] [blame] | 69 | |
dpapad | b6ce7d0 | 2020-07-13 08:30:22 | [diff] [blame] | 70 | Compressed resources are uncompressed at a fairly low layer within |
| 71 | ResourceBundle, and WebUI authors typically do not need to do anything special |
| 72 | to serve those files to the UI. |