Skip to content

Evaluate Workers Cache for rewritten JS chunks and OG images #186

Open

Description

@shigahi

Summary

Workers Cache caches a Worker's responses in front of the Worker. On a hit, Cloudflare returns the stored response without running the Worker. It uses tiered cache (lower tier near the visitor, shared upper tier) and collapses concurrent requests for the same key within a data center.

The generated worker currently keeps rewritten JS chunks (fetchJs) and OG image PNGs (generateOgImage) in caches.default. That cache is per data center, doesn't collapse concurrent misses, and the worker still runs on every hit. After each Notion deploy, the first visitor in each data center pays for fetching and rewriting the large app chunks. In local tests for #144, an uncached chunk request took about 675 ms of CPU against the Workers Free plan's 10 ms limit.

What it would change

  • Hits for /_assets/*.js, /app*.js and /og-image/* skip the worker: no CPU time and lower TTFB.
  • A cold data center fills from the upper tier instead of fetching and rewriting from Notion again.
  • Concurrent first requests for the same chunk run the worker once.
  • It does not reduce the request count. Cache hits are billed as Worker requests (see Reduce Worker invocations per page view #144).

Blockers and pitfalls

  • Workers Cache can only be enabled in the Wrangler configuration ("cache": { "enabled": true }). The limitations page lists dashboard support as coming soon. Worknot users paste the script into the dashboard, so this waits for dashboard support or becomes an optional Wrangler-based setup.
  • The cache key doesn't include the hostname. Subdomain redirects (SUBDOMAIN_REDIRECTS) and anything else host-dependent must never be served from it. Use the gateway pattern from the docs: keep caching disabled on the default entrypoint (routing, redirects, HTML, API), and enable it on a named entrypoint for the cacheable paths, called through ctx.exports.
  • Responses with Set-Cookie bypass the cache. fetchJs already strips it.
  • Set explicit Cache-Control on everything the cached entrypoint returns, because heuristic freshness applies otherwise, and don't cache upstream errors.
  • The Worker version is part of the cache key by default, which may make CACHE_VERSION unnecessary for this path.
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