Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(web): 补齐图标声明与兜底路径,书签不再画默认地球

longmeidao3e4db189bd3e
diff --git a/src/peach/routes_auth.py b/src/peach/routes_auth.py
index 0dc4a5980..a3402fb66 100644
--- a/src/peach/routes_auth.py
+++ b/src/peach/routes_auth.py
@@ -124,6 +124,9 @@ def login_html(next_path: str, *, invalid: bool = False) -> str:
'<!doctype html><html lang="zh-CN"><meta charset="utf-8">'
'<meta name="viewport" content="width=device-width,initial-scale=1">'
'<meta name="color-scheme" content="light dark"><title>登录 Peach</title>'
+ # 图标声明和主站同一份。书签地址是 `/`,没有会话时这一页就是它实际停在的地方:
+ # 这里不声明,浏览器只会去要 `/favicon.ico`,把「这个站没有图标」记进书签。
+ '<link rel="icon" href="/favicon.ico" type="image/x-icon">'
# 这一页在拿到 cookie 之前就要出图,取不到 /app.css,所以色板在这儿留一份最小副本。
# 三条分支和 web/css/01-base.css 同构:默认浅色、系统深色、手动选的那一档压过系统。
# 选择由下面这段脚本在第一次绘制前读出来——固定浅色的人不该在登录页先看一眼深色。
diff --git a/src/peach/routes_pages.py b/src/peach/routes_pages.py
index 4fafee6fe..578bc69a3 100644
--- a/src/peach/routes_pages.py
+++ b/src/peach/routes_pages.py
@@ -161,7 +161,7 @@ stroke-linejoin:round}
_SETUP_HEAD = ('<!doctype html>\n<html lang="zh-CN"><head><meta charset="utf-8">'
'<meta name="viewport" content="width=device-width,initial-scale=1">'
'<meta name="color-scheme" content="light dark">'
- '<link rel="icon" href="/peach-logo.png" type="image/png">')
+ '<link rel="icon" href="/favicon.ico" type="image/x-icon">')
#: 键 -> 页面上的题目与一句说明。顺序、默认值与校验仍然来自 `onboarding.questions()`,
#: 这里只决定同一道题在浏览器里怎么称呼:命令行那份题面要把可选值写进去,页面用控件表达。
@@ -595,7 +595,7 @@ def _normal_url(config) -> str:
def asset_response(request: Request, path: Path, media: str) -> Response:
- """页面资产用 ETag 复验代替 no-store,`/app.js`、`/js/`、`/dist/` 共用。
+ """页面资产与图标用 ETag 复验代替 no-store,`/app.js`、`/js/`、`/dist/`、图标共用。
`/app.css` 拼多份分区,ETag 口径见 `stylesheet_response()`,其余照这里。
@@ -611,7 +611,10 @@ def asset_response(request: Request, path: Path, media: str) -> Response:
if request.headers.get("if-none-match") == etag:
response: Response = Response(status_code=304)
else:
- response = FileResponse(path, media_type=f"{media}; charset=utf-8")
+ # charset 只对文本类型成立。图标是字节流,声明里挂一个字符集,在全站的
+ # `X-Content-Type-Options: nosniff` 之下就是让浏览器照一个自相矛盾的类型解码。
+ media_type = f"{media}; charset=utf-8" if media.startswith("text/") else media
+ response = FileResponse(path, media_type=media_type)
response.headers["ETag"] = etag
response.headers["Cache-Control"] = "no-cache"
return response
@@ -849,9 +852,20 @@ def favicon():
return response
[email protected]_route("/favicon.ico", methods=["GET", "HEAD"])
+def favicon_ico(request: Request):
+ """页面没有声明图标时浏览器按这个固定路径取,书签与历史记录也从这里拿。
+
+ 发 `resources/peach.ico`:它和 `peach-logo.png` 同出一张原图,里面已经装好
+ 16 到 256 七档尺寸,浏览器按需要挑一档,不必为一枚 16px 的角标下载
+ 1024×1024 的 PNG。声明写在每个页面的 head 里,这条路径是声明之外的兜底。
+ """
+ return asset_response(request, PROJECT_ROOT / "resources" / "peach.ico", "image/x-icon")
+
+
@router.api_route("/peach-logo.png", methods=["GET", "HEAD"])
-def peach_logo():
- return FileResponse(PROJECT_ROOT / "resources" / "peach-logo.png", media_type="image/png")
+def peach_logo(request: Request):
+ return asset_response(request, PROJECT_ROOT / "resources" / "peach-logo.png", "image/png")
@router.api_route("/item/{item_id}", methods=["GET", "HEAD"])
diff --git a/tests/test_fastapi_api.py b/tests/test_fastapi_api.py
index 1543daef6..242787da6 100644
--- a/tests/test_fastapi_api.py
+++ b/tests/test_fastapi_api.py
@@ -334,8 +334,28 @@ class FastApiContractTests(unittest.IsolatedAsyncioTestCase):
async def test_peach_logo_is_served_as_png(self):
response = await self.client.get("/peach-logo.png")
self.assertEqual(response.status_code, 200)
+ # 类型声明逐字相等:图片是字节流,`asset_response` 不许给它挂 charset。
self.assertEqual(response.headers["content-type"], "image/png")
self.assertTrue(response.content.startswith(b"\x89PNG\r\n\x1a\n"))
+ self.assertEqual(response.headers["cache-control"], "no-cache")
+
+ async def test_the_favicon_is_served_without_a_session_and_revalidates(self):
+ """浏览器取图标时手里没有会话,这条路径因此不设防,也不跳登录页。
+
+ 发的是 `resources/peach.ico`:里面装着 16 到 256 七档尺寸,浏览器挑一档就够,
+ 不必为一枚 16px 的角标下载 1024×1024 的 PNG。缓存走 ETag 复验——图标几个版本
+ 才动一次,每次开页重下没有道理,而复验又让换了图的那一次立刻生效。
+ """
+ response = await self.client.get("/favicon.ico")
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.headers["content-type"], "image/x-icon")
+ # ICO 的固定文件头:保留位 0、类型 1。
+ self.assertTrue(response.content.startswith(b"\x00\x00\x01\x00"))
+ self.assertEqual(response.headers["cache-control"], "no-cache")
+ again = await self.client.get(
+ "/favicon.ico", headers={"If-None-Match": response.headers["etag"]},
+ )
+ self.assertEqual(again.status_code, 304)
async def test_front_end_modules_are_served_and_the_name_cannot_escape(self):
"""ES module 拆分之后新增的静态路由。
@@ -887,6 +907,16 @@ class FastApiContractTests(unittest.IsolatedAsyncioTestCase):
self.assertEqual(accepted.headers["location"], "/stats")
self.assertNotIn("secret", accepted.headers["location"])
+ async def test_the_login_page_declares_the_icon_like_every_other_page(self):
+ """书签地址是 `/`,没有会话时浏览器实际停在这一页,图标要在这里就声明。
+
+ 缺声明的后果不止是这一页少个角标:浏览器改去要 `/favicon.ico`,并把那一次的
+ 结果当成整个站的图标记进书签。声明和兜底路径都在,才不依赖谁先谁后。
+ """
+ page = await self.client.get("/login?next=/")
+ self.assertEqual(page.status_code, 200)
+ self.assertIn('<link rel="icon" href="/favicon.ico" type="image/x-icon">', page.text)
+
async def test_the_login_page_follows_the_chosen_theme(self):
"""登录页在拿到 cookie 之前出图,所以它自带一份最小色板,跟着同一个选择走。
diff --git a/tests/test_onboarding.py b/tests/test_onboarding.py
index c9f67e76f..87217dd25 100644
--- a/tests/test_onboarding.py
+++ b/tests/test_onboarding.py
@@ -456,6 +456,8 @@ class SetupPageTests(_Case):
self.assertLess(body.index('id="dirs"'), body.index('id="add-dir"'))
# 品牌标记在标题上方,说明文字在标题下方。
self.assertIn('<img class="mark" src="/peach-logo.png"', body)
+ # 设置页往往是这台机器上第一个被打开、被加书签的地址,图标声明不能少。
+ self.assertIn('<link rel="icon" href="/favicon.ico" type="image/x-icon">', body)
self.assertLess(body.index("<h1>"), body.index('class="lede"'))
# 四个手填字段的标签末尾标红星;「谁可以访问」总有一个选中项,不标。
self.assertEqual(body.count('<span class="req"'), 4)
diff --git a/tests/test_web_ui.py b/tests/test_web_ui.py
index c6db82d5c..b389824b0 100644
--- a/tests/test_web_ui.py
+++ b/tests/test_web_ui.py
@@ -1459,8 +1459,13 @@ class WebUiSourceTests(unittest.TestCase):
self.assertPageLacks('onload="if(this.naturalWidth')
self.assertPageContains("img.addEventListener('error',fallback")
- def test_brand_icon_uses_shared_square_png(self):
- self.assertPageContains('<link rel="icon" href="/peach-logo.png" type="image/png">')
+ def test_brand_icon_declares_the_multi_size_ico_and_draws_the_square_png(self):
+ """标签页角标和页面里的品牌标记同出一张原图,取的却是两份资源。
+
+ 角标最大用到 32px,声明指向 `/favicon.ico`:那一份装着 16 到 256 七档尺寸,
+ 浏览器挑一档就够。页面里的标记要按 CSS 尺寸缩放,用 PNG。
+ """
+ self.assertPageContains('<link rel="icon" href="/favicon.ico" type="image/x-icon">')
self.assertPageContains('<img class="mark" src="/peach-logo.png" alt="">')
def test_global_navigation_and_controls_have_accessible_context(self):
diff --git a/web/index.html b/web/index.html
index 2c9ae13d4..bff758ba6 100644
--- a/web/index.html
+++ b/web/index.html
@@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<meta name="theme-color" content="#FFFFFF" media="(prefers-color-scheme: light)" data-theme-color="light">
<meta name="theme-color" content="#080A0D" media="(prefers-color-scheme: dark)" data-theme-color="dark">
-<link rel="icon" href="/peach-logo.png" type="image/png">
+<link rel="icon" href="/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="/vendor/videojs/8.24.0/video-js.min.css">
<title>Peach · 蜜桃</title>
<link rel="stylesheet" href="/app.css">