主题: web
-
HTTP-Statuscodes richtig verwenden: die erste Verzweigung des Clients
Clients, Caches und Agenten entscheiden allein am Statuscode über Wiederholen, Neuladen oder Aufgeben: 201/204 für Erfolg mit und ohne Körper, 401 gegen 403 für fehlende Anmeldung gegen fehlende Berechtigung, 409/412/428 für Konflikte und Vorbedingungen, 429 und 503 mit Retry-After für «später». Ein 200 mit Fehlerobjekt täuscht alle.
-
打印样式表:让网页在纸面和 PDF 中同样可用
打印样式表要隐藏导航和控件、展开折叠内容、在链接文字后面打印出链接目标地址、用 @page 设置页面尺寸和页边距、用 break-inside: avoid 防止表格和图表被从中间断开,并要求浏览器保留有实际意义的背景颜色。测试时应使用浏览器的打印预览,而不仅仅是在屏幕上查看。
-
使用 srcset、sizes 和 picture 实现响应式图片
带宽度描述符的 srcset 配合 sizes 属性,可以让浏览器根据当前视口和像素密度,选择能填满显示区域的最小图片文件。x 描述符为固定显示尺寸提供多种密度;带 source 元素的 picture 用于构图调整和格式回退。始终保留 width 和 height,避免图片加载时布局偏移。
-
通过输出编码防止跨站脚本攻击
根据数据实际插入的上下文进行转义,包括 HTML 文本、属性、JavaScript、URL 和 CSS。使用默认转义的模板引擎,不要通过字符串拼接构造 HTML,并辅以严格的 CSP。
-
HTML 原生表单验证:required、pattern、type 与 Constraint Validation API
HTML 无需脚本即可在提交前验证表单控件:required、minlength、min/max、pattern 和指定类型的输入定义约束,浏览器阻止提交并显示消息,CSS 可为 :user-invalid 设置样式,Constraint Validation API 则向脚本暴露同一状态。这是易用性层,而非安全层;服务器仍会再次验证。
-
Short-link services with sequential identifiers receive more enumeration requests than services with random identifiers
Hypothesis: a URL shortener whose keys are a counter encoded in base62 lets anyone walk every link, whereas random fixed-length keys make most guesses miss; the proposal is that sequential services see a higher share of requests for existing keys from clients that never received the link, and that the share of 404 responses does not distinguish the two.
-
语言标签:内容和 API 中的 BCP 47
语言标签将 ISO 639 语言代码与可选的文字体系和地区子标签组合起来,例如 de、de-CH、zh-Hant。应将其用于内容语言声明、HTML lang 属性和 API 筛选,并验证标签,而不是接受任意文本。
-
Custom 404 pages and soft 404s: serve the error page with the error status
A custom 404 page helps users only if it is served with status 404; a not-found page served with 200 is a soft 404 that crawlers keep fetching and search engines exclude. nginx's error_page can rewrite the status (error_page 404 =200 ...), which is exactly how soft 404s are created by accident; keep the status, make the page useful, and check with curl -I.
-
浏览器存储:比较 cookie、Web Storage 与 IndexedDB
Cookie 会随每个请求传输,规范要求的容量较小;localStorage 和 sessionStorage 是同步字符串存储,每个源只有数 MiB;IndexedDB 是异步、支持事务的存储,共享源的较大配额。除非获准持久化,否则浏览器存储都是尽力保留,并按源划分。
-
同源策略:源是什么,以及它隔离什么
源由 URL 的协议方案、主机和端口组成。脚本可以读取和修改同源文档、存储及响应;跨源读取默认被阻止,而表单提交等跨源写入,以及图片和脚本等嵌入通常被允许。CORS 放宽读取限制,但仍需 CSRF 防御。
-
让代理读懂网站:robots.txt、站点地图与 llms.txt
代理和爬虫通过一组简单约定发现内容:robots.txt 提供访问规则和站点地图位置,XML 站点地图给出真实修改日期,llms.txt 充当精心编选的简短指南;它们都不能替代身份验证。
-
记录一次范围受限的 HTTP 观察
一种简明方法,用于记录单次 HTTP 观察,让另一位贡献者能够复查,而不暴露凭据或私密数据。
-
Web 字体加载:font-display、预加载、unicode-range 子集与度量匹配的后备字体
首次绘制就显示文字,并让品牌字体加载后不引发布局跳动:用 unicode-range 划分字体子集,只下载用到的文字系统;按角色选择 font-display(正文用 optional,标题用 swap);用 crossorigin 预加载首次绘制文件;声明带 size-adjust 及 ascent/descent 覆盖值的后备字体,使替换前后换行一致。
-
Server-Sent Events 与 WebSocket 的比较
Server-Sent Events 通过普通 HTTP 将文本事件从服务器持续发送给客户端,并支持自动重连和最后事件标识;WebSocket 则使用自己的协议,提供支持二进制数据的双向通道。单向更新可选择 SSE,客户端需要频繁发送数据时可选择 WebSocket。
-
Web Components:自定义元素、Shadow DOM 与封装的边界
自定义元素用带连字符的标签名注册类,并提供生命周期回调;Shadow DOM 给予它作用域受限的子树,样式不会向任一方向泄漏;模板和声明式影子根提供标记。所有跨边界交互,从通过 parts 和自定义属性设置样式,到通过 ElementInternals 参与表单及关联标签,都必须有意开放。
-
Link 标头与链接关系类型
RFC 8288 允许任何 HTTP 响应在 Link 标头中携带带类型的链接:<target>; rel="relation",并可附加 anchor、hreflang、type、title 和 media 参数。关系名来自 IANA 注册表,例如 next、prev、canonical、alternate、describedby、preload;私有扩展则使用绝对 URI。非 HTML 响应由此可以指向相邻资源,103 Early Hints 也借此告诉浏览器应提前获取什么。
-
Web Vitals:LCP、INP 和 CLS 分别测量什么
Core Web Vitals 是三个真实用户指标:Largest Contentful Paint(最大可见元素的渲染时间,目标 2.5 秒)、Interaction to Next Paint(最长交互延迟,目标 200 毫秒)和 Cumulative Layout Shift(意外位移,目标 0.1)。每项都按页面加载的第 75 百分位评估。
-
When does client-side routing still pay off now that browsers offer bfcache, prerendering and cross-document view transitions?
Open question: in-page routers were adopted to avoid full page loads, at the cost of shell serving, 404 handling, scroll and focus restoration and a bundle that must arrive first; the back/forward cache, the Speculation Rules API and cross-document view transitions now address the original motivations in multi-page sites. For which sites and interaction patterns does an in-page router still measurably win?
-
Web Push basics: subscriptions, VAPID keys and the push service
A browser subscribes with its vendor's push service and hands the page an endpoint plus keys; the application server POSTs encrypted messages to that endpoint with a TTL header and a VAPID JWT (ES256, aud = push service origin, exp at most 24 hours) whose public key was passed to PushManager.subscribe as applicationServerKey. A 201 means accepted, not delivered.
-
Validating, storing and serving user file uploads
Accept only the file types the feature needs, decide type by extension allowlist plus content check rather than the client's Content-Type, rename to a random identifier, enforce size limits before and after decompression, store outside the web root or on a separate host, and serve through a handler that sets the type, nosniff and Content-Disposition, ideally from a separate origin.
机器可读: JSON