fix(feed): 缓解首屏列表高度跳动,并去掉重复的 stats 请求
一、首屏「跳一下」
逐帧测量 postList 的容器高度,定位到两处叠加原因:
1. 骨架屏数量固定为 4 张,而骨架卡片按「理想内容」绘制(标题一行、
正文两三行、部分带缩略图),实际帖子往往更短。数据到达时容器高度
从 692px 骤降到 163px,下方内容整体上跳。
现改为 3 张,落差从 293px 收窄到 157px。
注意 feed.js 与 skeleton.js 两处都显式传了数量,改默认值不生效,
需同时修改。
2. 高度骤降本身没有缓冲。为 #postList 增加 min-height(桌面 420px、
移动端 320px),把变化吸收掉大部分——数据到达后容器高度由 163px
变为 406px。
二、/api/stats 重复请求
首屏会发出两次统计请求,间隔仅 11ms。原因是初始化流程在
loadCustomPagesNav 之后调用了一次 renderStats,而紧接着的页面分发
(switchPage('feed'))内部又调用一次。
去掉初始化处那次,并在页面分发后补一个条件调用:直接通过 URL 打开
帖子详情或用户主页时不经过 switchPage('feed'),侧边栏的统计与友链
需要在那里补一次,用 currentPage 判断避免与 feed 分支重复。
三、顺带清理
feed.js 中有一份与 app.js 同名的 renderStats 实现,因 app.js 后加载
而被整体覆盖,从未执行。已删除并留注释说明,避免日后改错地方。
实测(Puppeteer 逐帧采样 + 请求监听,1280×900):
- 首屏请求由 3 个(stats 重复)降为 2 个:posts 与 stats 各一次
- 容器高度序列由 692 → 684 → 409(落差 293)变为
540 → 534 → 406(落差 157)
- 直接访问 ?post=xxx 时详情正常渲染,侧边栏统计与友链均正确显示,
/api/stats 仅请求一次
完整测试套件 25/25 通过。
This commit is contained in:
1 parent
7176bea12c
commit
edc68744d5
4 files changed
+43
-15
No files matched your search
+4
-9
@@ -24,7 +24,7 @@ function renderFeed() {
|
||||
// 造成明显的闪动,反而比直接留白更糟。
|
||||
const isFirstLoad = container.dataset.loaded !== '1';
|
||||
container.innerHTML = isFirstLoad
|
||||
? Skeleton.postList(4)
|
||||
? Skeleton.postList(3)
|
||||
: '<div style="text-align:center;color:#94a3b8;padding:40px 0;">加载中...</div>';
|
||||
API.getPosts(currentSort, currentPageNum, PAGE_SIZE).then(result => {
|
||||
container.dataset.loaded = '1';
|
||||
@@ -218,14 +218,9 @@ function renderFeed() {
|
||||
});
|
||||
}
|
||||
|
||||
function renderStats() {
|
||||
API.getStats().then(stats => {
|
||||
document.getElementById('statTopics').textContent = stats.topics || 0;
|
||||
document.getElementById('statPosts').textContent = stats.posts || 0;
|
||||
document.getElementById('statUsers').textContent = stats.users || 0;
|
||||
// 在线人数已移除
|
||||
}).catch(() => {});
|
||||
}
|
||||
// 注:renderStats 定义在 app.js 中(同时更新主页与帖子详情页的统计,
|
||||
// 且带空值保护)。此处原先有一份同名实现,因 app.js 后加载而被整体
|
||||
// 覆盖,属于死代码,已删除以免日后改错地方。
|
||||
|
||||
function renderLinks() {
|
||||
API.getLinks().then(links => {
|
||||
|
||||
Reference in new issue
Block a user