|
| 1 | +--- |
| 2 | +template: overrides/main.html |
| 3 | +--- |
| 4 | + |
| 5 | +# Building for offline usage |
| 6 | + |
| 7 | +If you want to ship your documentation together with your product, MkDocs has |
| 8 | +you covered – with support from themes, [MkDocs] allows for building |
| 9 | +offline-capable documentation. Luckily, Material for MkDocs offers offline |
| 10 | +support for many of its features. |
| 11 | + |
| 12 | + [MkDocs]: https://www.mkdocs.org |
| 13 | + |
| 14 | +## Configuration |
| 15 | + |
| 16 | +### Built-in offline plugin |
| 17 | + |
| 18 | +[:octicons-heart-fill-24:{ .mdx-heart } Insiders][Insiders]{ .mdx-insiders } · |
| 19 | +[:octicons-tag-24: insiders-4.10.0][Insiders] · |
| 20 | +:octicons-cpu-24: Plugin · |
| 21 | +:octicons-beaker-24: Experimental |
| 22 | + |
| 23 | +The built-in offline plugin makes sure that the [site search] works when you |
| 24 | +distribute the contents of your [site directory] as a download. Simply add |
| 25 | +the following lines to `mkdocs.yml`: |
| 26 | + |
| 27 | +``` yaml |
| 28 | +plugins: |
| 29 | + - offline # (1)! |
| 30 | +``` |
| 31 | +
|
| 32 | +1. Note that the offline plugin should be located at the end of the list of |
| 33 | + `plugins`, as it will post-process the search index. If you want to use |
| 34 | + other plugins that alter the search index together with this plugin, add |
| 35 | + them before the built-in offline plugin.[^1] |
| 36 | + |
| 37 | + [^1]: |
| 38 | + Offline search was previously implemented through the third-party |
| 39 | + [localsearch] plugin, which is still possible if you don't want to use |
| 40 | + [Insiders]. Note, however, that setup might be challenging if you're not |
| 41 | + experienced with MkDocs. |
| 42 | + |
| 43 | +The plugin will automatically disable [`use_directory_urls`][use_directory_urls] |
| 44 | +via `mkdocs.yml`, ensuring that the user can open your documentation directly |
| 45 | +from the local file system. |
| 46 | + |
| 47 | +The following configuration options are available: |
| 48 | + |
| 49 | +`enabled`{ #enabled } |
| 50 | + |
| 51 | +: :octicons-milestone-24: Default: `true` – This option specifies whether |
| 52 | + the plugin is enabled when building your project. If you want to switch |
| 53 | + the plugin off, e.g. for local builds, use an [environment variable]: |
| 54 | + |
| 55 | + ``` yaml |
| 56 | + plugins: |
| 57 | + - privacy: |
| 58 | + enabled: !ENV [OFFLINE, false] |
| 59 | + ``` |
| 60 | + |
| 61 | +Now, after invoking `mkdocs build`, you can open `site/index.html` directly |
| 62 | +in your browser and the [site search] will work as if the documentation was |
| 63 | +hosted on a regular server. |
| 64 | + |
| 65 | +!!! tip "Automatically bundle all external assets" |
| 66 | + |
| 67 | + The brand-new [built-in privacy plugin] makes it easy to use external assets |
| 68 | + while building documentation for offline usage, as it will automatically |
| 69 | + download all external assets to distribute them with your documentation. |
| 70 | + |
| 71 | + [Insiders]: ../insiders/index.md |
| 72 | + [site search]: setting-up-site-search.md |
| 73 | + [site directory]: https://www.mkdocs.org/user-guide/configuration/#site_dir |
| 74 | + [localsearch]: https://github.com/wilhelmer/mkdocs-localsearch/ |
| 75 | + [use_directory_urls]: https://www.mkdocs.org/user-guide/configuration/#use_directory_urls |
| 76 | + [environment variable]: https://www.mkdocs.org/user-guide/configuration/#environment-variables |
| 77 | + [built-in privacy plugin]: ensuring-data-privacy.md#built-in-privacy-plugin |
| 78 | + |
| 79 | +#### Limitations |
| 80 | + |
| 81 | +Material for MkDocs offers many interactive features, some of which will not |
| 82 | +work from the file system due to the restrictions of modern browsers: all |
| 83 | +features that use the `fetch` API will error. |
| 84 | + |
| 85 | +Thus, when building for offline usage, make sure to disable the following |
| 86 | +configuration settings: [instant loading], [site analytics], [git repository], |
| 87 | +[versioning] and [comment systems]. |
| 88 | + |
| 89 | + [Instant loading]: setting-up-navigation.md#instant-loading |
| 90 | + [Site analytics]: setting-up-site-analytics.md |
| 91 | + [Versioning]: setting-up-versioning.md |
| 92 | + [Git repository]: adding-a-git-repository.md |
| 93 | + [Comment systems]: adding-a-comment-system.md |
0 commit comments