Skip to content
Closed
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/selfish-pigs-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Reuse server data when the client load is invalidated
9 changes: 4 additions & 5 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,10 +656,9 @@ export function create_client({ target, base, trailing_slash }) {

/**
* @param {import('types').ServerDataNode | import('types').ServerDataSkippedNode | null} node
* @param {import('./types').DataNode | null} [previous]
* @returns {import('./types').DataNode | null}
*/
function create_data_node(node, previous) {
function create_data_node(node) {
if (node?.type === 'data') {
return {
type: 'data',
Expand All @@ -671,8 +670,6 @@ export function create_client({ target, base, trailing_slash }) {
url: !!node.uses.url
}
};
} else if (node?.type === 'skip') {
return previous ?? null;
}
return null;
}
Expand Down Expand Up @@ -759,6 +756,8 @@ export function create_client({ target, base, trailing_slash }) {
throw server_data_node;
}

const prev_server_data_node = (!changed?.url && previous?.server) || null;
Copy link
Author

Choose a reason for hiding this comment

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

I realize that changed.url is not the perfect condition, because we will want to reuse the server data of a layout load even if the url changes, but I'm not familiar enough with the code base to determine the correct condition.


return load_node({
loader: loader[1],
url,
Expand All @@ -771,7 +770,7 @@ export function create_client({ target, base, trailing_slash }) {
}
return data;
},
server_data_node: create_data_node(server_data_node, previous?.server)
server_data_node: create_data_node(server_data_node) ?? prev_server_data_node
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('./$types').PageLoad} */
export function load({ data, depends }) {
depends('isomorphic-load');
return { from_server: data };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('./$types').PageServerLoad} */
export function load() {
return { server: true };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
import { invalidate } from '$app/navigation';
import { onMount } from 'svelte';

export let data;

onMount(() => invalidate('isomorphic-load'));
</script>

<p>Page with server load and isomorphic-load</p>

<pre>{JSON.stringify(data.from_server)}</pre>
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
<p>Page with server load</p>

<pre>{JSON.stringify(data)}</pre>

<a href="/load/server-data-reuse/no-load">No load page</a>
5 changes: 5 additions & 0 deletions packages/kit/test/apps/basics/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,11 @@ test.describe('Load', () => {
expect(await page.textContent('pre')).toBe(JSON.stringify({ foo: { bar: 'Custom layout' } }));
});

test('server data is reused if client load is invalidated', async ({ page }) => {
await page.goto('/load/server-data-reuse/with-client-invalidation');
expect(await page.textContent('pre')).toBe(JSON.stringify({ server: true }));
});

test('load does not call fetch if max-age allows it', async ({ page, request }) => {
await request.get('/load/cache-control/reset');

Expand Down