部署指南
将ECharts知识库部署到生产环境的完整指南
📋 部署前检查清单
- [ ] 所有文档链接正常
- [ ] 图片资源已添加到
docs/public/ - [ ] 搜索功能测试通过
- [ ] 深色/浅色模式切换正常
- [ ] 移动端响应式测试通过
- [ ] SEO元数据配置正确
- [ ] 自定义域名配置(如需要)
🌐 部署选项
选项1: GitHub Pages (推荐)
优点: 免费、简单、自动HTTPS
适合: 开源项目、个人博客
步骤1: 配置基础路径
编辑 .vitepress/config.ts:
typescript
export default defineConfig({
base: '/echarts-kb/', // 仓库名
// ...其他配置
})1
2
3
4
2
3
4
步骤2: 创建部署脚本
.github/workflows/deploy.yml:
yaml
name: Deploy to GitHub Pages
on:
push:
branches: [main]
workflow_dispatch:
permissions:
contents: read
pages: write
id-token: write
concurrency:
group: pages
cancel-in-progress: false
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 18
cache: npm
- name: Setup Pages
uses: actions/configure-pages@v4
- name: Install dependencies
run: npm ci
- name: Build with VitePress
run: npm run docs:build
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: docs/.vitepress/dist
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
needs: build
runs-on: ubuntu-latest
name: Deploy
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v41
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
步骤3: 启用GitHub Pages
- 进入仓库 Settings → Pages
- Source 选择 "GitHub Actions"
- 保存后会自动部署
访问: https://yourusername.github.io/echarts-kb/
选项2: Vercel
优点: 零配置、全球CDN、自动HTTPS
适合: 商业项目、需要高性能
步骤1: 连接GitHub
- 访问 https://vercel.com
- 点击 "New Project"
- 导入GitHub仓库
步骤2: 配置构建设置
Framework Preset: VitePress
Build Command: npm run docs:build
Output Directory: docs/.vitepress/dist
Install Command: npm install1
2
3
4
2
3
4
步骤3: 部署
点击 "Deploy",Vercel会自动构建并发布
访问: https://echarts-kb.vercel.app
选项3: Netlify
优点: 强大的表单处理、A/B测试
适合: 需要后端集成的项目
步骤1: 创建netlify.toml
toml
[build]
publish = "docs/.vitepress/dist"
command = "npm run docs:build"
[build.environment]
NODE_VERSION = "18"
[[redirects]]
from = "/*"
to = "/index.html"
status = 2001
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
步骤2: 连接到Netlify
- 访问 https://netlify.com
- 拖拽项目文件夹或连接GitHub
- 自动检测配置并部署
选项4: 自建服务器 (Nginx)
优点: 完全控制、内网部署
适合: 企业内部、私有化部署
步骤1: 构建静态文件
bash
npm run docs:build1
步骤2: 配置Nginx
nginx
server {
listen 80;
server_name echarts-kb.example.com;
root /var/www/echarts-kb;
index index.html;
# Gzip压缩
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml;
# 缓存静态资源
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
# SPA路由支持
location / {
try_files $uri $uri/ /index.html;
}
# 安全头
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
add_header X-XSS-Protection "1; mode=block";
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
步骤3: 上传文件
bash
scp -r docs/.vitepress/dist/* user@server:/var/www/echarts-kb/1
步骤4: 配置HTTPS (Let's Encrypt)
bash
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d echarts-kb.example.com1
2
2
🔧 高级配置
自定义域名
GitHub Pages
- 在仓库根目录创建
CNAME文件:
docs.echarts.com1
- 在DNS提供商处添加CNAME记录:
CNAME docs.echarts.com yourusername.github.io1
Vercel/Netlify
在控制面板直接添加自定义域名,会自动配置DNS
PWA支持
编辑 .vitepress/config.ts:
typescript
export default defineConfig({
head: [
['link', { rel: 'manifest', href: '/manifest.json' }],
['meta', { name: 'theme-color', content: '#667eea' }],
],
})1
2
3
4
5
6
2
3
4
5
6
创建 docs/public/manifest.json:
json
{
"name": "ECharts 技术知识库",
"short_name": "ECharts KB",
"description": "系统化、结构化、源码级深度的ECharts完全指南",
"start_url": "/",
"display": "standalone",
"background_color": "#ffffff",
"theme_color": "#667eea",
"icons": [
{
"src": "/icon-192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "/icon-512.png",
"sizes": "512x512",
"type": "image/png"
}
]
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
分析统计
Google Analytics
编辑 .vitepress/config.ts:
typescript
export default defineConfig({
head: [
['script', { async: true, src: 'https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID' }],
['script', {}, `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'GA_MEASUREMENT_ID');
`]
]
})1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
Umami (隐私友好)
typescript
head: [
['script', {
defer: true,
src: 'https://umami.example.com/script.js',
'data-website-id': 'YOUR_WEBSITE_ID'
}]
]1
2
3
4
5
6
7
2
3
4
5
6
7
🚀 性能优化
1. 图片优化
bash
# 安装imagemin
npm install -g imagemin-cli imagemin-mozjpeg imagemin-pngquant
# 压缩图片
imagemin docs/public/*.png --out-dir=docs/public/optimized/1
2
3
4
5
2
3
4
5
2. 预加载关键资源
html
<!-- 在head中添加 -->
<link rel="preload" href="/assets/main.js" as="script">
<link rel="preload" href="/assets/main.css" as="style">1
2
3
2
3
3. CDN加速
使用Cloudflare等CDN服务:
1. 注册Cloudflare账号
2. 添加网站域名
3. 修改DNS nameservers
4. 自动获得全球CDN加速1
2
3
4
2
3
4
🔍 SEO优化
Sitemap生成
安装插件:
bash
npm install vitepress-plugin-sitemap1
配置 .vitepress/config.ts:
typescript
import { sitemap } from 'vitepress-plugin-sitemap'
export default defineConfig({
sitemap: {
hostname: 'https://echarts-kb.example.com',
lastmod: new Date().toISOString(),
}
})1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
robots.txt
创建 docs/public/robots.txt:
User-agent: *
Allow: /
Sitemap: https://echarts-kb.example.com/sitemap.xml1
2
3
4
2
3
4
🛡️ 安全加固
内容安全策略(CSP)
Nginx配置:
nginx
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline';";1
HTTPS强制跳转
nginx
server {
listen 80;
server_name echarts-kb.example.com;
return 301 https://$server_name$request_uri;
}1
2
3
4
5
2
3
4
5
📊 监控与告警
Uptime监控
使用UptimeRobot(免费):
- 注册 https://uptimerobot.com
- 添加监控URL
- 设置检查间隔(5分钟)
- 配置告警通知(邮件/短信)
错误追踪
集成Sentry:
bash
npm install @sentry/browser1
typescript
import * as Sentry from '@sentry/browser'
Sentry.init({
dsn: 'YOUR_SENTRY_DSN',
environment: 'production'
})1
2
3
4
5
6
2
3
4
5
6
🔄 持续集成/持续部署(CI/CD)
GitHub Actions完整示例
.github/workflows/ci-cd.yml:
yaml
name: CI/CD Pipeline
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- name: Install dependencies
run: npm ci
- name: Lint check
run: npm run lint
- name: Build test
run: npm run docs:build
deploy:
needs: test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- name: Install dependencies
run: npm ci
- name: Build
run: npm run docs:build
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./docs/.vitepress/dist
commit_message: 'deploy: ${{ github.event.head_commit.message }}'1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
🎯 部署检查清单
部署前逐项检查:
功能性检查
- [ ] 所有页面可以正常访问
- [ ] 内部链接无404错误
- [ ] 搜索功能正常工作
- [ ] 深色/浅色模式切换正常
- [ ] 移动端导航菜单可用
- [ ] 代码复制按钮有效
性能检查
- [ ] 首屏加载时间 < 2秒
- [ ] Lighthouse评分 > 90
- [ ] 图片已压缩优化
- [ ] Gzip/Brotli压缩启用
- [ ] 浏览器缓存配置正确
SEO检查
- [ ] 每个页面有唯一的title
- [ ] meta description配置
- [ ] Open Graph标签完整
- [ ] sitemap.xml可访问
- [ ] robots.txt配置正确
安全检查
- [ ] HTTPS证书有效
- [ ] 安全头配置正确
- [ ] 无敏感信息泄露
- [ ] CORS策略合理
🆘 常见问题
Q1: 部署后页面空白?
原因: 基础路径配置错误
解决:
typescript
// .vitepress/config.ts
base: '/repository-name/' // 确保与仓库名一致1
2
2
Q2: 刷新页面404?
原因: SPA路由需要fallback配置
解决(Nginx):
nginx
location / {
try_files $uri $uri/ /index.html;
}1
2
3
2
3
Q3: 搜索不工作?
原因: 本地搜索索引未正确构建
解决:
bash
# 清除缓存重新构建
rm -rf docs/.vitepress/cache
npm run docs:build1
2
3
2
3
Q4: 图片加载失败?
原因: 图片路径错误
解决:
- 图片放在
docs/public/目录 - 引用时使用绝对路径:
/image.png
📞 获取帮助
祝您部署顺利!🚀
