GitHub Pages 部署后,CSS 样式不加载,页面显示无样式的 HTML。
当部署到 GitHub Pages 的子路径时(如 https://bjlida.github.io/Cursor-Model-Guide/),Next.js 需要知道应用的基础路径,否则它会从根路径(/)加载 CSS 和 JavaScript 文件,导致 404 错误。
错误的资源路径:
https://bjlida.github.io/_next/static/css/app.css ❌ 404 Not Found
正确的资源路径:
https://bjlida.github.io/Cursor-Model-Guide/_next/static/css/app.css ✅
在 next.config.js 中添加 basePath 配置:
const nextConfig = {
output: 'export',
basePath: '/Cursor-Model-Guide', // 添加这一行
images: {
unoptimized: true,
},
trailingSlash: true,
}npm run buildgit add next.config.js
git commit -m "Fix: Add basePath for GitHub Pages deployment"
git push origin main- 访问:https://github.com/bjlida/Cursor-Model-Guide/actions
- 等待工作流完成(约 2-5 分钟)
- 状态显示为绿色勾号 ✓
访问:https://bjlida.github.io/Cursor-Model-Guide/
检查项:
- ✅ 页面有样式(渐变背景、卡片样式等)
- ✅ 所有交互功能正常
- ✅ 浏览器控制台无 404 错误
Windows: Ctrl + Shift + R
Mac: Cmd + Shift + R
- 打开浏览器开发者工具(F12)
- 右键点击刷新按钮
- 选择"清空缓存并硬性重新加载"
- 访问:https://github.com/bjlida/Cursor-Model-Guide/actions
- 点击最新的工作流运行
- 查看构建和部署日志
- 确认没有错误
# 查看构建输出的 HTML
cat out/index.html | grep "_next"
# 应该看到类似这样的路径:
# /Cursor-Model-Guide/_next/static/...npm run dev
# 访问: http://localhost:3000
# basePath 在开发模式下会自动处理npm run build
npm run preview
# 访问: http://localhost:3000/Cursor-Model-Guide/
# 注意:需要包含 /Cursor-Model-Guide/ 路径basePath: '/Cursor-Model-Guide'访问地址:https://bjlida.github.io/Cursor-Model-Guide/
如果使用自定义域名(如 your-domain.com):
// 不需要 basePath,或者设置为空
basePath: ''访问地址:https://your-domain.com/
// 不需要 basePath
// 这些平台会自动处理部署后,检查以下项目:
- 访问网站,页面有完整样式
- 打开浏览器控制台(F12)→ Network 标签
- 刷新页面
- 确认所有 CSS 和 JS 文件都是 200 状态(不是 404)
- 确认资源路径包含
/Cursor-Model-Guide/
✅ 已修复:添加了 basePath: '/Cursor-Model-Guide' 配置
✅ 已构建:项目重新构建成功
📤 待执行:推送代码到 GitHub 触发自动部署
下一步:
git add next.config.js
git commit -m "Fix: Add basePath for GitHub Pages deployment"
git push origin main几分钟后访问 https://bjlida.github.io/Cursor-Model-Guide/ 验证修复!🎉