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 cache headers to responses #1390

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

alxndrsn
Copy link
Contributor

@alxndrsn alxndrsn commented Feb 8, 2025

Closes getodk/central#662

What has been done to verify that this works as intended?

Added tests.

Why is this the best possible solution? Were any other approaches considered?

It seems simple.

How does this change affect users? Describe intentional changes to behavior and behavior that could have accidentally been affected by code changes. In other words, what are the regression risks?

Should prevent future caching surprises.

Does this change require updates to the API documentation? If so, please update docs/api.yaml as part of this PR.

No? "cache" is not currently mentioned in docs/

Before submitting this PR, please make sure you have:

  • run make test and confirmed all checks still pass OR confirm CircleCI build passes
  • verified that any code from external sources are properly credited in comments or that everything is internally sourced

@alxndrsn alxndrsn requested a review from lognaturel February 8, 2025 09:01
@alxndrsn alxndrsn changed the title Add cache headers Add cache headers to responses Feb 8, 2025
Copy link
Member

@lognaturel lognaturel left a comment

Choose a reason for hiding this comment

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

That looks good to me -- using the private cache directly for 20s keeps a client from totally hammering the server with requests without compromising freshness. Then we expect the request to happen and result in a 304 if there are no changes.

@sadiqkhoja in case you want to take a second look.

@alxndrsn alxndrsn requested a review from sadiqkhoja February 20, 2025 06:40
if (req.path.startsWith('/sessions/')) {
res.set('Cache-Control', 'no-store');
} else {
res.set('Cache-Control', 'private, max-age=20');
Copy link
Contributor

Choose a reason for hiding this comment

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

20 seconds isn't too much?

And for data endpoints like submissions and entities max-age should be 0

Copy link
Member

Choose a reason for hiding this comment

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

The idea is to have a low max-age for all dynamic content so that there is some local caching that comes into play for unusual patterns like immediately refreshing. In general, we don't expect immediate refreshes so this eases load on the server a bit without significantly compromising freshness. We could set it to 0 instead but I don't see a big advantage. Whichever we choose we should apply consistently, I think, since all content is equally dynamic.

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 think max-age=0 is likely to provide fewer surprises.

Is immediate refreshing an issue? I'd guess if it's being done, then it's because of an expectation of changed data...

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough, that works for me!

@@ -17,24 +17,192 @@ const serverUrl = 'http://localhost:8383';
const userEmail = 'x@example.com';
const userPassword = 'secret1234';

describe('Cache headers', () => {
const undici = require('undici');
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: maybe move this to the top of the file with other requires.

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.

Cloudflare caches Entity Lists for longer than expected
3 participants