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

feat: Support caching of responses with Vary header, and fix browser caching of adjacent pages/endpoints #9993

Merged
merged 6 commits into from
Jul 4, 2023

Conversation

oscarhermoso
Copy link
Contributor

@oscarhermoso oscarhermoso commented May 20, 2023

(fixes #9780)

Expand below to see recorded GIF of the issue - notice that on "forward" navigation the browser renders the endpoint response from cache, instead of the expected HTML response.

Before/after

Before

actual

After

svelte-kit-expected

  • fix: Include Vary: Accept header on responses to GET requests
  • feat: Allow caching of requests with Vary (as long as it's not Vary: *)

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 pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

@changeset-bot
Copy link

changeset-bot bot commented May 20, 2023

🦋 Changeset detected

Latest commit: a0a4d3a

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 Minor

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Looking good, thanks for the contribution! Just a couple of comments from me 😁

.changeset/shy-ears-report.md Outdated Show resolved Hide resolved
packages/kit/src/runtime/server/page/serialize_data.js Outdated Show resolved Hide resolved
Co-authored-by: S. Elliott Johnson <[email protected]>
Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

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

Thank you!

Note to self: This is now possible without any changes on the client because since #8754 we're taking headers into account for the cache key.

@dummdidumm dummdidumm merged commit 42c9b93 into sveltejs:master Jul 4, 2023
@github-actions github-actions bot mentioned this pull request Jul 4, 2023
@jaylinski
Copy link
Contributor

jaylinski commented Jul 10, 2023

Edit: I created an issue: #10366

This change broke an app of mine. When returning a fetch()-response (via node undici) directly to SvelteKit, undici will throw TypeError: immutable, because the change in this PR tries to modify the (immutable) undici-header.

Is there a good fix for this or do I have to clone the response-object so that the headers are mutable again? 🤔

Example for reproduction

+server.ts

import type { RequestHandler } from './$types';

// Proxy requests to API
export const GET = (async ({ request }) => {
	const headers = new Headers({ Authorization: `Bearer xyz` });

	return await fetch('https://api.test/', {
		method: request.method,
		headers,
		body: request.body,
		// @ts-ignore: https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/1483
		duplex: 'half'
	});
}) satisfies RequestHandler;

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.

Fix browser caching of endpoints instead of page
5 participants