Skip to content

Commit fb73a78

Browse files
Beauty project use React and Tailwindcss (#13)
- current website is not so beautiful(which just render README.md to html) - define blog as a object with 4 propeties ``` name(string): who's blog url(string): blogs's url describe(string): introduce the blog, the author avatar(string): author's avatar url or location path ``` - use github action with `npm run deploy(vite build && gh-pages -d dist)`command to deploy website after code pushed to master branch
1 parent 7a8bb5f commit fb73a78

25 files changed

+6763
-134
lines changed

.github/workflows/convertMarkDownToHtml.yml

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Deploy to GitHub Pages
2+
3+
on:
4+
push:
5+
branches: [ "master" ]
6+
# Allow manual trigger
7+
workflow_dispatch:
8+
9+
permissions:
10+
contents: write
11+
12+
jobs:
13+
build-and-deploy:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v4
18+
19+
- name: Setup Node.js
20+
uses: actions/setup-node@v4
21+
with:
22+
node-version: '20'
23+
cache: 'npm'
24+
25+
- name: Install dependencies
26+
run: npm ci
27+
28+
- name: Build & Deploy to gh-pages
29+
env:
30+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
run: |
32+
npm run deploy
33+
34+
- name: Upload artifact (dist) for reference
35+
if: success()
36+
uses: actions/upload-artifact@v4
37+
with:
38+
name: dist
39+
path: dist

.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?

.python-version

Lines changed: 0 additions & 1 deletion
This file was deleted.

AI_Document.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# Avatar 本地资源加载说明
2+
3+
`blogs.json` 中,有的条目的 `avatar` 字段是完整的远程 URL(以 https:// 开头),有的条目使用了相对文件名,比如 `avatar.png`
4+
5+
问题:
6+
之前 `avatar` 写成 `avatar.png` 时,页面 `<img src="avatar.png" />` 会向站点根路径请求 `/avatar.png`,但实际文件位于 `src/assets/avatar.png`,导致 404 无法显示。
7+
8+
解决方案:
9+
`App.tsx` 中新增:
10+
1. 使用 `import.meta.glob('./assets/*', { eager: true, import: 'default' })` 预加载 `src/assets` 下资源,Vite 会返回它们打包后的真实 URL。
11+
2. 新增 `resolveAvatar` 函数:
12+
- 如果是 `http(s)://``data:` 开头,直接返回。
13+
- 否则尝试用 `./assets/文件名` 去匹配 `assetAvatars` 映射。
14+
- 匹配成功返回打包后的路径,失败则原样返回(兼容未来放入 `public/` 的情况)。
15+
16+
这样 `blogs.json` 里写成 `"avatar": "avatar.png"` 也能自动正确显示。
17+
18+
使用说明:
19+
- 如果想添加新的本地头像文件,把图片放入 `src/assets/`,然后在 `blogs.json` 里写文件名即可。
20+
- 如果使用远程链接,直接写完整 URL。
21+
- 如果未来想放到 `public/` 目录(例如 `public/custom.png`),则可以直接在 JSON 中写 `"custom.png"``/custom.png`,但建议保持一致:
22+
- `src/assets` 内的文件:写文件名。
23+
- 远程文件:写完整 URL。
24+
25+
可能的扩展:
26+
- 增加一个类型检测,验证本地文件是否存在,不存在在控制台警告。
27+
- 支持子目录:可以把 glob 改成 `./assets/**/*`
28+
29+
如需再改进或遇到其他加载问题,可以继续提出。
30+
31+
---
32+
33+
# GitHub Actions 自动部署到 GitHub Pages 说明
34+
35+
本仓库已添加工作流:`.github/workflows/deployToGithubPages.yml`
36+
37+
## 触发方式
38+
1. 推送到 `master` 分支(`git push origin master`)。
39+
2. 手动触发:在 GitHub 仓库页面 -> Actions -> 选择该工作流 -> Run workflow。
40+
41+
## 工作流执行步骤概览
42+
| 步骤 | 说明 |
43+
| ---- | ---- |
44+
| checkout | 拉取仓库最新代码 |
45+
| setup-node | 使用 Node.js 20 并启用 npm 缓存 |
46+
| npm ci | 安装依赖(更快且保证与 lockfile 一致) |
47+
| npm run deploy | 执行 `vite build && gh-pages -d dist``dist` 发布到 `gh-pages` 分支 |
48+
| upload artifact | (可选)上传构建产物供调试/下载 |
49+
50+
## `npm run deploy` 细节
51+
脚本定义:`vite build && gh-pages -d dist`
52+
53+
`gh-pages` 包会:
54+
1. 如果没有 `gh-pages` 分支则创建。
55+
2.`dist` 目录内容强制推送到该分支根目录。
56+
3. 使用默认提供的 `GITHUB_TOKEN` 进行认证(工作流中已通过 `env` 注入)。
57+
58+
## 首次配置 GitHub Pages
59+
1. 打开仓库 Settings -> Pages。
60+
2. Source 选择:`Deploy from a branch`
61+
3. Branch 选择:`gh-pages`,文件夹保持 `/ (root)`
62+
4. 保存,等待几分钟即可访问。
63+
64+
## 自定义域名 (CNAME)
65+
仓库根目录已有 `CNAME` 文件;`gh-pages` 发布后会自动包含在构建产物里(确保 `dist` 中最终包含该文件)。如果发现被覆盖:
66+
1. 可在 `vite.config.ts` 中通过 `build.copyPublicDir` 确保 `public/CNAME` 被复制。
67+
2. 或在 deploy 前手动将根目录 `CNAME` 复制到 `dist/`:可以改写脚本:
68+
```jsonc
69+
"deploy": "vite build && cp CNAME dist/CNAME && gh-pages -d dist"
70+
```
71+
72+
当前如果访问不到自定义域名,请确认:
73+
- DNS A 记录 或 CNAME 记录 已指向 `username.github.io`
74+
- Pages 设置里成功识别该域名(会显示绿色小盾牌或正在验证)。
75+
76+
## 常见问题排查
77+
| 问题 | 可能原因 | 解决 |
78+
| ---- | -------- | ---- |
79+
| 页面空白/404 | 路由使用 BrowserHistory 且未配置 base | 若为纯静态单页无需更改;多路由建议使用 Hash 路由或在 `vite.config.ts` 设置 `base: '/<repo>/'` (如果不是自定义域名场景) |
80+
| 样式不生效 | 构建 `base` 前缀错误 | 检查 `vite.config.ts``base` 是否匹配部署路径 |
81+
| CNAME 丢失 | 未复制到 dist | 参见上面 CNAME 处理 |
82+
| 构建失败 | 缓存损坏或 lock 不一致 | 重跑工作流或删除 `node_modules` 后本地验证 `npm ci` |
83+
84+
## 手动本地验证发布内容
85+
```
86+
npm ci
87+
npm run build
88+
npx serve dist # 或者 npx http-server dist
89+
```
90+
91+
## 后续可改进
92+
1. 添加缓存命中统计(actions/cache 手动控制)。
93+
2.`gh-pages` 步骤替换为官方 Pages Action(`actions/deploy-pages`)并开启构建产物保护。
94+
3. 增加一个工作流检测 `blogs.json` 的结构有效性。
95+
96+
如需我继续改成官方 Pages 方案或增强校验,请直接告诉我。
97+
98+
---
99+
100+
# 404 问题排查与修复记录 (2025-09-15)
101+
102+
## 现象
103+
在自定义域名 `https://friends.yitianyigexiangfa.com/` 访问页面时,浏览器控制台出现若干静态资源(JS/CSS)返回 404,页面白屏或样式缺失。
104+
105+
## 根因分析
106+
`vite.config.ts` 中配置了:
107+
```ts
108+
base: '/awesome-blogs/'
109+
```
110+
该配置适用于部署在 `https://<username>.github.io/awesome-blogs/` 这种“仓库二级路径”场景;但当前仓库使用 **自定义独立域名**,Pages 会把站点挂在域名根路径 `/`。继续带前缀会导致最终 HTML 中引用:
111+
```
112+
<script src="/awesome-blogs/assets/index-xxxx.js"></script>
113+
```
114+
而真实发布路径其实是:
115+
```
116+
/assets/index-xxxx.js
117+
```
118+
因此所有带 `/awesome-blogs/` 前缀的资源请求均 404。
119+
120+
## 修复措施
121+
1. 修改 `vite.config.ts`
122+
```diff
123+
- base: '/awesome-blogs/',
124+
+ base: '/',
125+
```
126+
2. 重新执行 `npm run build` / 部署。
127+
3. 刷新页面(必要时清缓存 / 强刷)。
128+
129+
## 验证步骤
130+
本地:
131+
```
132+
npm run build
133+
npx serve dist # 打开 http://localhost:3000 或 serve 输出的端口
134+
```
135+
查看生成的 `dist/index.html` 中静态资源引用前缀应为:`/assets/...` 或相对路径,不再含 `/awesome-blogs/`
136+
137+
## 何时应该使用非根 base
138+
| 场景 | base 设置 |
139+
| ---- | --------- |
140+
| GitHub Pages 默认:`https://<user>.github.io/<repo>/` | `base: '/<repo>/'` |
141+
| 自定义域名:`CNAME -> friends.yitianyigexiangfa.com` | `base: '/'` |
142+
| 反向代理挂载到子路径:`https://domain.com/friends/` | `base: '/friends/'` |
143+
144+
## 额外建议(可选)
145+
1. 若未来改用 GitHub Pages 官方 Actions(`actions/deploy-pages`),可在 workflow 中显式写入构建产物;当前 `gh-pages` 包方式保持即可。
146+
2. 可在本地加一个小脚本自动检测 `dist/index.html` 中是否仍残留错误前缀,避免回归。
147+
3. 如果需要保留旧链接(/awesome-blogs/)做兼容,可在根目录放一个 `awesome-blogs/index.html`,里边用 `<meta http-equiv="refresh">` 方式 301/模拟跳转到根。
148+
149+
---

docs/CNAME

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/index.html

Lines changed: 0 additions & 47 deletions
This file was deleted.

eslint.config.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import js from '@eslint/js'
2+
import globals from 'globals'
3+
import reactHooks from 'eslint-plugin-react-hooks'
4+
import reactRefresh from 'eslint-plugin-react-refresh'
5+
import tseslint from 'typescript-eslint'
6+
import { globalIgnores } from 'eslint/config'
7+
8+
export default tseslint.config([
9+
globalIgnores(['dist']),
10+
{
11+
files: ['**/*.{ts,tsx}'],
12+
extends: [
13+
js.configs.recommended,
14+
tseslint.configs.recommended,
15+
reactHooks.configs['recommended-latest'],
16+
reactRefresh.configs.vite,
17+
],
18+
languageOptions: {
19+
ecmaVersion: 2020,
20+
globals: globals.browser,
21+
},
22+
},
23+
])

index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Friends</title>
7+
</head>
8+
<body>
9+
<div id="root"></div>
10+
<script type="module" src="/src/main.tsx"></script>
11+
</body>
12+
</html>

main.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)