Importing vs fetching JSON

2025年10月22日

AI 摘要 AI 生成,可能有误

JSON 模块导入现已得到所有主流浏览器的支持,但作者认为在浏览器环境中不应将其当作 fetch() 的一揽子替代方案。导入的模块会在页面的整个生命周期内被缓存,一旦失败就会导致整个模块图崩溃,且错误内省能力有限;在动态导入 API 响应等数据时,存在内存泄漏的风险。JSON 模块导入最适合用于构建时打包的本地静态资源,而在运行时获取数据方面,fetch() 仍然是更优的选择,因为它在垃圾回收和错误处理方面表现更出色。

This year, JSON module imports became baseline 'newly available', meaning they're implemented across browser engines.

I'm glad JavaScript has this feature, but I can't see myself using it in a browser environment, other than small demos. I might still use it in frontend source code, but generally only in cases where it'd be bundled away before reaching the browser.

It comes down to the behaviour differences compared to this:

继续阅读全文

为了尊重作者,这里只展示开头几段。你可以去原文阅读,或者在 HorizonReader 里读:划线做笔记、让 AI 帮忙。

更多来自 Jake Archibald

JPEG XL vs AVIF for web use-cases

AI 摘要This article benchmarks JPEG XL (via libjxl and the newer jixel encoder) against AVIF across web use-cases, including lossy photos, screenshots, illustrations, transparency, HDR, and lossless images. It concludes that AVIF almost always produces smaller files and encodes/decodes faster, making it the better default for lossy web images, while JPEG XL only clearly wins for lossless compression and lossless JPEG recompression. The author recommends defaulting to AVIF 4:4:4 at high effort, but checking JPEG XL via jixel when AVIF produces an unusually large file.

Controlling when CSS custom property values are computed

AI 摘要The article explains that the timing of when CSS custom property values are computed depends on whether they are registered with @property: unregistered properties store values as token streams and only fully compute where they are used via var(), while registered properties with a specific syntax compute earlier, at the element where they are defined. This distinction changes the outcome for functions like sibling-index(), relative font and container query units, and URL resolution. It also notes that arbitrary substitution functions like var() are always substituted regardless of the registered syntax, which can cause nested values like a child's --multiplier to be ignored.

Fixing my tooltip accessibility mistake

AI 摘要The author describes an accessibility mistake made while creating a toolbar tooltip: after removing the button's aria-label to stop VoiceOver from announcing 'Bold' twice, the button was left without an accessible name, since aria-describedby only provides supplementary description rather than a name. Accessibility experts from TetraLogical pointed out the error, and the fix was to switch to aria-labelledby, which supplies the accessible name from the tooltip content. The key lessons are to always ensure interactive elements have an accessible name and to test with multiple screen readers, since results vary across tools.

The Goldilocks customizable select height

AI 摘要The article explores CSS techniques for achieving ideal sizing of customizable pickers, addressing problems where the picker hits the viewport edge or becomes unusably small when dragged to the screen boundary. The author uses margins with @supports fallbacks for Firefox, flip-block/flip-inline position-try fallbacks to keep margins correct when the picker flips in Chrome and Safari, and calc-size() with fit-content to set a minimum height that doesn't affect small pickers. The key insight—that calc-size() enables minimum sizes like min(fit-content, 12em)—came from Ian Kilpatrick, with a hacky :has() selector fallback provided for browsers lacking calc-size() support.

The present and potential future of progressive image rendering

AI 摘要The article surveys the current state of progressive image rendering in browsers across JPEG, PNG, WebP, AVIF, and JPEG XL, comparing when partial renders appear, decoding costs, and each format's limitations—concluding that browser support is inconsistent (e.g., Safari lacks WebP and JPEG XL progressive rendering, and AVIF's experimental layered mode has quirks). The author argues that progressive rendering's real-world benefit is limited because data typically arrives in bursts rather than gradually, making it a nice-to-have rather than a replacement for careful image sizing. He also explains why using progressive decoding to replace responsive images is impractical due to network lag, and notes promising but not-yet-browser-ready progressiveness in JPEG XL encoders and third-party decoders.