32
.workbuddy/memory/2026-08-22.md
Normal file
32
.workbuddy/memory/2026-08-22.md
Normal file
@ -0,0 +1,32 @@
|
||||
# 2026-08-22 工作记忆
|
||||
|
||||
## 重构:把 SSR / SSG 请求 base URL 从 astro 文件抽离到 lib
|
||||
用户要求:重新封装 SSR 和 SSG 请求,baseurl 不要放在 .astro 文件里。
|
||||
|
||||
### 改动清单
|
||||
- **新建 `src/lib/ssr.ts`**:集中 SSR(按需渲染)数据客户端。
|
||||
- `ssrBase()`:解析优先级 `API_SSR` > `PUBLIC_API_BASE` > `http://localhost:8090`(替代之前散布在 article 页面的内联写法)。
|
||||
- `getSsrArticle(id)`:包揽「缺 id / 取数失败 / 成功」三态,返回 `{article, error}`。
|
||||
- **`src/pages/en/article.astro` / `cn/article.astro`**:删除 `const API = (import.meta.env.API_SSR || PUBLIC_API_BASE || "http://localhost:8090")...`,改用 `getSsrArticle(id)`;不再给组件传 `apiBase`。
|
||||
- **`src/pages/en/items/[id].astro`**:删掉**硬编码** `const API = "http://localhost:8090"`(最严重的一处)和调试 `console.log(id/article)`,改用 `getSsrArticle(id)`。
|
||||
- **`src/components/pages/article.astro` 与 `RunwayLooksDetail.astro`**:移除 `apiBase` prop,改用 `ssrBase()` 内部解析图片绝对地址(原来 `apiBase` 只用于服务端 frontmatter 的 `full()`,浏览器 `<script>` 用的是服务端注入的 `images` 数据,不依赖该 prop)。
|
||||
- **`src/lib/api.ts`** 新增供客户端脚本复用:
|
||||
- `publicBase()`:仅 `PUBLIC_API_BASE`(空→同源相对 `/api`),与 `getBase()`(含 SSR/DEV 覆盖)区分。
|
||||
- `toAbsPublic(url)`:基于 `publicBase()` 的相对路径补全。
|
||||
- `fetchArticlesList({page,size,collection_type,season,brand_id})`:替代 articles.astro 内联的 `fetch('/api/public/articles?...')`。
|
||||
- **`src/components/pages/articles.astro` 客户端脚本**:删除内联 `const API_BASE = import.meta.env.PUBLIC_API_BASE...` 与 `toAbs` 闭包,改用 `fetchArticlesList` + `toAbsPublic`;并补声明原本**未定义**却被引用的 `let fType = ""` / `let fSeason = ""`(之前因 `!API_BASE` 提前 return 才不报错,属潜在 bug)。
|
||||
- 顺手清理调试日志:`data.ts` 的 `fetchArticleById` 内 `console.log`、 `ssg.ts` 的 `getSsgPopularBrands` 内 `console.log(j)`。
|
||||
|
||||
### 关于 SSG
|
||||
SSG 请求本身**已封装在 `src/lib/ssg.ts`**(`ssgBase()` / `getSsgBrands` / `getSsgPopularBrands` / `getSsgArticleIds`,基 `SSG_API`> `API_SSR` > `http://localhost:8091`),`brands.astro` 与 `Index.astro` 在用,本次未改动其结构,仅清掉一处调试日志。
|
||||
|
||||
### 验证
|
||||
- `npm run build` 通过(Complete!)。
|
||||
- Lint 0 报错。
|
||||
- `astro dev` 下 `/en/article?id=1` → 200、`/en/items/1` → 200(后端离线时走 error 态,不影响编译/路由)。
|
||||
|
||||
### 文件职责(请求层)
|
||||
- `ssr.ts`:运行期按需 SSR,调 `/api/public/*`,base=API_SSR。
|
||||
- `ssg.ts`:构建期 Node,调 `/api/ssg/*`,base=SSG_API(内部端口 8091)。
|
||||
- `api.ts`:客户端运行期通用 API(RunwayLooks 等用 `getBase()`;articles 用 `publicBase()`)。
|
||||
- `data.ts`:网站统一数据层(首页/列表 getter),`API_BASE` 仍基于 `PUBLIC_API_BASE`,属 lib 内逻辑、不在 astro 文件。
|
||||
Reference in New Issue
Block a user