diff --git a/.changeset/fast-rules-exercise.md b/.changeset/fast-rules-exercise.md
new file mode 100644
index 000000000000..3aab4ebc9844
--- /dev/null
+++ b/.changeset/fast-rules-exercise.md
@@ -0,0 +1,5 @@
+---
+'@sveltejs/kit': patch
+---
+
+fix: don't reuse previous server load cache when there's no server load function
diff --git a/packages/kit/src/runtime/client/client.js b/packages/kit/src/runtime/client/client.js
index a0b3b63170ef..f5c53e7dd732 100644
--- a/packages/kit/src/runtime/client/client.js
+++ b/packages/kit/src/runtime/client/client.js
@@ -853,7 +853,7 @@ export function create_client({ target }) {
// server_data_node is undefined if it wasn't reloaded from the server;
// and if current loader uses server data, we want to reuse previous data.
server_data_node === undefined && loader[0] ? { type: 'skip' } : server_data_node ?? null,
- previous?.server
+ loader[0] ? previous?.server : undefined
)
});
});
diff --git a/packages/kit/test/apps/basics/src/routes/load/server-data-reuse/with-changing-parent/+layout.server.js b/packages/kit/test/apps/basics/src/routes/load/server-data-reuse/with-changing-parent/+layout.server.js
new file mode 100644
index 000000000000..76faa0d9e991
--- /dev/null
+++ b/packages/kit/test/apps/basics/src/routes/load/server-data-reuse/with-changing-parent/+layout.server.js
@@ -0,0 +1,6 @@
+/** @type {import('./$types').LayoutServerLoad} */
+export function load(input) {
+ return {
+ title: input.url.pathname
+ };
+}
diff --git a/packages/kit/test/apps/basics/src/routes/load/server-data-reuse/with-changing-parent/+layout.svelte b/packages/kit/test/apps/basics/src/routes/load/server-data-reuse/with-changing-parent/+layout.svelte
new file mode 100644
index 000000000000..4fa864ce7aa9
--- /dev/null
+++ b/packages/kit/test/apps/basics/src/routes/load/server-data-reuse/with-changing-parent/+layout.svelte
@@ -0,0 +1 @@
+
Page without load
+ +{JSON.stringify(data)}diff --git a/packages/kit/test/apps/basics/src/routes/load/server-data-reuse/with-changing-parent/with-server-load/+page.server.js b/packages/kit/test/apps/basics/src/routes/load/server-data-reuse/with-changing-parent/with-server-load/+page.server.js new file mode 100644 index 000000000000..f3fc49dac602 --- /dev/null +++ b/packages/kit/test/apps/basics/src/routes/load/server-data-reuse/with-changing-parent/with-server-load/+page.server.js @@ -0,0 +1,4 @@ +/** @type {import('./$types').PageServerLoad} */ +export function load() { + return { server: true }; +} diff --git a/packages/kit/test/apps/basics/src/routes/load/server-data-reuse/with-changing-parent/with-server-load/+page.svelte b/packages/kit/test/apps/basics/src/routes/load/server-data-reuse/with-changing-parent/with-server-load/+page.svelte new file mode 100644 index 000000000000..f64e3918b0b0 --- /dev/null +++ b/packages/kit/test/apps/basics/src/routes/load/server-data-reuse/with-changing-parent/with-server-load/+page.svelte @@ -0,0 +1,8 @@ + + +
Page with server load
+ +{JSON.stringify(data)}diff --git a/packages/kit/test/apps/basics/test/client.test.js b/packages/kit/test/apps/basics/test/client.test.js index 80bc78ab210e..086452215eef 100644 --- a/packages/kit/test/apps/basics/test/client.test.js +++ b/packages/kit/test/apps/basics/test/client.test.js @@ -102,6 +102,22 @@ test.describe('Load', () => { ); await app.goto('/load/server-data-reuse/no-load'); expect(await page.textContent('pre')).toBe(JSON.stringify({ foo: { bar: 'Custom layout' } })); + + await page.goto('/load/server-data-reuse/with-changing-parent/with-server-load'); + expect(await page.textContent('pre')).toBe( + JSON.stringify({ + foo: { bar: 'Custom layout' }, + title: '/load/server-data-reuse/with-changing-parent/with-server-load', + server: true + }) + ); + await app.goto('/load/server-data-reuse/with-changing-parent/no-load'); + expect(await page.textContent('pre')).toBe( + JSON.stringify({ + foo: { bar: 'Custom layout' }, + title: '/load/server-data-reuse/with-changing-parent/no-load' + }) + ); }); test('keeps server data when valid while not reusing client load data', async ({