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

Don't overwrite custom etag #3348

Merged
merged 6 commits into from
Jan 15, 2022
Merged

Conversation

cristovao-trevisan
Copy link
Contributor

@cristovao-trevisan cristovao-trevisan commented Jan 14, 2022

Kit should not overwrite custom etag value.

Sometimes it's useful to provide a custom etag value which should not be overwritten by sveltekit.
For example, in my particular use case, when proxying an image from S3 it's possible to retrieve the ETAG with the metadata, without downloading it.
This is also useful when the etag is based on api requests, making it possible to skip rendering the page.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpx changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Jan 14, 2022

🦋 Changeset detected

Latest commit: c054545

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@netlify
Copy link

netlify bot commented Jan 14, 2022

✔️ Deploy Preview for kit-demo canceled.

🔨 Explore the source changes: c054545

🔍 Inspect the deploy log: https://app.netlify.com/sites/kit-demo/deploys/61e30d5f11570500077d4f3b

@Rich-Harris
Copy link
Member

Thank you! Can you add a test and a changeset please? You could add a test to this section:

test.describe.parallel('ETags', () => {
test.skip(({ javaScriptEnabled }) => !javaScriptEnabled);
test('generates etag/304 for text body', async ({ request }) => {
const r1 = await request.get('/etag/text');
const etag = r1.headers()['etag'];
expect(etag).toBeTruthy();
const r2 = await request.get('/etag/text', {
headers: {
'if-none-match': etag
}
});
expect(r2.status()).toBe(304);
expect(r2.headers()['expires']).toBe('yesterday');
});
test('generates etag/304 for binary body', async ({ request }) => {
const r1 = await request.get('/etag/binary');
const etag = r1.headers()['etag'];
expect(etag).toBeTruthy();
const r2 = await request.get('/etag/binary', {
headers: {
'if-none-match': etag
}
});
expect(r2.status()).toBe(304);
});
test('support W/ etag prefix', async ({ request }) => {
const r1 = await request.get('/etag/text');
const etag = r1.headers()['etag'];
expect(etag).toBeTruthy();
const r2 = await request.get('/etag/text', {
headers: {
'if-none-match': `W/${etag}`
}
});
expect(r2.status()).toBe(304);
});
});

@Conduitry
Copy link
Member

Should we also, then, trust an etag from the user's response object when we're checking whether we should return a 304?

@cristovao-trevisan
Copy link
Contributor Author

Yes @Rich-Harris , I can add a test and the changeset, will do soon

@Conduitry , I don't think there's a stateless (or serveless) way to check the user provided etag, I assume that if the etag is being set manually the user know what he's doing and will check the if-none-matches equality himself

Copy link
Member

@Rich-Harris Rich-Harris left a comment

Choose a reason for hiding this comment

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

Thanks — response inline

.changeset/clever-readers-turn.md Outdated Show resolved Hide resolved
packages/kit/src/runtime/server/index.js Outdated Show resolved Hide resolved
@Rich-Harris Rich-Harris merged commit 668b0aa into sveltejs:master Jan 15, 2022
@cristovao-trevisan
Copy link
Contributor Author

Was going to apply your suggestion right now, sorry to be late but thanks for all the work!!

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.

3 participants