vercel部署问题

Why vercel?

1
2
deployment more fast than gh-pages
automatic: 支持持续部署,push source 分支即可自动生成

Problem I : 报错-指令错误

1
2
vercel的原理是linux虚拟机
windows系统的node_modules需要gitignore掉,不然vercel会报错权限不足
1
2
3
4
5
6
7
8
# 确保 node_modules 被忽略
echo "node_modules/" >> .gitignore

# 移除已上传的 node_modules
# .gitignore 需要在第一次 push node_modules 之前配置,否则已有文件仍会被上传。
git rm -r --cached node_modules
git commit -m "Remove node_modules from repo"
git push

Problem II : 部署后全空白

1
2
3
vercel终端打印: No layout: xxx.xxx
去看github发现:themes/cactus 完全没有上传
原因:git嵌套问题,因为下载时 git clone 后子模块自行 init 了
1
2
3
4
# 删掉cactus目录下的.git文件后返回根目录
git add themes/cactus
git commit -m "Add Hexo theme Cactus"
git push

Problem III : Branch的选择错误导致没有样式

1
2
3
vercel默认选择defalut分支,故需要在仓库内修改
若选择 gh-pages 分支,会导致路径不是根目录,而是 <repository_name>/

Problem IV : 同步实现vercel和gh-pages部署

1
2
3
4
5
6
7
8
9
10
11
12
13
# 创建 Vercel 专用配置文件
根目录下创建 _config.vercel.yml
url: https://mingsage-site.vercel.app # 替换成你的 Vercel 域名
root: / # 网站根路径
relative_link: false # 禁用相对路径,保证主题资源引用绝对路径

# Vercel 部署
Vercel 会检测 source 分支提交并自动 deploy
快速、无需手动 generate

# gh-pages 部署
执行 `hexo g -d`,先生成静态页面再push到gh=pages部署
可控、路径可能不是根目录,需要注意主题资源路径

Problem V : .gitignore怎么书写

1
2
3
4
node_modules/
public/
.cache/
db.json

build配置

1
2
3
4
5
{
"scripts": {
"build": "npx hexo clean && npx hexo generate --config _config.yml,_config.vercel.yml"
}
}

Why npx?

1
2
3
Vercel、GitHub Actions 等环境一般没有全局 Hexo
使用 npx hexo generate 就能直接生成静态文件,无需额外安装
不然会报错:hexo generate cannnot found