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

cache control plugin: set cache-control: no-store by default #6986

Merged
merged 1 commit into from Oct 4, 2022

Conversation

glasser
Copy link
Member

@glasser glasser commented Oct 4, 2022

As pointed out in #2605, browsers cache many GET requests by default, so "not cacheable" shouldn't be the same as "don't set a cache-control header". This PR makes the backwards-incompatible change to the cache control plugin to make it always set the cache-control header to something if it is enabled (with calculateHttpHeaders not set to false), perhaps no-store. (If some other plugin or error already set the header, it does not override it with no-store.)

To restore AS3 behavior:

ApolloServerPluginCacheControl({ calculateHttpHeaders: 'if-cacheable' })

Fixes #2605.

As pointed out in #2605, browsers cache many GET requests by default, so
"not cacheable" shouldn't be the same as "don't set a cache-control
header". This PR makes the backwards-incompatible change to the cache
control plugin to make it always set the cache-control header to
something if it is enabled (with calculateHttpHeaders not set to false),
perhaps `no-store`. (If some other plugin or error already set the
header, it does not override it with `no-store`.)

To restore AS3 behavior:

    ApolloServerPluginCacheControl({ calculateHttpHeaders: 'if-cacheable' })

Fixes #2605.
@netlify
Copy link

netlify bot commented Oct 4, 2022

Deploy Preview for apollo-server-docs ready!

Name Link
🔨 Latest commit 2359cb0
🔍 Latest deploy log https://app.netlify.com/sites/apollo-server-docs/deploys/633b8883c1a789000849fe58
😎 Deploy Preview https://deploy-preview-6986--apollo-server-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 4, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 2359cb0:

Sandbox Source
Apollo Server Typescript Configuration
Apollo Server Configuration

@glasser
Copy link
Member Author

glasser commented Oct 4, 2022

Going forward with this; post-merge review.

@glasser glasser merged commit db5d715 into version-4 Oct 4, 2022
@glasser glasser deleted the glasser/cache-control-no-store branch October 4, 2022 19:00
@github-actions github-actions bot mentioned this pull request Oct 4, 2022
glasser pushed a commit that referenced this pull request Oct 4, 2022
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to version-4, this PR
will be updated.

⚠️⚠️⚠️⚠️⚠️⚠️

`version-4` is currently in **pre mode** so this branch has prereleases
rather than normal releases. If you want to exit prereleases, run
`changeset pre exit` on `version-4`.

⚠️⚠️⚠️⚠️⚠️⚠️

# Releases
## @apollo/server-integration-testsuite@4.0.0-rc.16

### Patch Changes

- [#6986](#6986)
[`db5d715a3`](db5d715)
Thanks [@glasser](https://github.com/glasser)! - The cache control
plugin sets `cache-control: no-store` for uncacheable responses. Pass
`calculateHttpHeaders: 'if-cacheable'` to the cache control plugin to
restore AS3 behavior.

- Updated dependencies
\[[`db5d715a3`](db5d715)]:
    -   @apollo/server@4.0.0-rc.16

## @apollo/server@4.0.0-rc.16

### Patch Changes

- [#6986](#6986)
[`db5d715a3`](db5d715)
Thanks [@glasser](https://github.com/glasser)! - The cache control
plugin sets `cache-control: no-store` for uncacheable responses. Pass
`calculateHttpHeaders: 'if-cacheable'` to the cache control plugin to
restore AS3 behavior.

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
@github-actions github-actions bot mentioned this pull request Oct 4, 2022
@@ -331,14 +331,21 @@ The effect of setting `honorSubgraphCacheControlHeader` to `false` is to have no

## Caching with a CDN

Whenever Apollo Server sends an operation response that has a non-zero `maxAge`, it includes a `Cache-Control` HTTP header that describes the response's cache policy.
Whenever Apollo Server sends an operation response that is cacheable, it includes a `Cache-Control` HTTP header that describes the response's cache policy.
Copy link
Member

Choose a reason for hiding this comment

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

@glasser Additionally, it now also sets the cache-control header when it's not cacheable as well. We do go on to clarify this below, but the way this is written leads me to think it's only set when cacheable.

@@ -852,13 +870,18 @@ export function defineIntegrationTestSuiteHttpServerTests(
expect(res.body.data).toEqual({
pooks: [{ title: 'pook', books }],
});
expect(res.headers['cache-control']).toBeUndefined;
expect(res.headers['cache-control']).toBeUndefined();
Copy link
Member

Choose a reason for hiding this comment

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

yikes, good catch

@@ -873,6 +896,23 @@ export function defineIntegrationTestSuiteHttpServerTests(
});
});

it('cache-control not set without any hints', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

Might update the description of this test? cache-control is set to no-store.

@trevor-scheer
Copy link
Member

Implementation LGTM, with a few docs/test related suggestions within.

glasser added a commit that referenced this pull request Oct 10, 2022
@github-actions github-actions bot mentioned this pull request Oct 10, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants