Skip to content

Update cache headers to include max-age and immutable#12420

Merged
sfc-gh-gwatters merged 3 commits intodevelopfrom
gwatters/st-cache-headers-1
Sep 4, 2025
Merged

Update cache headers to include max-age and immutable#12420
sfc-gh-gwatters merged 3 commits intodevelopfrom
gwatters/st-cache-headers-1

Conversation

@sfc-gh-gwatters
Copy link
Copy Markdown
Contributor

Describe your changes

Without max-age defined in the cache header, browsers fallback to use a heuristic caching strategy which is typically (Chrome)10% of the time since the "Last Modified" header (see https://datatracker.ietf.org/doc/html/rfc7234#section-4.2.2). Firefox caps this at a week. Since the files being served all contain a hash, the actual cache duration can be much much longer.

For environments where the static files get written at deployment or startup time, the heuristic cache can be on the order of minutes/hours.

GitHub Issue Link (if applicable)

Testing Plan

  • Explanation of why no additional tests are needed
  • Unit Tests (JS and/or Python)
  • E2E Tests
  • Any manual testing needed?

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@snyk-io
Copy link
Copy Markdown
Contributor

snyk-io bot commented Sep 3, 2025

🎉 Snyk checks have passed. No issues have been found so far.

security/snyk check is complete. No issues have been found. (View Details)

license/snyk check is complete. No issues have been found. (View Details)

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Sep 3, 2025

✅ PR preview is ready!

Name Link
📦 Wheel file https://core-previews.s3-us-west-2.amazonaws.com/pr-12420/streamlit-1.49.1-py3-none-any.whl
🕹️ Preview app pr-12420.streamlit.app (☁️ Deploy here if not accessible)

This comment was marked as outdated.

@vdonato vdonato added security-assessment-completed change:other PR contains other type of change impact:users PR changes affect end users labels Sep 3, 2025
@sfc-gh-gwatters sfc-gh-gwatters force-pushed the gwatters/st-cache-headers-1 branch from a9eaebb to a2d95df Compare September 4, 2025 16:31
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates cache headers for static assets to include explicit max-age and immutable directives instead of relying on browser heuristic caching. The change addresses the issue where browsers use unpredictable heuristic caching (typically 10% of time since Last-Modified) for assets without explicit cache duration, which can result in very short cache times for recently deployed files.

  • Updates cache control headers to use explicit one-year max-age for hashed static assets
  • Adds immutable directive to indicate cached assets won't change
  • Maintains no-cache behavior for HTML files and manifest.json files

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
lib/streamlit/web/server/routes.py Updates StaticFileHandler to set explicit cache headers with max-age and immutable for hashed assets
lib/tests/streamlit/web/server/routes_test.py Adds comprehensive test coverage for the new cache control header behavior

@sfc-gh-gwatters sfc-gh-gwatters merged commit 4129d6d into develop Sep 4, 2025
36 checks passed
@sfc-gh-gwatters sfc-gh-gwatters deleted the gwatters/st-cache-headers-1 branch September 4, 2025 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:other PR contains other type of change impact:users PR changes affect end users

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants