Skip to content

Commit

Permalink
[fix] load function should not leak props
Browse files Browse the repository at this point in the history
When navigating from a page with a load() function to a page without a
load() function, props were not being reset, so if the second page had a
prop with the same name as the prop returned by the first page's load()
function, the second page would receive the same prop that the first
page did.
  • Loading branch information
rmunn committed Sep 3, 2021
1 parent 621e071 commit 7bbc7e8
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/polite-elephants-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[fix] load function should not leak props to other components
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/client/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ export class Renderer {

for (let i = 0; i < filtered.length; i += 1) {
const loaded = filtered[i].loaded;
if (loaded) result.props[`props_${i}`] = await loaded.props;
result.props[`props_${i}`] = loaded ? await loaded.props : null;
}

if (
Expand Down
8 changes: 8 additions & 0 deletions packages/kit/test/apps/basics/src/routes/load/_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,12 @@ export default function (test, is_dev) {
assert.equal(await page.innerHTML('.parsed'), '{"oddly":{"formatted":"json"}}');
assert.equal(await page.innerHTML('.raw'), '{ "oddly" : { "formatted" : "json" } }');
});

test('does not leak props to other pages', '/load/props/about', async ({ page, clicknav }) => {
assert.equal(await page.textContent('p'), 'Data: undefined');
await clicknav('[href="/load/props/"]');
assert.equal(await page.textContent('p'), 'Data: Hello from Index!');
await clicknav('[href="/load/props/about"]');
assert.equal(await page.textContent('p'), 'Data: undefined');
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<header>
<nav>
<a href="/load/props/">Index</a>
<a href="/load/props/about">About</a>
</nav>
</header>

<main>
<slot />
</main>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
export let data;
</script>

<h3>About</h3>

<p>Data: {data}</p>
14 changes: 14 additions & 0 deletions packages/kit/test/apps/basics/src/routes/load/props/index.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script context="module">
export async function load() {
const data = 'Hello from Index!';
return { props: { data } };
}
</script>

<script>
export let data;
</script>

<h3>Index</h3>

<p>Data: {data}</p>

0 comments on commit 7bbc7e8

Please sign in to comment.