将 src/main.ts 构建为静态页面,发布到 GitHub Pages,访客可直接在浏览器操作地图 Demo。
推送 main 分支并成功部署后:
https://<你的用户名>.github.io/<仓库名>/
例如仓库名为 grid-webgl-openlayers:
https://octocat.github.io/grid-webgl-openlayers/
README 中的 Demo 徽章链接格式相同。
main(或 master)分支| 文件 | 作用 |
|---|---|
.github/workflows/deploy-pages.yml |
push 后自动构建并部署 |
scripts/prepare-demo-data.mjs |
将 data/arrayData.json 复制到 public/data/ |
vite.config.ts demo 模式 |
设置 base 路径,适配 /<仓库名>/ |
npm run build:demo |
输出到 dist-demo/ |
npm install
# 按仓库名设置 base(与 GitHub 上一致)
# Windows PowerShell:
$env:VITE_BASE_PATH="/grid-webgl-openlayers/"; npm run build:demo
# macOS / Linux:
VITE_BASE_PATH=/grid-webgl-openlayers/ npm run build:demo
npm run preview:demo
浏览器打开终端提示的地址(通常为 http://localhost:4173/grid-webgl-openlayers/)。
本地开发仍用:
npm run dev
GitHub 仓库 → Actions → Deploy GitHub Pages Demo → Run workflow
/src/main.ts说明浏览器加载的是源码版 index.html(开发用),不是 Actions 构建的 dist-demo。
原因 A:访问了错误地址
项目页 Demo 必须带仓库名:
✅ https://openlayer-computer.github.io/grid-webgl-openlayers/
❌ https://openlayer-computer.github.io/
❌ https://openlayer-computer.github.io/src/main.ts
原因 B:Pages 发布源配置错误
/ (root))原因 C:Actions 未成功
构建产物里的 index.html 应引用 /仓库名/assets/index-xxx.js,而不是 /src/main.ts。本地可验证:
VITE_BASE_PATH=/grid-webgl-openlayers/ npm run build:demo
grep main.ts dist-demo/index.html # 应无输出
gh-pages 分支)VITE_BASE_PATH 与仓库名一致,格式为 /仓库名/(首尾都要有 /)xxx,无需改代码,CI 会自动使用新仓库名Demo 依赖 public/data/arrayData.json,由 prebuild:demo 从 data/arrayData.json 复制。确保示例数据已提交到仓库。
username.github.io 根仓库若仓库名恰好是 <用户名>.github.io,Pages 根路径为 /,需在 workflow 中改:
env:
VITE_BASE_PATH: /