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

[fix] use searchParams for x-sveltekit-invalidated #7912

Merged
merged 1 commit into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/heavy-forks-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[fix] use searchParams for x-sveltekit-invalidated
13 changes: 8 additions & 5 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1700,12 +1700,15 @@ export function create_client({ target, base }) {
async function load_data(url, invalid) {
const data_url = new URL(url);
data_url.pathname = add_data_suffix(url.pathname);
if (__SVELTEKIT_DEV__ && url.searchParams.has('x-sveltekit-invalidated')) {
throw new Error('Cannot used reserved query parameter "x-sveltekit-invalidated"');
}
data_url.searchParams.append(
'x-sveltekit-invalidated',
invalid.map((x) => (x ? '1' : '')).join('_')
);

const res = await native_fetch(data_url.href, {
headers: {
'x-sveltekit-invalidated': invalid.map((x) => (x ? '1' : '')).join(',')
}
});
const res = await native_fetch(data_url.href);
const data = await res.json();

if (!res.ok) {
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/runtime/server/data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ export async function render_data(event, route, options, state, trailing_slash)
const node_ids = [...route.page.layouts, route.page.leaf];

const invalidated =
event.request.headers.get(INVALIDATED_HEADER)?.split(',').map(Boolean) ??
event.url.searchParams.get(INVALIDATED_HEADER)?.split('_').map(Boolean) ??
node_ids.map(() => true);
event.url.searchParams.delete(INVALIDATED_HEADER);

let aborted = false;

Expand Down
12 changes: 1 addition & 11 deletions packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
strip_data_suffix
} from '../../utils/url.js';
import { exec } from '../../utils/routing.js';
import { INVALIDATED_HEADER, redirect_json_response, render_data } from './data/index.js';
import { redirect_json_response, render_data } from './data/index.js';
import { add_cookies_to_headers, get_cookies } from './cookie.js';
import { create_fetch } from './fetch.js';
import { Redirect } from '../control.js';
Expand Down Expand Up @@ -230,16 +230,6 @@ export async function respond(request, options, state) {
response.headers.set(key, /** @type {string} */ (value));
}

if (is_data_request) {
// set the Vary header on __data.json requests to ensure we don't cache
// incomplete responses with skipped data loads
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Vary
const vary = response.headers.get('Vary');
if (vary !== '*') {
response.headers.append('Vary', INVALIDATED_HEADER);
}
}

add_cookies_to_headers(response.headers, Object.values(new_cookies));

if (state.prerendering && event.route.id !== null) {
Expand Down
7 changes: 4 additions & 3 deletions packages/kit/test/apps/basics/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,15 @@ test.describe('a11y', () => {
});

test.describe('Caching', () => {
test('caches __data.json requests with Vary header', async ({ page, app }) => {
test('caches __data.json requests with invalidated search param', async ({ page, app }) => {
await page.goto('/');
const [, response] = await Promise.all([
app.goto('/caching/server-data'),
page.waitForResponse((request) => request.url().endsWith('server-data/__data.json'))
page.waitForResponse((request) =>
request.url().endsWith('server-data/__data.json?x-sveltekit-invalidated=_1')
)
]);
expect(response.headers()['cache-control']).toBe('public, max-age=30');
expect(response.headers()['vary']).toBe('x-sveltekit-invalidated');
});
});

Expand Down