From 8f6f9de4974ed015f7e1ac677ee7b26ecd77f504 Mon Sep 17 00:00:00 2001 From: albert4719 Date: Sun, 20 Sep 2026 21:03:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A3=80=E7=B4=A2=E9=A1=B5=EF=BC=9A=E6=AD=A3?= =?UTF-8?q?=E6=96=87=E5=88=86=E7=89=87=E5=8F=96=E5=A4=B1=E8=B4=A5=E6=97=B6?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E9=87=8D=E8=AF=95=E5=B9=B6=E4=BF=9D=E7=95=99?= =?UTF-8?q?=E5=B7=B2=E8=AF=BB=E9=83=A8=E5=88=86=EF=BC=8C=E6=8A=A5=E9=94=99?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E7=94=A9=E9=94=85=E6=96=87=E4=BB=B6=E4=B8=8D?= =?UTF-8?q?=E5=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 手机信号差时读到一半报 Failed to fetch,整页三十多个分片全白读一遍,而且 提示是「确认 README.md 和 book/ 目录都在 index.html 旁边」——文件在不在和 这种失败没关系,照着查只会白查。 - readText 自己退避重试三次(0.5s、2s),只对网络类失败重试:fetch 的 TypeError、超时中断,以及 5xx 和 429;4xx 不重试(文件真不在) - 每个请求加 20s 超时,信号差时挂住不返回的请求能变成一次重试 - 取到的正文存进 CORPUS_CACHE,重试只补没取到的那几个,已读的不重下 - 彻底失败时提示改成「网络中断,正文只读了一部分(取不到 book/13-…)」并给 一个「接着读」按钮;文件真缺(4xx)仍是原来那句查文件的提示 - 无头实测三种情形:掉两次自愈(552 张卡照常出)、一直失败时提示与按钮正确、 点「接着读」只重新请求 1 个文件并读完全书 --- index.html | 55 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 3528ae0..4c8ac62 100644 --- a/index.html +++ b/index.html @@ -227,6 +227,8 @@ abbr.term:hover{border-bottom-color:var(--brand-1);color:var(--brand-1)} .status code{font-family:var(--mono);font-size:13px;background:var(--bg-mute);padding:2px 6px;border-radius:4px} .status .bar{max-width:260px;height:3px;margin:14px auto 0;border-radius:2px;background:var(--bg-mute);overflow:hidden} .status .bar i{display:block;height:100%;width:0;border-radius:2px;background:var(--brand-1);transition:width .18s ease-out} +.status button{margin-top:14px;border:1px solid var(--divider);background:var(--bg-soft);color:var(--t1);border-radius:6px;padding:6px 16px;font:inherit;cursor:pointer} +.status button:hover{border-color:var(--brand-1);color:var(--brand-1)} .foot{margin-top:64px;padding-top:24px;border-top:1px solid var(--divider);color:var(--t3);font-size:13px;line-height:1.7} @media (max-width:960px){ @@ -1026,10 +1028,38 @@ function wire(){ } /* ---------- 启动 ---------- */ -async function readText(path){ - const res = await fetch(path, {cache:'no-cache'}); - if (!res.ok) throw new Error('http '+res.status+':'+path); - return res.text(); +// 一次要取 README 加三十多个正文文件,手机信号差时中间掉一个,整页就全军覆没 +// (2026-09-20 用户在手机上读到一半撞到 Failed to fetch)。所以:取到的存下来, +// 重试时不重下;网络类的失败自己先退避重试几次;再不行也只报「网络中断」并给 +// 一个接着读的按钮,别再让读者去查文件在不在——文件在不在和这种失败没关系。 +const CORPUS_CACHE = new Map(); +async function readText(path, tries = 3){ + if (CORPUS_CACHE.has(path)) return CORPUS_CACHE.get(path); + for (let i = 1; ; i++){ + let retry = false; + try { + // 信号差时请求可能挂着不返回,给它一个上限,超时按可重试处理 + const signal = AbortSignal.timeout ? AbortSignal.timeout(20000) : undefined; + const res = await fetch(path, {cache:'no-cache', signal}); + if (!res.ok){ + // 4xx 重试没意义(文件真不在),5xx 和 429 是服务端一时的事,值得再来一次 + retry = res.status >= 500 || res.status === 429; + throw new Error('http '+res.status+':'+path); + } + const text = await res.text(); + CORPUS_CACHE.set(path, text); + return text; + } catch (err) { + // fetch 的网络失败是 TypeError,超时和中断是 DOMException + const net = retry || err instanceof TypeError || /^(TimeoutError|AbortError)$/.test(err.name); + if (!net) throw err; + if (i >= tries){ + const e = new Error(`取不到 ${path}(网络中断或超时)`); + e.net = true; throw e; + } + await new Promise(r => setTimeout(r, 500 * i * i)); // 0.5s、2s 后再试 + } + } } (async function init(){ @@ -1084,9 +1114,20 @@ async function readText(path){ if (land) requestAnimationFrame(() => land.scrollIntoView()); } } catch (err) { - status.innerHTML = err.message === 'file' - ? '这个页面要通过 http 打开才能读取正文。在仓库目录运行 python -m http.server,然后访问 http://localhost:8000/;或直接用 GitHub Pages 地址。' - : `读取正文失败(${esc(String(err.message))})。确认 README.md 和 book/ 目录都在 index.html 旁边。`; + if (err.message === 'file'){ + status.innerHTML = '这个页面要通过 http 打开才能读取正文。在仓库目录运行 python -m http.server,然后访问 http://localhost:8000/;或直接用 GitHub Pages 地址。'; + return; + } + if (err.net){ + status.innerHTML = `网络中断,正文只读了一部分(${esc(String(err.message))})。已经读到的不用重下,换个网络或等信号稳一点,点这里接着读。` + + '
'; + document.getElementById('retry').addEventListener('click', () => { + status.innerHTML = '正在读取正文 …
'; + init(); + }); + return; + } + status.innerHTML = `读取正文失败(${esc(String(err.message))})。确认 README.md 和 book/ 目录都在 index.html 旁边。`; } })();