|
| 1 | +--- |
| 2 | +status: new |
| 3 | +--- |
| 4 | + |
| 5 | +# Building an optimized site |
| 6 | + |
| 7 | +Material for MkDocs, by default, allows to build optimized sites that rank great |
| 8 | +on search engines, load fast (even on slow networks), and work perfectly without |
| 9 | +JavaScript. Additionally, the [built-in optimize plugin] adds support for |
| 10 | +further useful automatic optimization techniques. |
| 11 | + |
| 12 | + [built-in optimize plugin]: #built-in-optimize-plugin |
| 13 | + |
| 14 | +## Configuration |
| 15 | + |
| 16 | +### Built-in optimize plugin |
| 17 | + |
| 18 | +[:octicons-heart-fill-24:{ .mdx-heart } Sponsors only][Insiders]{ .mdx-insiders } · |
| 19 | +[:octicons-tag-24: insiders-4.29.0][Insiders] · |
| 20 | +:octicons-cpu-24: Plugin · |
| 21 | +:octicons-beaker-24: Experimental |
| 22 | + |
| 23 | +The built-in optimize plugin automatically identifies and optimizes all media |
| 24 | +files as part of the build using compression and conversion techniques. Add |
| 25 | +the following lines to `mkdocs.yml`: |
| 26 | + |
| 27 | +``` yaml |
| 28 | +plugins: |
| 29 | + - optimize |
| 30 | +``` |
| 31 | +
|
| 32 | +> If you need to be able to build your documentation with and without |
| 33 | +> [Insiders], please refer to the [built-in plugins] section to learn how |
| 34 | +> shared configurations help to achieve this. |
| 35 | +
|
| 36 | +The following configuration options are available: |
| 37 | +
|
| 38 | + [Insiders]: ../insiders/index.md |
| 39 | + [built-in plugins]: ../insiders/getting-started.md#built-in-plugins |
| 40 | +
|
| 41 | +[`enabled`](#+optimize.enabled){ #+optimize.enabled } |
| 42 | + |
| 43 | +: :octicons-milestone-24: Default: `true` – This option specifies whether |
| 44 | + the plugin is enabled when building your project. If you want to speed up |
| 45 | + local builds, you can use an [environment variable]: |
| 46 | + |
| 47 | + ``` yaml |
| 48 | + plugins: |
| 49 | + - optimize: |
| 50 | + enabled: !ENV [CI, false] |
| 51 | + ``` |
| 52 | + |
| 53 | +[`concurrency`](#+optimize.concurrency){ #+optimize.concurrency } |
| 54 | + |
| 55 | +: :octicons-milestone-24: Default: _number of CPUs_ – This option specifies |
| 56 | + how many CPUs the plugin is allowed to use when optimizing media files. |
| 57 | + With more CPUs, the plugin can do more work in the same time, thus complete |
| 58 | + optimization faster. Concurrent processing can be disabled with: |
| 59 | + |
| 60 | + ``` yaml |
| 61 | + plugins: |
| 62 | + - optimize: |
| 63 | + concurrency: 1 |
| 64 | + ``` |
| 65 | + |
| 66 | + [environment variable]: https://www.mkdocs.org/user-guide/configuration/#environment-variables |
| 67 | + |
| 68 | +#### Optimization |
| 69 | + |
| 70 | +Technical documentation often includes screenshots or diagrams, both of which |
| 71 | +are prime candidates for compression. The [built-in optimize plugin] allows to |
| 72 | +automatically compress images using [pngquant] (for PNGs), and [Pillow] |
| 73 | +(for JPGs). |
| 74 | + |
| 75 | +The following configuration options are available for optimization: |
| 76 | + |
| 77 | +[`optimize_png`](#+optimize.optimize_png){ #+optimize.optimize_png } |
| 78 | + |
| 79 | +: :octicons-milestone-24: Default: `true` – This option specifies whether |
| 80 | + the plugin should optimize PNG files using [pngquant], which must be |
| 81 | + installed on the system. PNG optimization can be disabled with: |
| 82 | + |
| 83 | + ``` yaml |
| 84 | + plugins: |
| 85 | + - optimize: |
| 86 | + optimize_png: false |
| 87 | + ``` |
| 88 | + |
| 89 | +[`optimize_png_speed`](#+optimize.optimize_png_speed){ #+optimize.optimize_png_speed } |
| 90 | + |
| 91 | +: :octicons-milestone-24: Default: `4` of `[1,10]` – This option specifies the |
| 92 | + speed/quality tradeoff that [pngquant] applies when compressing. The lower |
| 93 | + the number, the more time will be spent optimizing: |
| 94 | + |
| 95 | + === "Slower <small>small</small>" |
| 96 | + |
| 97 | + ``` yaml |
| 98 | + plugins: |
| 99 | + - optimize: |
| 100 | + optimize_png_speed: 1 |
| 101 | + ``` |
| 102 | + |
| 103 | + === "Faster <small>rough</small>" |
| 104 | + |
| 105 | + ``` yaml |
| 106 | + plugins: |
| 107 | + - optimize: |
| 108 | + optimize_png_speed: 10 |
| 109 | + ``` |
| 110 | + |
| 111 | + A factor of `10` has 5% lower quality, but is 8x faster than the default `4`. |
| 112 | + |
| 113 | +[`optimize_png_strip`](#+optimize.optimize_png_strip){ #+optimize.optimize_png_strip } |
| 114 | + |
| 115 | +: :octicons-milestone-24: Default: `true` – This option specifies whether |
| 116 | + [pngquant] should remove all non-optional metadata that is not necessary |
| 117 | + for rendering images in a browser: |
| 118 | + |
| 119 | + ``` yaml |
| 120 | + plugins: |
| 121 | + - optimize: |
| 122 | + optimize_png_strip: false |
| 123 | + ``` |
| 124 | + |
| 125 | +[`optimize_jpg`](#+optimize.optimize_jpg){ #+optimize.optimize_jpg } |
| 126 | + |
| 127 | +: :octicons-milestone-24: Default: `true` – This option specifies whether |
| 128 | + the plugin should optimize JPG files using [Pillow], a Python image |
| 129 | + processing library. JPG optimization can be disabled with: |
| 130 | + |
| 131 | + ``` yaml |
| 132 | + plugins: |
| 133 | + - optimize: |
| 134 | + optimize_jpg: false |
| 135 | + ``` |
| 136 | + |
| 137 | +[`optimize_jpg_quality`](#+optimize.optimize_jpg_quality){ #+optimize.optimize_jpg_quality } |
| 138 | + |
| 139 | +: :octicons-milestone-24: Default: `60` of `[0,100]` – This option specifies |
| 140 | + the image quality that [Pillow] uses when compressing. If the images look |
| 141 | + blurry, it's a good idea to tune and change this setting: |
| 142 | + |
| 143 | + ``` yaml |
| 144 | + plugins: |
| 145 | + - optimize: |
| 146 | + optimize_jpg_quality: 75 |
| 147 | + ``` |
| 148 | + |
| 149 | +[`optimize_jpg_progressive`](#+optimize.optimize_jpg_progressive){ #+optimize.optimize_jpg_progressive } |
| 150 | + |
| 151 | +: :octicons-milestone-24: Default: `true` – This option specifies whether |
| 152 | + [Pillow] should use [progressive encoding] (faster rendering) when |
| 153 | + compressing JPGs. Progressive encoding can be disabled with: |
| 154 | + |
| 155 | + ``` yaml |
| 156 | + plugins: |
| 157 | + - optimize: |
| 158 | + optimize_jpg_progressive: false |
| 159 | + ``` |
| 160 | + |
| 161 | + [pngquant]: https://pngquant.org/ |
| 162 | + [Pillow]: https://pillow.readthedocs.io/ |
| 163 | + [progressive encoding]: https://medium.com/hd-pro/jpeg-formats-progressive-vs-baseline-73b3938c2339 |
| 164 | + |
| 165 | +#### Caching |
| 166 | + |
| 167 | +The [built-in optimize plugin] implements an intelligent caching mechanism, |
| 168 | +ensuring that media files are only pushed through the optimization pipeline when |
| 169 | +their contents change. If you swap out or update an image, the plugin will |
| 170 | +detect it and update the optimized version. |
| 171 | + |
| 172 | +The following configuration options are available for caching: |
| 173 | + |
| 174 | +[`cache`](#+optimize.cache){ #+optimize.cache } |
| 175 | + |
| 176 | +: :octicons-milestone-24: Default: `true` – This option specifies whether |
| 177 | + the plugin queries its cache for an existing artifact before starting an |
| 178 | + optimization job. It's normally not necessary to change this setting, |
| 179 | + except for when debugging the plugin itself. Caching can be disabled with: |
| 180 | + |
| 181 | + ``` yaml |
| 182 | + plugins: |
| 183 | + - optimize: |
| 184 | + cache: false |
| 185 | + ``` |
| 186 | + |
| 187 | +[`cache_dir`](#+optimize.cache_dir){ #+optimize.cache_dir } |
| 188 | + |
| 189 | +: :octicons-milestone-24: Default: `.cache/plugins/optimize` – This option |
| 190 | + specifies the file system location of the plugin's cache. It's normally not |
| 191 | + necessary to change this setting, except for when debugging the plugin |
| 192 | + itself. The cache directory can be changed with: |
| 193 | + |
| 194 | + ``` yaml |
| 195 | + plugins: |
| 196 | + - optimize: |
| 197 | + cache_dir: path/to/folder |
| 198 | + ``` |
| 199 | + |
| 200 | + By default, all built-in plugins that implement caching will create a |
| 201 | + `.cache` directory in the same folder your `mkdocs.yml` resides, and create |
| 202 | + subfolders to not interfere with each other. If you use multiple instances |
| 203 | + of this plugin, it could be necessary to change this setting. |
0 commit comments