From 0c885f48fce281e2a95779b01786961b2607bf02 Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Fri, 29 Nov 2024 11:31:48 +0800 Subject: [PATCH 1/6] return updated layout data if child uses parent --- packages/kit/src/runtime/server/data/index.js | 49 ++++++++++++------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/packages/kit/src/runtime/server/data/index.js b/packages/kit/src/runtime/server/data/index.js index b278ee31873b..cbf5ca8a5409 100644 --- a/packages/kit/src/runtime/server/data/index.js +++ b/packages/kit/src/runtime/server/data/index.js @@ -39,6 +39,7 @@ export async function render_data( try { const node_ids = [...route.page.layouts, route.page.leaf]; const invalidated = invalidated_data_nodes ?? node_ids.map(() => true); + let parent_invalid = false; let aborted = false; @@ -64,6 +65,8 @@ export async function render_data( state, node, parent: async () => { + parent_invalid = true; + /** @type {Record} */ const data = {}; for (let j = 0; j < i; j += 1) { @@ -96,27 +99,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} 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) { + invalidated.fill(true, 0, -1); + nodes = await Promise.all(functions.map((fn) => fn()).map(handle_load_error)); + } const { data, chunks } = get_data_json(event, options, nodes); From 9e70e643e9aa96895ba1c61786c7f801efb4de5f Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Fri, 29 Nov 2024 11:31:52 +0800 Subject: [PATCH 2/6] update test --- packages/kit/test/apps/basics/test/client.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/test/apps/basics/test/client.test.js b/packages/kit/test/apps/basics/test/client.test.js index 56b2c96bc744..00cbdb0d7b2c 100644 --- a/packages/kit/test/apps/basics/test/client.test.js +++ b/packages/kit/test/apps/basics/test/client.test.js @@ -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'); From d7a4c38729dfdcb33daa68138f913c5704dc7271 Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Fri, 29 Nov 2024 11:32:47 +0800 Subject: [PATCH 3/6] changeset --- .changeset/strong-adults-rest.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/strong-adults-rest.md diff --git a/.changeset/strong-adults-rest.md b/.changeset/strong-adults-rest.md new file mode 100644 index 000000000000..e4bf9509a923 --- /dev/null +++ b/.changeset/strong-adults-rest.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: return updated layout data if `parent` was used From b8666271fc24dc41b92fd59e6a6b9d023ff7e02c Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Fri, 29 Nov 2024 11:42:07 +0800 Subject: [PATCH 4/6] only invalidate necessary parent load functions --- packages/kit/src/runtime/server/data/index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/kit/src/runtime/server/data/index.js b/packages/kit/src/runtime/server/data/index.js index cbf5ca8a5409..18cf52d1cca5 100644 --- a/packages/kit/src/runtime/server/data/index.js +++ b/packages/kit/src/runtime/server/data/index.js @@ -39,7 +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); - let parent_invalid = false; + /** @type {Set} */ + const parent_invalid = new Set(); let aborted = false; @@ -65,8 +66,6 @@ export async function render_data( state, node, parent: async () => { - parent_invalid = true; - /** @type {Record} */ const data = {}; for (let j = 0; j < i; j += 1) { @@ -77,6 +76,8 @@ export async function render_data( if (parent) { Object.assign(data, parent.data); } + + parent_invalid.add(j); } return data; } @@ -124,8 +125,8 @@ export async function render_data( let nodes = await Promise.all(promises.map(handle_load_error)); // return updated layout data if `parent` is used - if (parent_invalid) { - invalidated.fill(true, 0, -1); + if (parent_invalid.size) { + parent_invalid.forEach((i) => invalidated[i] = true); nodes = await Promise.all(functions.map((fn) => fn()).map(handle_load_error)); } From 7ec5636db5d1bdb16a1140ab062241922f65fef2 Mon Sep 17 00:00:00 2001 From: Tee Ming Date: Fri, 29 Nov 2024 11:45:09 +0800 Subject: [PATCH 5/6] Update .changeset/strong-adults-rest.md --- .changeset/strong-adults-rest.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/strong-adults-rest.md b/.changeset/strong-adults-rest.md index e4bf9509a923..a929cf675018 100644 --- a/.changeset/strong-adults-rest.md +++ b/.changeset/strong-adults-rest.md @@ -2,4 +2,4 @@ '@sveltejs/kit': patch --- -fix: return updated layout data if `parent` was used +fix: return updated layout data if child uses `parent` From b2880faa658b74c4f96736eabb99d7ebbf9cef1d Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Fri, 29 Nov 2024 11:48:25 +0800 Subject: [PATCH 6/6] format --- packages/kit/src/runtime/server/data/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/runtime/server/data/index.js b/packages/kit/src/runtime/server/data/index.js index 18cf52d1cca5..ce5f3966870a 100644 --- a/packages/kit/src/runtime/server/data/index.js +++ b/packages/kit/src/runtime/server/data/index.js @@ -126,7 +126,7 @@ export async function render_data( // return updated layout data if `parent` is used if (parent_invalid.size) { - parent_invalid.forEach((i) => invalidated[i] = true); + parent_invalid.forEach((i) => (invalidated[i] = true)); nodes = await Promise.all(functions.map((fn) => fn()).map(handle_load_error)); }