|
| 1 | +# Versioned documentation |
| 2 | + |
| 3 | +The theme ships with a version selector in the header. It is opt-in by |
| 4 | +deployment: when the site exposes a `versions.json` at its root the |
| 5 | +selector appears, populated, and lets readers switch between versions |
| 6 | +while staying on the same page. When `versions.json` is missing the |
| 7 | +selector stays hidden and adds nothing to the layout. |
| 8 | + |
| 9 | +The theme does not produce `versions.json` itself. It expects the format |
| 10 | +that [`mike`](https://github.com/jimporter/mike) writes when it deploys |
| 11 | +multi-version sites to the `gh-pages` branch. |
| 12 | + |
| 13 | +## How the selector works |
| 14 | + |
| 15 | +The browser fetches `versions.json` relative to the deployment root (one |
| 16 | +level above the current version directory). Each entry has a `version`, |
| 17 | +an optional `title`, and an optional list of `aliases`. The selector |
| 18 | +shows the active version's title and any aliases attached to it. Picking |
| 19 | +another entry sends the reader to the same page path under that other |
| 20 | +version, so deep links survive a version switch. |
| 21 | + |
| 22 | +If the fetch fails (404, network error, malformed JSON) the selector |
| 23 | +stays hidden. There is no flash of unstyled content because the partial |
| 24 | +renders with `hidden` and is only revealed once the data is in. |
| 25 | + |
| 26 | +## Publishing with mike |
| 27 | + |
| 28 | +Install mike alongside MkDocs: |
| 29 | + |
| 30 | +```bash |
| 31 | +pip install mike |
| 32 | +``` |
| 33 | + |
| 34 | +Deploy a version and tag it as the default `latest`: |
| 35 | + |
| 36 | +```bash |
| 37 | +mike deploy --push --update-aliases 1.0 latest |
| 38 | +mike set-default --push latest |
| 39 | +``` |
| 40 | + |
| 41 | +Subsequent releases follow the same pattern. Mike rewrites |
| 42 | +`versions.json` on the `gh-pages` branch each time and the selector |
| 43 | +reflects the new state on the next page load. |
| 44 | + |
| 45 | +```bash |
| 46 | +mike deploy --push --update-aliases 2.0 latest |
| 47 | +``` |
| 48 | + |
| 49 | +To remove a version: |
| 50 | + |
| 51 | +```bash |
| 52 | +mike delete --push 1.0 |
| 53 | +``` |
| 54 | + |
| 55 | +## URL layout |
| 56 | + |
| 57 | +Mike publishes each version into its own subdirectory at the deployment |
| 58 | +root. A site published under `https://example.com/myproject/` ends up |
| 59 | +shaped like this: |
| 60 | + |
| 61 | +``` |
| 62 | +myproject/ |
| 63 | + versions.json |
| 64 | + 1.0/ |
| 65 | + index.html |
| 66 | + ... |
| 67 | + 2.0/ |
| 68 | + index.html |
| 69 | + ... |
| 70 | +``` |
| 71 | + |
| 72 | +The `versions.json` file uses this shape: |
| 73 | + |
| 74 | +```json |
| 75 | +[ |
| 76 | + { "version": "2.0", "title": "2.0", "aliases": ["latest"] }, |
| 77 | + { "version": "1.0", "title": "1.0", "aliases": [] } |
| 78 | +] |
| 79 | +``` |
| 80 | + |
| 81 | +The selector's "switch to another version" link rewrites the path so |
| 82 | +`https://example.com/myproject/2.0/guide/configuration/` becomes |
| 83 | +`https://example.com/myproject/1.0/guide/configuration/` when you pick |
| 84 | +`1.0`. Pages that only exist in one version land at a 404 and the |
| 85 | +bundled 404 page sends the reader back home. |
| 86 | + |
| 87 | +## Testing locally |
| 88 | + |
| 89 | +Mike can serve the multi-version build without pushing: |
| 90 | + |
| 91 | +```bash |
| 92 | +mike serve |
| 93 | +``` |
| 94 | + |
| 95 | +This starts a server that mirrors the `gh-pages` layout, so the selector |
| 96 | +behaves the same way it will in production. |
0 commit comments