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

Page props are re-set whenever session store is updated #3871

Closed
ponderingexistence opened this issue Feb 12, 2022 · 3 comments
Closed

Page props are re-set whenever session store is updated #3871

ponderingexistence opened this issue Feb 12, 2022 · 3 comments

Comments

@ponderingexistence
Copy link

ponderingexistence commented Feb 12, 2022

Describe the bug

In the docs, in the section about the load function, it's stated that:

...if url, session or stuff are used in the function, they will be re-run whenever their value changes... (here)

But this doesn't seem to be the case. I have the following page in my SvelteKit app:
index.svelte:

<script lang="ts" context="module">
    import type { Load } from '@sveltejs/kit';

    export const load: Load = () => {
        console.warn('index load function');
        return {};
    }
</script>

<script lang="ts">
    import { session } from '$app/stores';

    function changeSession() {
        $session.someField = Math.random();
    }
</script>

<pre>{JSON.stringify($session, null, 4)}</pre>

<button on:click={changeSession}>
    Change $session
</button>

If you click on the "Change $session" button, you'll see the message "index load function" appearing in the console, which obviously means the load function is re-run, even though session isn't used in the load function. This is inconsistent with the behavior described in the docs.

The strange thing, however, is that from the second time onwards, when the value of the '$session' store changes, the load function is no longer re-run. This is confusing.

The more severe bug, however, which seems to be related to this one, is that every time the $session store changes, the page's props are apparently re-set, which means reactivity is re-triggered throughout the entire page, potentially causing massive problems.

Consider the following:

<script lang="ts" context="module">
    import type { Load } from '@sveltejs/kit';

    export const load: Load = () => {
        console.warn('index load function');
        return {
            props: {
                userBasicInfo: {
                    id: 2,
                }
            }
        };
    }
</script>

<script lang="ts">
    import { browser } from '$app/env';
    import { session } from '$app/stores';

    export let userBasicInfo;

    function changeSession() {
        $session.someField = Math.random();
    }

    $: console.log('userBasicInfo changed:', userBasicInfo);
</script>

<pre>{JSON.stringify($session, null, 4)}</pre>

<button on:click={changeSession}>
    Change Ssession
</button>

<hr />

{#if browser}
    {#await fetch(`https://reqres.in/api/users/${userBasicInfo.id}`).then(r => r.json())}
        Loading...
    {:then result}
        <pre>{JSON.stringify(result, null, 4)}</pre>
    {/await}
{/if}

In this setup, whenever you click on the "Change $session" button — which is to say whenever you update the $session store — the fetch request in the markup, which uses the userBaicInfo prop, is re-triggered, even though it obviously shouldn't. And you can see the userBasicInfo changed: ... message in the console which proves that it's because of the userBasicInfo prop getting re-set.

Reproduction

You can create an empty SvelteKit skeleton project and then copy-paste the two code snippets above into the index.svelte file, and then npm run dev => open your browser and see the behavior for yourself.

Logs

No response

System Info

System:
    OS: Windows 10 10.0.19042
    CPU: (4) x64 Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz
    Memory: 1.09 GB / 7.89 GB
  Binaries:
    Node: 14.15.0 - C:\Program Files\nodejs\node.EXE     
    Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD       
    npm: 6.14.8 - C:\Program Files\nodejs\npm.CMD        
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (97.0.1072.76)
    Internet Explorer: 11.0.19041.1202
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.17
    @sveltejs/kit: next => 1.0.0-next.260
    svelte: ^3.44.0 => 3.46.4

Severity

blocking all usage of SvelteKit

@babichjacob
Copy link
Member

Duplicate of #3732 -- don't do that

@ponderingexistence
Copy link
Author

@babichjacob Well, I didn't get a response in that issue even though pretty much all of the issues that were created after mine did, I thought maybe there's something wrong.

Should I just wait?

@rmunn
Copy link
Contributor

rmunn commented Feb 21, 2022

@Aaron2321d - Yes, wait for someone to have time to look at your issue. There are a limited number of Svelte maintainers and volunteers. If you want attention to an issue, mentioning it on the #svelte or #svelte-kit channel (as appropriate) on the Discord would be appropriate, as long as you don't do so too often. No more than once a week would be my recommendation.

That said, you don't need to do so right now, as I'm taking a look at #3732 now and I'll post my findings when I'm done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants