Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explicit Cache-Control, to make sure Cloudflare does not cache resources for too long #1077

Closed
wants to merge 1 commit into from

Conversation

acelaya
Copy link
Contributor

@acelaya acelaya commented Jun 5, 2023

Until we implement a better solution, this PR adds explicit 5 minute cache for any resource served through the pattern library production nginx (index.html, CSS/JS bundles, and any other static resource).

This will make sure Cloudflare revalidates those assets after 5 minutes tops, and we don't end-up showing outdated content after a deployment.

Next steps would include improving this process to make it smarter (cache longer what doesn't change, while making sure new content is displayed right away after a deployment). We have discussed a few options on slack https://hypothes-is.slack.com/archives/C1M8NH76X/p1685957404405389

Testing steps

  • Check out this branch.
  • Build the docker image: docker build . -t hypothesis_frontend_shared.
  • Run the image on a port of your choice. For example, port 88: docker run --rm -p 89:5001 hypothesis_frontend_shared
  • Visit http://localhost:88, and check in the console's network tab, that it loaded the root page and the CSS and JS bundles. Check that all those containe the response header: Cache-Control: public, s-maxage=300, max-age=60, must-revalidate.
  • If you refresh the page, you should see how it either receives a 304 from nginx (for the document request), or it directly loads resources from cache (for the bundles). The bundle requests should also not show in nginx logs, as the browser did not make the requests.
  • If you wait for 5 min and repeat the process, you should see how the assets are requested again to nginx (requests are loaded, although it returns 304 because the content did not change).

This PR relates to #1084

@acelaya acelaya requested a review from lyzadanger June 5, 2023 10:53
try_files $uri $uri/ /index.html$is_args$args;
# Ensure Cloudflare caches assets for 5 minutes
add_header Cache-Control "public, s-maxage=300, max-age=60, must-revalidate";
try_files $uri $uri/ /index.html;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have removed $is_args$args, because those actually make no sense when loading a static file. They are meant to be used in fast-cgi contexts, like with PHP and such, where there might be some server-side logic run before nginx returns the result.

@@ -12,12 +12,15 @@ server {

# When requesting static paths with an extension, try them, and return a 404 if not found
location ~* .+\..+ {
add_header Cache-Control "public, s-maxage=300, max-age=60, must-revalidate";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For now, I have just set the same values we have for the client assets.

@acelaya acelaya force-pushed the patterns-deployment-naive-cache branch from d0ad597 to f9c7215 Compare June 5, 2023 10:57
@lyzadanger lyzadanger removed their request for review June 7, 2023 18:16
@acelaya
Copy link
Contributor Author

acelaya commented Jun 9, 2023

This PR is superseded by #1089

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.

2 participants