-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Navigating to new rest parameters in route does not cause page to re-render #3884
Comments
you are doing it all wrong.
this is how it should look like. this is not a bug. |
Duplicate #3727 |
Ahhh yes! I see now @raprocks! Ok, so that part seems to be working as expected. I guess the behavior I was seeing was actually a little bit different from what I described initially. So the path changes work as expected, but let's say I do something like -
Let's say we navigate to /test, it executes the script and it would display Like I said, I'm very new to svelte, so some of the nuances are still lost on me. I guess I'm imagining a scenario where navigating within the SPA may want to use data that might be available in the stores, which aren't available from the server-side script. |
if its a writable store then you should update it rather than creating a new one everytime. Also store it in some js module and import it and subscribe to it. On mobile right now so cannot try. Will let you know as soon as i do if that works. |
Alright, I think I'm starting to understand more. If I want something inside of the <script></script> to execute again after the parameters change, should I do something like -
I'm guessing the <script></script> only executes when the component renders initially, but not when the props change. So I would have to listen to the page store change instead if I want it to re-render, right? If that's the case, I think this issue can be closed. |
Not your fault, this was a bug in a recent version of Kit — fixed by #3925! |
Thanks for the reply @Rich-Harris! Admittedly, I might be even more confused now because I'm still seeing the original issue with 272. <!-- The load function runs every time I navigate to a new page -->
<script context="module">
export const load = async ({ params }) => ({
props: {
someData: params.test,
},
});
</script>
<!--
Should this whole script block execute every time a new subsequent page that
matches this pattern is visited? Or only the first time it renders from the server, and
then the next time it renders on the client?
-->
<script>
export let someData;
let someDataUppercased = someData.toUpperCase();
</script>
<main>
<h1>{ someData } > { someDataUppercased }</h1>
<a href="/testing/foo">foo</a>
<a href="/testing/bar">bar</a>
</main> Before trying to replicate again using the template (I'm not even sure which version of sveltekit it's using), I just want to see if my understanding is right, or if I should be subscribing to the page store inside of the Thanks! |
Note that #3925 hasn't been published yet (it's not even in |
Hmm, alright, maybe I was reading the commit history incorrectly then, my apologies @mrkishi. Since #3925's commits included the changelog change present in https://github.com/sveltejs/kit/releases, I guess I assumed incorrectly. I'll hold off on testing again then. |
I'm also facing this issue. |
Looks like #3925 has been published now, in |
Unfortunately, as of 1.0.0-next.276, I still seem to be experiencing the same behavior. I'll update the stackblitz environment above later today to double check just in case it's something weird with my local environment. |
I updated the stackblitz environment and was able to replicate the same behavior I encountered before, so I suspect #3925 did not fix this particular issue. |
I've played around with your stackblitz setup, and I believe I understand what's going on. If I'm right, this is not a bug, but a case of you not yet understanding how Svelte is supposed to work. So I'll try to explain it. But first, a quick TL;DR: Replace Now to go into more detail to explain. For starters, I'll answer the question you asked in the code sample of this issue comment:
The answer is that it's not page visits that cause the Now, the thing that's causing you some confusion is that in Svelte-Kit, pages are also components. And when you put an Which means that the <a href="/test/first-path">Test One</a>
<a href="/test/second-path">Test Two</a> And now for the And since the And that's why I said that this is not a bug, but a case of you not yet understanding how Svelte is supposed to work. Because you declared Replace |
First off, thank you so much for the detailed explanation @rmunn. So that's similar to what I mentioned earlier where I can also subscribe to the page store for the updates, right? Alright, I think I understand better now. It's still a little confusing coming from a React background, where really any reference change would result in a re-render. For example, I assumed that since the props changed, it would result in a re-render of the component, even if it was already mounted (or whatever svelte's equivalent terminology is). But I'm also seeing similar behavior elsewhere that are not related to route changes. For example, if I render a component and I pass in an object (as opposed to a string, which doesn't seem to have the same issue), I have to specifically make the script reactive and listen to the props change. While subscribing to stores made sense, I would have assumed a prop change would automatically cause the component to re-render. But it seems I have a lot more to learn about svelte. Thank you again, I'll close this bug report then since it sounds like this is expected behavior, and as I suspected, a limitation of my knowledge. At minimum, I think it might be helpful if at some point the svelte kit documentation could have some sort of documentation explaining some of this behavior, much like your wonderfully detailed explanation. |
Just a nitpick: when a prop changes, the component does re-render. But in Svelte, the main script tag runs on component instantiation, unlike a React render function that gets called repeatedly. If you have a prop ( |
Yeah, it's funny that you replied, @mrkishi - I was just rubber duckying to my wife as she nodded along when I suddenly understood what was happening. I normally destructure the prop that's passed in, and I realized that the destructuring only happens once. So you have to either a) reference the object attribute directly or b) set the destructured property as reactive. Thank you for confirming my suspicions about that too! |
Additional documentation for those still facing this issue: |
Describe the bug
Let me preface this by saying that I'm very new to svelte and svelte kit, so maybe I'm doing something dumb. I'm doing something similar to #643, although that issue seems to be closed without an obvious resolution.
Basically, if I have
/some-path/[...slug].svelte
, I can do -/
->/some-path/first-path
But when I am on
/some-path/first-path
and I try to navigate to/some-path/second-path
...The url changes as expected, but the page itself doesn't realize it should be re-rendering, so the contents stay the same. A refresh of the page does load the new page as expected.
Reproduction
https://stackblitz.com/edit/sveltejs-kit-template-default-mppqbl
Click on 'Test'
Click on 'Works Ok' - Navigates to a different route as expected and renders 'first-path'.
Click on 'Home'
Click on 'Test'
Click on 'Broken' - Nothing happens.
I would expect 'another-path' to be displayed on the page.
Logs
No response
System Info
Severity
annoyance
Additional Information
I didn't really know what to put for severity. Surely I'm not the first person to have run into this issue, so I assume I'm doing something silly, or there is a way to work around it.
The text was updated successfully, but these errors were encountered: