Skip to content

Cache downloaded font files to avoid re-downloading on retries and reruns #206

Description

Install-GoogleFont deletes each downloaded .ttf immediately after installing it, and the clean block removes the temp directory. Any subsequent invocation — whether a retry after a partial failure, a reinstall with -Force, or a separate command targeting overlapping fonts — re-downloads the same files from fonts.gstatic.com.

A small on-disk cache keyed by the asset URL eliminates the duplicate transfers and makes recovery from interrupted batches essentially free. The win is especially large for -All (thousands of files) and for users who script font installs repeatedly across machines.

Request

Current experience

Every run starts from zero. A user who runs Install-GoogleFont -All, hits a transient network error halfway through, and re-runs the command pays the full download cost again for every file already fetched.

Desired experience

Downloaded font files persist in a well-known cache directory. On the next invocation, if the cached file matches the expected URL, it is reused instead of re-fetched. The cache is opt-out, has a clear default location, and grows boundedly.

Acceptance criteria

  • Successful downloads are saved into a cache directory
  • A second invocation that needs the same URL does not perform a network request
  • Cache lookup is keyed by URL (which encodes the version path segment, e.g. /v23/), so a future Google Fonts metadata update that ships a same-named file at a new URL is not served stale content
  • -Force bypasses the cache and re-downloads
  • The cache location is documented and discoverable via verbose output

Related

  • The companion NerdFonts cache issue tracks the same idea for the sibling module

Technical decisions

Cache location: Under the platform-appropriate user cache directory:

  • Windows: $Env:LOCALAPPDATA\PSModule\GoogleFonts\cache
  • Linux: $Env:XDG_CACHE_HOME ?? $HOME/.cache + /PSModule/GoogleFonts
  • macOS: $HOME/Library/Caches/PSModule/GoogleFonts

Cache key: Hash of the asset URL (e.g. SHA-256 truncated to 16 hex chars) joined with the original filename, so two URLs that happen to share a filename do not collide. URLs already embed a version segment (/v23/), so cache entries naturally rotate when Google publishes a new font version.

Validation: Trust the URL-keyed path. Google Fonts CDN URLs are effectively immutable per version. A failed Install-Font after extraction can fall back to re-downloading.

Bypass: Reuse -Force to mean "ignore the cache and re-download". No new parameter required for the first iteration.

Eviction: Out of scope for this issue — the cache grows monotonically. A follow-up can add LRU eviction or a Clear-GoogleFontCache command if the directory becomes a problem.


Implementation plan

  • Add a helper that computes the cache directory for the current platform and ensures it exists
  • Before downloading, check for the cached file and reuse it if present and -Force is not set
  • After a successful download, move the file from the per-run temp dir into the cache (or download directly into the cache)
  • Emit verbose output indicating cache hit vs. miss per file
  • Document the cache location in the function help
  • Add a Pester test asserting that a second invocation for the same font issues no Invoke-WebRequest call

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions