-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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 script is only executed once #2455
Comments
Hmm. I mostly see the message printed only once. I have seen it printed twice a few times as I experimented with various things, but can't reproduce The reason it does not execute everytime with the demo repo is:
|
If you start from the homepage then yes, you'll only see one log. But if you start from /1 (just reload that page) then you'll see two logs. |
Even then I'm just seeing one log. I wonder if there's some kind of race condition here. |
Strange, but either way, I'd expect the code in the script block to run on every page view, or am I wrong in expecting that? |
If you're switching between two pages that are rendered by the same page component, no, that doesn't re-create the component - it just runs |
Yeah it does indeed run the load function every time, but then when I want to do something with the props that are returned by the load function, that doesn't happen after the second page load. If that's how it's supposed to work, then I think that's quite confusing. How would I work around it? |
You can use |
I couldn't find it because it got transferred to the SvelteKit repo. There's a lot of discussion about this in #552. |
@benmccann Do you still think there's a bug here? I'm looking at this issue more closely, and it just looks like another instance of the (apparently controversial and poorly-documented) decision to not force re-creation of page components when switching to another URL that's handled by the same route, and that this should be closed in favor of #552. |
It seems that this is indeed a duplicate of #552. I would like to add my vote to the "this is extremely confusing, poorly documented, and very annoying to work with" crowd. I now have to add a bunch of extra code and logic all over my SK app because I don't know how a page route will be made; it it's from within the same parent layout or not, and it behaves vastly different for those two situations. |
So my breaking example:
Can indeed be fixed like this:
But it feels wrong that I have to know the inner working of Svelte's reusing / remounting logic to work around it 😓 Anyway, I guess this ticket can be closed because we have #552. |
Even if we accept the current behavior as working as intended, I do think there's another bug here. The code shouldn't sometimes be run once and sometimes run twice. I think there might be a race condition here, which is why I left this open |
I wonder if it's running twice because of SSR. The issue description mentions navigating to http://localhost:3000/1 first, which would cause an SSR render. Then going to http://localhost:3000/2 would cause client-side navigation, and it's possible that that's causing something to run for the first time client-side that previously ran during SSR. I wonder if #1214 might be related. @kevinrenskers - If you add |
With SSR disabled I only see one log instead of two. |
I was seeing two client logs, so what I was seeing wasn't related to SSR. But now I'm wondering if maybe one of the logs was from me updating the code and triggering HMR because I can't reproduce it Since I can't reproduce the running twice behavior I'll close this as a duplicate of #552 |
I'm very surprised that you can't reproduce it. I'll create a video showing the issue because yes it's two client side logs. |
Haha well.. turns out I can't reproduce it anymore either. Strange, I swear I was able to trigger it reliably but no idea what changed. Oh well, #552 is a fine replacement anyways :) |
Describe the bug
When switching between different dynamic pages, it seems that their script is only ever executed twice.
Reproduction
I think the problem is kind of hard to explain, but I have a reproduction available: https://github.com/kevinrenskers/svelte-script-problem-repro. If you run this and open the console, then when switching between the different links you can see the content changing, but the console.log is only executed twice: once on the initial page load, and once on a route change, and then no more.
Of course this is a really simple example and you will say to use something like
$: console.log("HELLO")
, and yes, in that case it's run every time. But in my real world app I am fetching data in theload
method and then in the script itself I want to store it in a svelte store:And that works fine.. for the first two pages that I open. Then on the third page, the code in
<script>
is not executed at all. I don't understand why the script code only runs on the first 2 page loads?Logs
No response
System Info
Severity
With my current knowledge: blocking all usage of SvelteKit. But maybe there's a workaround or I am doing it wrong?
Additional Information
No response
The text was updated successfully, but these errors were encountered: