Skip to content

Commit 4e01f4d

Browse files
authored
Merge pull request #1970 from docsifyjs/update-routerMode-docs
docs: add notes on "hash" vs "history" routing in the config docs
2 parents 4d560c0 + dab6e1c commit 4e01f4d

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

docs/configuration.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,16 @@ window.$docsify = {
4444
'/zh-cn/changelog': '/changelog',
4545
'/changelog':
4646
'https://raw.githubusercontent.com/docsifyjs/docsify/master/CHANGELOG',
47+
48+
// You may need this if you use routerMode:'history'.
4749
'/.*/_sidebar.md': '/_sidebar.md', // See #301
4850
},
4951
};
5052
```
5153

54+
> **Note** If you change [`routerMode`](#routermode) to `'history'`, you may
55+
> want to configure an alias for your `_sidebar.md` and `_navbar.md` files.
56+
5257
## auto2top
5358

5459
- Type: `Boolean`
@@ -659,6 +664,8 @@ window.$docsify = {
659664

660665
## routerMode
661666

667+
Configure the URL format that the paths of your site will use.
668+
662669
- Type: `String`
663670
- Default: `'hash'`
664671

@@ -668,6 +675,57 @@ window.$docsify = {
668675
};
669676
```
670677

678+
For statically-deployed sites (f.e. on GitHub Pages) hash-based routing is
679+
simpler to set up. For websites that can re-write URLs, the history-based format
680+
is better (especially for search-engine optimization, hash-based routing is not
681+
so search-engine friendly)
682+
683+
Hash-based routing means all URL paths will be prefixed with `/#/` in the
684+
address bar. This is a trick that allows the site to load `/index.html`, then it
685+
uses the path that follows the `#` to determine what markdown files to load. For
686+
example, a complete hash-based URL may look like this:
687+
`https://example.com/#/path/to/page`. The browser will actually load
688+
`https://example.com` (assuming your static server serves
689+
`index.html` by default, as most do), and then the Docsify JavaScript code will
690+
look at the `/#/...` and determine the markdown file to load and render.
691+
Additionally, when clicking on a link, the Docsify router will change the
692+
content after the hash dynamically. The value of `location.pathname` will still be
693+
`/` no matter what. The parts of a hash path are _not_ sent to the server when
694+
visiting such a URL in a browser.
695+
696+
On the other hand, history-based routing means the Docsify JavaScript will use
697+
the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API)
698+
to dynamically change the URL without using a `#`. This means that all URLs will
699+
be considered "real" by search engines, and the full path will be sent to the
700+
server when visiting the URL in your browser. For example, a URL may look like
701+
`https://example.com/path/to/page`. The browser will try to load that full URL
702+
directly from the server, not just `https://example.com`. The upside of this is
703+
that these types of URLs are much more friendly for search engines, and can be
704+
indexed (yay!). The downside, however, is that your server, or the place where
705+
you host your site files, has to be able to handle these URLs. Various static
706+
website hosting services allow "rewrite rules" to be configured, such that a
707+
server can be configured to always send back `/index.html` no matter what path
708+
is visited. The value of `location.pathname` will show `/path/to/page`, because
709+
it was actually sent to the server.
710+
711+
TLDR: start with `hash` routing (the default). If you feel adventurous, learn
712+
how to configure a server, then switch to `history` mode for better experience
713+
without the `#` in the URL and SEO optimization.
714+
715+
> **Note** If you use `routerMode: 'history'`, you may want to add an
716+
> [`alias`](#alias) to make your `_sidebar.md` and `_navbar.md` files always be
717+
> loaded no matter which path is being visited.
718+
>
719+
> ```js
720+
> window.$docsify = {
721+
> routerMode: 'history',
722+
> alias: {
723+
> '/.*/_sidebar.md': '/_sidebar.md',
724+
> '/.*/_navbar.md': '/_navbar.md',
725+
> },
726+
> };
727+
> ```
728+
671729
## routes
672730
673731
- Type: `Object`

0 commit comments

Comments
 (0)