Skip to content

Serve SPA with CDN-friendly caching and add ETag-based caching for API responses#174

Draft
zhenghaoz wants to merge 1 commit intomasterfrom
codex/optimize-code-for-cloudflare-cdn
Draft

Serve SPA with CDN-friendly caching and add ETag-based caching for API responses#174
zhenghaoz wants to merge 1 commit intomasterfrom
codex/optimize-code-for-cloudflare-cdn

Conversation

@zhenghaoz
Copy link
Copy Markdown
Collaborator

Motivation

  • Improve caching behavior for the single-page app to be CDN-friendly and reduce origin load by setting explicit cache headers.
  • Reduce API response bandwidth and latency by adding ETag support so clients can receive 304 Not Modified when appropriate.

Description

  • Added make_spa_response() which serves index.html with Cache-Control: public, max-age=0, s-maxage=60, stale-while-revalidate=30 for CDN-friendly caching.
  • Added make_api_cached_response(payload) which computes a SHA256 ETag for the JSON payload, returns 304 when If-None-Match matches, and sets ETag, Cache-Control, and Vary headers.
  • Replaced direct app.send_static_file("index.html") calls in "/", "/login", "/privacy", and the 404 handler with make_spa_response().
  • Updated cache-hit and response paths in "/api/trending" and "/api/hackernews" to use make_api_cached_response() instead of manually creating responses.

Testing

  • Ran the project's automated test suite (unit and integration tests) and the modified endpoints passed with no test failures.

Codex Task

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant