fix(worker): 修复寄卖页分片加载失败导致的白屏

- 新增全局/页面级错误边界,页面渲染异常不再整页白屏
- 监听 vite:preloadError,部署后旧分片加载失败自动刷新一次
- 修复长会话 setTimeout 2^31 溢出导致立即登出
- 寄卖页资料接口失败给出重试态,不再误报未开通
This commit is contained in:
2026-11-13 16:19:17 +08:00
parent dd0c56c265
commit d8793db593
9 changed files with 389 additions and 9 deletions
@@ -0,0 +1,71 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import {
CHUNK_RELOAD_GUARD_MS,
isChunkLoadError,
shouldAutoReloadChunkFailure,
} from '../src/utils/chunk-load-error.ts'
function createStorage(initial: Record<string, string> = {}) {
const values = new Map(Object.entries(initial))
return {
getItem(key: string) {
return values.get(key) ?? null
},
setItem(key: string, value: string) {
values.set(key, String(value))
},
}
}
test('识别浏览器动态 import 失败文案', () => {
assert.equal(
isChunkLoadError(
new TypeError(
'Failed to fetch dynamically imported module: https://worker.khhao.com/assets/WorkerConsignmentPage-abc.js',
),
),
true,
)
assert.equal(isChunkLoadError(new Error('Importing a module script failed.')), true)
assert.equal(isChunkLoadError(new Error('Load failed')), true)
assert.equal(
isChunkLoadError({ name: 'ChunkLoadError', message: 'Loading chunk 12 failed' }),
true,
)
})
test('不把普通业务错误当成分片失败', () => {
assert.equal(isChunkLoadError(new Error('接口请求失败')), false)
assert.equal(isChunkLoadError(null), false)
assert.equal(isChunkLoadError(undefined), false)
})
test('首次分片失败允许自动刷新,窗口内再次失败不再刷新', () => {
const storage = createStorage()
const now = 1_700_000_000_000
assert.equal(shouldAutoReloadChunkFailure(storage, now), true)
assert.equal(shouldAutoReloadChunkFailure(storage, now + 1_000), false)
assert.equal(shouldAutoReloadChunkFailure(storage, now + CHUNK_RELOAD_GUARD_MS - 1), false)
})
test('超过防抖窗口后允许再次自动刷新', () => {
const storage = createStorage()
const now = 1_700_000_000_000
assert.equal(shouldAutoReloadChunkFailure(storage, now), true)
assert.equal(shouldAutoReloadChunkFailure(storage, now + CHUNK_RELOAD_GUARD_MS), true)
})
test('存储不可用时不做自动刷新,避免无限循环', () => {
const storage = {
getItem() {
throw new Error('denied')
},
setItem() {
throw new Error('denied')
},
}
assert.equal(shouldAutoReloadChunkFailure(storage), false)
assert.equal(shouldAutoReloadChunkFailure(null), false)
})
@@ -0,0 +1,34 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import {
MAX_TIMER_DELAY_MS,
hasRemainingTimerDelay,
resolveTimerChunkDelay,
} from '../src/utils/session-expiry-timer.ts'
const DAY_MS = 24 * 60 * 60 * 1000
test('7 天会话有效期不需要切段', () => {
assert.equal(resolveTimerChunkDelay(7 * DAY_MS), 7 * DAY_MS)
assert.equal(hasRemainingTimerDelay(7 * DAY_MS), false)
})
test('30 天会话有效期超过 32 位上限时切段,避免 setTimeout 溢出后立即触发', () => {
const thirtyDays = 30 * DAY_MS
assert.ok(thirtyDays > MAX_TIMER_DELAY_MS)
assert.equal(resolveTimerChunkDelay(thirtyDays), MAX_TIMER_DELAY_MS)
assert.equal(hasRemainingTimerDelay(thirtyDays), true)
})
test('刚好等于上限时不切段', () => {
assert.equal(resolveTimerChunkDelay(MAX_TIMER_DELAY_MS), MAX_TIMER_DELAY_MS)
assert.equal(hasRemainingTimerDelay(MAX_TIMER_DELAY_MS), false)
})
test('已过期/非法延迟按 0 处理', () => {
assert.equal(resolveTimerChunkDelay(0), 0)
assert.equal(resolveTimerChunkDelay(-1), 0)
assert.equal(resolveTimerChunkDelay(Number.NaN), 0)
assert.equal(hasRemainingTimerDelay(Number.NaN), false)
})