docsify/docs/ssr.md

125 lines
2.6 KiB
Markdown
Raw Normal View History

2018-02-23 11:56:51 +08:00
# Server-Side Rendering
2017-05-30 03:48:29 +08:00
See https://docsify.now.sh
2017-05-30 11:54:37 +08:00
Repo in https://github.com/docsifyjs/docsify-ssr-demo
2017-05-31 08:35:01 +08:00
2017-05-30 11:54:37 +08:00
## Why SSR?
- Better SEO
- Feeling cool
## Quick start
2017-05-30 12:55:09 +08:00
Install `now` and `docsify-cli` in your project.
2017-05-30 11:54:37 +08:00
```bash
2017-05-31 08:35:01 +08:00
npm i now docsify-cli -D
2017-05-30 11:54:37 +08:00
```
2017-05-30 12:55:09 +08:00
Edit `package.json`. If the documentation in `./docs` subdirectory.
2017-05-30 11:54:37 +08:00
```json
{
"name": "my-project",
"scripts": {
2017-05-31 08:35:01 +08:00
"start": "docsify start . -c ssr.config.js",
"deploy": "now -p"
2017-05-30 11:54:37 +08:00
},
"files": [
"docs"
],
"docsify": {
"config": {
"basePath": "https://docsify.js.org/",
2017-05-30 11:54:37 +08:00
"loadSidebar": true,
"loadNavbar": true,
"coverpage": true,
"name": "docsify"
}
}
}
```
2017-05-31 08:35:01 +08:00
!> The `basePath` just like webpack `publicPath`. We can use local or remote files.
2017-05-30 11:54:37 +08:00
2017-05-30 12:55:09 +08:00
We can preview in the local to see if it works.
2017-05-30 11:54:37 +08:00
```bash
npm start
# open http://localhost:4000
```
2017-05-30 12:55:09 +08:00
Publish it!
2017-05-30 11:54:37 +08:00
```bash
now -p
```
Now, You have a support for SSR the docs site.
## Custom template
2017-12-16 12:06:12 +08:00
You can provide a template for entire page's HTML. such as
2017-05-30 11:54:37 +08:00
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>docsify</title>
2020-04-28 04:47:49 +08:00
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/docsify/themes/vue.css" title="vue">
2017-05-30 11:54:37 +08:00
</head>
<body>
<!--inject-app-->
<!--inject-config-->
<script src="//cdn.jsdelivr.net/npm/docsify/lib/docsify.js"></script>
<script src="//cdn.jsdelivr.net/npm/docsify/lib/plugins/search.js"></script>
<script src="//cdn.jsdelivr.net/npm/prismjs/components/prism-bash.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/prismjs/components/prism-markdown.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/prismjs/components/prism-nginx.min.js"></script>
2017-05-30 11:54:37 +08:00
</body>
</html>
```
The template should contain these comments for rendered app content.
- `<!--inject-app-->`
- `<!--inject-config-->`
## Configuration
You can configure it in a special config file, or `package.json`.
```js
module.exports = {
2017-05-30 15:41:34 +08:00
template: './ssr.html',
2017-12-29 16:45:32 +08:00
maxAge: 60 * 60 * 1000, // lru-cache config
2017-05-30 11:54:37 +08:00
config: {
// docsify config
}
}
```
## Deploy for your VPS
You can run `docsify start` directly on your Node server, or write your own server app with `docsify-server-renderer`.
```js
var Renderer = require('docsify-server-renderer')
var readFileSync = require('fs').readFileSync
// init
var renderer = new Renderer({
2019-01-06 07:26:21 +08:00
template: readFileSync('./docs/index.template.html', 'utf-8'),
2017-05-30 11:54:37 +08:00
config: {
name: 'docsify',
repo: 'docsifyjs/docsify'
2017-05-30 11:54:37 +08:00
}
})
renderer.renderToString(url)
.then(html => {})
.catch(err => {})
```