|
| 1 | +# Server client renderer |
| 2 | + |
| 3 | +See https://docsify.now.sh |
| 4 | + |
| 5 | +## Why SSR? |
| 6 | +- Better SEO |
| 7 | +- Feeling cool |
| 8 | + |
| 9 | +## Quick start |
| 10 | + |
| 11 | +Install `now` and `docsify-cli` in your project. |
| 12 | + |
| 13 | +```bash |
| 14 | +npm i now -g |
| 15 | +npm i docsify-cli -D |
| 16 | +``` |
| 17 | + |
| 18 | +Edit `package.json`. If the documentation in `./docs` subdirectory. |
| 19 | + |
| 20 | +```json |
| 21 | +{ |
| 22 | + "name": "my-project", |
| 23 | + "scripts": { |
| 24 | + "start": "docsify start docs" |
| 25 | + }, |
| 26 | + "files": [ |
| 27 | + "docs" |
| 28 | + ], |
| 29 | + "docsify": { |
| 30 | + "config": { |
| 31 | + "basePath": "/docs/", |
| 32 | + "loadSidebar": true, |
| 33 | + "loadNavbar": true, |
| 34 | + "coverpage": true, |
| 35 | + "name": "docsify" |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +!> The `basePath` just like webpack `publicPath`. You should config it if your docs is in the subdirectory. |
| 42 | + |
| 43 | +We can preview in the local to see if it works. |
| 44 | + |
| 45 | +```bash |
| 46 | +npm start |
| 47 | + |
| 48 | +# open http://localhost:4000 |
| 49 | +``` |
| 50 | + |
| 51 | +Publish it! |
| 52 | + |
| 53 | +```bash |
| 54 | +now -p |
| 55 | +``` |
| 56 | + |
| 57 | +Now, You have a support for SSR the docs site. |
| 58 | + |
| 59 | +## Custom template |
| 60 | + |
| 61 | +You can provide a templte for entire page's HTML. such as |
| 62 | + |
| 63 | +```html |
| 64 | +<!DOCTYPE html> |
| 65 | +<html lang="en"> |
| 66 | +<head> |
| 67 | + <meta charset="UTF-8"> |
| 68 | + <title>docsify</title> |
| 69 | + <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> |
| 70 | + <link rel="stylesheet" href="//unpkg.com/docsify/themes/vue.css" title="vue"> |
| 71 | +</head> |
| 72 | +<body> |
| 73 | + <!--inject-app--> |
| 74 | + <!--inject-config--> |
| 75 | +</body> |
| 76 | +<script src="//unpkg.com/docsify/lib/docsify.js"></script> |
| 77 | +<script src="//unpkg.com/docsify/lib/plugins/search.js"></script> |
| 78 | +<script src="//unpkg.com/prismjs/components/prism-bash.min.js"></script> |
| 79 | +<script src="//unpkg.com/prismjs/components/prism-markdown.min.js"></script> |
| 80 | +<script src="//unpkg.com/prismjs/components/prism-nginx.min.js"></script> |
| 81 | +</html> |
| 82 | +``` |
| 83 | + |
| 84 | +The template should contain these comments for rendered app content. |
| 85 | + - `<!--inject-app-->` |
| 86 | + - `<!--inject-config-->` |
| 87 | + |
| 88 | +## Configuration |
| 89 | + |
| 90 | +You can configure it in a special config file, or `package.json`. |
| 91 | + |
| 92 | +```js |
| 93 | +module.exports = { |
| 94 | + tempate: './ssr.html', |
| 95 | + config: { |
| 96 | + // docsify config |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +## Deploy for your VPS |
| 102 | + |
| 103 | +You can run `docsify start` directly on your Node server, or write your own server app with `docsify-server-renderer`. |
| 104 | + |
| 105 | +```js |
| 106 | +var Renderer = require('docsify-server-renderer') |
| 107 | +var readFileSync = require('fs').readFileSync |
| 108 | + |
| 109 | +// init |
| 110 | +var renderer = new Renderer({ |
| 111 | + template: readFileSync('./docs/index.template.html', 'utf-8')., |
| 112 | + config: { |
| 113 | + name: 'docsify', |
| 114 | + repo: 'qingwei-li/docsify' |
| 115 | + } |
| 116 | +}) |
| 117 | + |
| 118 | +renderer.renderToString(url) |
| 119 | + .then(html => {}) |
| 120 | + .catch(err => {}) |
| 121 | +``` |
0 commit comments