fix: 发版后软刷新检测入口壳并在 chunk 404 时硬刷新
避免 Logo/下拉刷新仍跑旧 index.js,以及站内消息预取失败弹出英文 toast。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { isChunkLoadError, reloadForStaleChunk, reloadIfShellStale } from './chunkLoad';
|
||||
import { prefetchLayoutShell, prefetchRoute } from './prefetchRoute';
|
||||
import { doneTransition, startTransition } from './spaTransition';
|
||||
|
||||
@@ -10,20 +11,28 @@ export type SoftRefreshOpts = {
|
||||
};
|
||||
|
||||
/**
|
||||
* 软刷新当前页:预热齐套后派发一次 commit。
|
||||
* 软刷新当前页:先检测入口壳是否过期,再预热齐套后派发一次 commit。
|
||||
* `progress: true` 时走顶栏进度条。
|
||||
*/
|
||||
export async function softRefreshCurrentPage(to?: string, opts?: SoftRefreshOpts): Promise<void> {
|
||||
const path = to ?? `${window.location.pathname}${window.location.search}`;
|
||||
const id = opts?.progress ? startTransition() : undefined;
|
||||
try {
|
||||
// 发版后旧壳仍在内存:先对比入口 script,过期则硬刷新
|
||||
if (await reloadIfShellStale()) {
|
||||
return;
|
||||
}
|
||||
await Promise.all([
|
||||
prefetchRoute(path, { force: true }),
|
||||
prefetchLayoutShell({ force: true }),
|
||||
]);
|
||||
} catch {
|
||||
// 仍派发 commit,让界面有机会用已有缓存自愈
|
||||
} catch (e: unknown) {
|
||||
if (isChunkLoadError(e) && reloadForStaleChunk()) {
|
||||
return;
|
||||
}
|
||||
// 其它错误仍派发 commit,让界面有机会用已有缓存自愈
|
||||
} finally {
|
||||
if (id != null) doneTransition(id);
|
||||
}
|
||||
window.dispatchEvent(new Event(PAGE_SOFT_REFRESH_COMMIT_EVENT));
|
||||
if (id != null) doneTransition(id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user