Skip to content

correctly determine whether a route uses server data #6275

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

Merged
merged 1 commit into from
Aug 25, 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/modern-files-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Correctly determine whether route uses server data
8 changes: 2 additions & 6 deletions packages/kit/src/core/sync/write_client_manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,13 @@ export function write_client_manifest(manifest_data, output) {
while (layouts.at(-1) === '') layouts.pop();
while (errors.at(-1) === '') errors.pop();

/** @type {import('types').RouteData | null} */
let current_route = route;

/** @type {import('types').PageNode | null} */
let current_node = route.leaf;

let uses_server_data = false;
while (current_route && !uses_server_data) {
while (current_node && !uses_server_data) {
uses_server_data = !!current_node?.server;
current_route = current_route.parent;
current_node = current_route?.layout ?? null;
current_node = current_node?.parent ?? null;
}

// encode whether or not the route uses the server data
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { increment } from './state';

export const load = () => {
return {
a: increment()
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<slot />
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { increment } from './state';

export const load = () => {
return {
b: increment()
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
import { invalidate } from '$app/navigation';

/** @type {import('./$types').PageData} */
export let data;
</script>

<h1>a: {data.a}, b: {data.b}</h1>

<button on:click={() => invalidate()}>invalidate</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { reset } from '../state.js';

/** @type {import('./$types').RequestHandler} */
export function GET() {
reset();
return new Response('ok');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let count = 0;

export function increment() {
return count++;
}

export function reset() {
count = 0;
}
20 changes: 20 additions & 0 deletions packages/kit/test/apps/basics/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,4 +640,24 @@ test.describe.serial('Invalidation', () => {
await clicknav('[href="?a=3"]');
expect(await page.textContent('h1')).toBe('3');
});

test('server-only load functions are re-run following forced invalidation', async ({
page,
request
}) => {
await request.get('/load/invalidation/forced/reset');

await page.goto('/load/invalidation/forced');
expect(await page.textContent('h1')).toBe('a: 0, b: 1');

await page.click('button');
await page.waitForLoadState('networkidle');
await page.waitForTimeout(0); // apparently necessary
expect(await page.textContent('h1')).toBe('a: 2, b: 3');

await page.click('button');
await page.waitForLoadState('networkidle');
await page.waitForTimeout(0);
expect(await page.textContent('h1')).toBe('a: 4, b: 5');
});
});