Skip to content

Fix font_google(local = TRUE) failing on Windows and after cache pruning - #164

Merged
cpsievert merged 7 commits into
mainfrom
fix/issue-157-google-font-windows
Nov 18, 2026
Merged

cpsievert merged 7 commits into
mainfrom
fix/issue-157-google-font-windows

Conversation

@cpsievert

@cpsievert cpsievert commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Closes #157.

Why

font_google(local = TRUE) has two bugs that make locally-downloaded Google fonts unreliable:

  1. It fails outright on Windows. Google Fonts now serves font URLs containing query strings, and the code named local files after the URL's basename — which contains characters like ? that are invalid in Windows file names.
  2. Dependencies can silently miss font files after cache pruning. If the cache evicts a font file but keeps the CSS that references it, the recovery path re-downloaded the CSS but discarded the recursive call's result, so the returned htmlDependency was missing exactly the pruned file(s).

What changed

  • Local font files are now named after a deterministic hash of their URL (rlang::hash()), with an extension derived from the font's format() hint. When the hint is absent, a recognized extension from the URL path is used instead, ignoring query strings and fragments. Files continue to be downloaded locally without a format hint, and filenames remain valid on every platform.
  • The cache-pruning recovery now defers the recursive font_dep_google_local() call until after font processing and returns its result, busting the stale CSS cache entry exactly once (no infinite recursion; still-cached fonts are reused, only pruned ones re-download).

Existing caches need no migration: old Unix caches keep working and age out naturally; Windows caches can't contain the broken entries (the download itself errored before this fix).

Tests

  • New end-to-end regression test: populate the cache, delete the font files, re-render, and assert every url() in the served CSS exists on disk (verified to fail against the old code).
  • Network-free filename tests cover uniqueness, Windows-safe characters, deterministic naming, format-hint precedence, URL-extension fallback, and cached local filenames without format hints.
  • All 217 package assertions pass locally, including 66 font-objects assertions and the live Google Fonts snapshot test. The new missing-format assertions were verified to fail before the fallback was added.

Known limitations

  • Unrecognized format() values, or missing format hints with no recognized URL extension, produce extensionless local files, which may get a generic MIME type. The current Google Fonts responses tested use woff2.

cpsievert and others added 4 commits November 18, 2026 09:55
When the Google Fonts CSS was a cache hit but a font file it
references had been pruned from the cache, font_dep_google_local()
would 'bust the CSS cache and start over' by calling itself
recursively. However, that call happened inside a Map() callback, so
its return value -- the correctly rebuilt htmlDependency -- was
discarded. The outer call then continued and returned a dependency
whose directory was missing (at least) the font file that triggered
the cache miss.

Record the need to restart in a flag and do the recursive restart at
the top level of font_dep_google_local() instead.
Google Fonts may return font URLs containing a query string, and
basename() of such URLs includes characters (e.g., '?') that are
invalid in Windows file names, causing the download to fail.

Name downloaded font files after a hash of their URL, plus an
extension derived from the font's format() hint in the CSS. Names in
already-rewritten (cached) CSS are kept as-is so cache lookups
round-trip.

This comment was marked as resolved.

When only some font files are pruned from the cache, the recovery path
re-downloads the CSS (so all URLs are remote again), but fonts still
present in the cache hit the early return before their URLs were
rewritten to the local copies -- the returned and re-cached CSS kept
pointing at Google for those fonts, breaking offline rendering.

Addresses Copilot review feedback on #164.
@cpsievert
cpsievert merged commit aa19862 into main Nov 18, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

font_google(local = TRUE) fails on Windows: downloaded font file name contains '?'

2 participants