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: return updated layout data if child uses parent #13076

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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/strong-adults-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: return updated layout data if `parent` was used
eltigerchino marked this conversation as resolved.
Show resolved Hide resolved
50 changes: 31 additions & 19 deletions packages/kit/src/runtime/server/data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export async function render_data(
try {
const node_ids = [...route.page.layouts, route.page.leaf];
const invalidated = invalidated_data_nodes ?? node_ids.map(() => true);
/** @type {Set<number>} */
const parent_invalid = new Set();

let aborted = false;

Expand Down Expand Up @@ -74,6 +76,8 @@ export async function render_data(
if (parent) {
Object.assign(data, parent.data);
}

parent_invalid.add(j);
}
return data;
}
Expand All @@ -96,27 +100,35 @@ export async function render_data(
});

let length = promises.length;
const nodes = await Promise.all(
promises.map((p, i) =>
p.catch(async (error) => {
if (error instanceof Redirect) {
throw error;
}

// Math.min because array isn't guaranteed to resolve in order
length = Math.min(length, i + 1);
/**
* @param {Promise<import('types').ServerDataSkippedNode | import('types').ServerDataNode | null>} p
* @param {number} i
*/
const handle_load_error = (p, i) =>
p.catch(async (error) => {
if (error instanceof Redirect) {
throw error;
}

// Math.min because array isn't guaranteed to resolve in order
length = Math.min(length, i + 1);

return /** @type {import('types').ServerErrorNode} */ ({
type: 'error',
error: await handle_error_and_jsonify(event, options, error),
status:
error instanceof HttpError || error instanceof SvelteKitError
? error.status
: undefined
});
})
)
);
return /** @type {import('types').ServerErrorNode} */ ({
type: 'error',
error: await handle_error_and_jsonify(event, options, error),
status:
error instanceof HttpError || error instanceof SvelteKitError ? error.status : undefined
});
});

let nodes = await Promise.all(promises.map(handle_load_error));

// return updated layout data if `parent` is used
if (parent_invalid.size) {
parent_invalid.forEach((i) => invalidated[i] = true);
nodes = await Promise.all(functions.map((fn) => fn()).map(handle_load_error));
}

const { data, chunks } = get_data_json(event, options, nodes);

Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/basics/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ test.describe('Invalidation', () => {

await clicknav('[href="/load/unchanged-parent/uses-parent/b"]');
expect(await page.textContent('h1')).toBe('slug: b');
expect(await page.textContent('h2')).toBe('count: 0');
expect(await page.textContent('h2')).toBe('count: 1');

// this looks wrong, but is actually the intended behaviour (the increment side-effect in a GET would be a bug in a real app)
expect(await page.textContent('h3')).toBe('doubled: 2');
Expand Down
Loading