-
-
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
What's the point of rerunning the load
function on session
change?
#2252
Comments
This is beneficial because you are then able to refresh your page based on changed parameters. I can see that this behavior is sometimes not beneficial, not sure for the best way to handle this. Random things I just came up with:
Random thoughts on how to work around it right now:
|
Thanks for the response! |
Okay, so as I see the export async function load({ page, fetch, session }) {
if (!session?.user) {
return { status: 302, redirect: '/'};
}
const { data } = await api.get(...);
return { props: { data } };} This means that the page needs to have an exported value named ...
export let data;
... We want to use this This means that I can't change the |
To reproduce it see: https://github.com/csangonzo/sveltekit-refresh-test |
one way to workaround, probably?
|
This has always been a controversial feature. I wonder if we could turn it off, but let people re-enable it with |
I'd say a flag with the |
I can only speak for myself, but I've been using sveltekit since day one and I always looked at |
I vote for opt in. It's only in very rare cases I think I would need load to be reactive. |
Slightly related: Is the Sapper "preload" reactive? I'm not actually sure now but would this require an update to the migration doc too? |
I don't think so, at least the sapper doc doesn't mention anything like that. |
Sapper's |
i was thinking about the original post use-case, what is the expected behaviour if silent-refresh session fails? I think some options are, the page would be no longer valid and it would need to disable input / maybe show a message, or redirect away. to my understanding, load provides the page state, and if page content is dependent on session, page.params or page.query it seems natural to me to re evaluate load because changing inputs define a new page state. otherwise, setting new query parameters or changing params wouldn't load new data and update the page. maybe what can be done here is, to implement a logic to keep previous state or user changes so page can recover them on re-load or even not re-fetch/update if a previous state exists another solution i could come up would be to do silent-refresh when a graphql api call fails auth (token expired). For example urql auth exchange (https://formidable.com/open-source/urql/docs/api/auth-exchange/) has that kind of feature, if a graphql query/mutation fails auth (or about to expire before running the operation), it can run a refresh logic and retry the original operation transparently. this may prevent interrupting user input with a background refresh or doing unexpected loads |
@zeckon agree with your comment but I think it should explicitly be opt-in. There are probably many different use cases for a reactive load (although edge cases in my opinion) but it's currently not obvious and the unsupported (default) case should have it disabled in my opinion. Perhaps a way to disable it could be an option... but that's still not clear and I think would lead to reloads when a user doesn't want/expect it. |
The last two comments make me conflicted on this. I think it's good to rerun load when the page parameters change and I guess quite a few sites rely on this feature. On the other hand, having |
Agree. Defaulting with
|
I vote for the |
I would go for part of return (as it affects load function itself),... and maybe also instead return whole page and session object instead strings, to tell which objects to invalidate, (not just name of object, but it's reference instead). |
I have a design proposal for session persistence and refreshing sketched out in #1726 (comment). Part of that design includes the idea that As far as I can tell from reading the discussion here, most people are surprised by For anyone who wants session assignment not to cause Edit to add: Also, would an automatic session refresh feature (where the user clicking back on the tab automatically triggers a
Would that behavior, where tab B calling |
Sounds good! |
@rmunn I think in your case the |
This comment has been minimized.
This comment has been minimized.
<script context="module">
export async function load({ page, session }) {
return {
invalidate: { page, session }, // see reasons down.
status: 200,
props: { id: page.params.id },
};
}
</script> reasons: array is ordered list, and I would say, order doesn't matter in this case, so object makes it feels more natural. name instead string? auto complete in IDE and IDE understand that page in params of load is connected to page in invalidate property. Performance is not problem, as it's passing reference, not moving whole objects in memory. (it will even takes less memory, as there will be not another array of strings, even tho it's just few bytes) |
FWIW, I think this is where the session reactivity is happening in Sapper: https://github.com/sveltejs/sapper/blob/master/runtime/src/app/app.ts#L241 Looks like it was done as part of this commit to support Svelte 3: sveltejs/sapper@ca034d0 The original thinking seems to be that users could end up in weird states and need the |
@Mlocik97 Agree. I've hidden my comment as it's just going to confuse things. |
I vote up for making the reactivity of |
@seanlail - I missed the notification email when you wrote this two weeks ago so I just saw your question. You're right that that's not really part of this issue per se, but it's certainly related. The reason I asked that question here was because I want to get feedback from people who are surprised when |
What about this half-baked idea? If people like it I'll try to bake it fully and turn it into a feature request. The Would that be too much "magic" for people? Would you prefer explicitly calling |
@rmunn If I understand what you're suggesting: So instead marking one or many of the load store params as "reactive" to rerun load, you instead have more fine-grained ability to say "re-run when this specific nested value of X store changes". I like it. Although I imagine it will probably be difficult to implement, it may just cover all use cases. 👍 |
It would only apply to the special ... I think that might be a bit too much magic. Better to have a new return value from export async function load({ page, session }) {
const { user } = session;
// Do something with session.user, like check access permissions to the page
return {
props: { user },
reloadIfChanged: { session: { user: { permissions: true } } },
};
} Then the default would be to not rerun on Hmm, I can already see a problem with that proposal. What if the username changes, to a different user who happens to have the same set of permissions? Say, Jane Smith logged out and Bob Jones logged in, but both are basic free-tier users so their page permissions are the same. You'd have to remember to write Not 100% sure what the right answer is here. |
Vote up to make it opt-in with |
Hello, Why not leaving the functionality as it is and use a derived or custom store around session? REPL ideas: https://svelte.dev/repl/fdbf426e0bd84855b8f88bc6c3b7f488?version=3.45.0 |
Just a quick note that the approach here that seems to be most accepted, |
We are not React here.... I'm against |
I absolutely agree we're not React here, but I also think it'd be fallacious to say "It's like React, therefore it's bad and we don't want to do it." React does a lot of things well. Can you clarify what abilities we would lose? |
I already explained how invalidate would give better control in load... in case of load, you can do something like: export async function load({ session, page, fetch }) {
// some logic
const shouldSessionInvalidate /* @type boolean */ = ...
return {
invalidate: shouldSessionInvalidate ? { session } : {}
}
} that's impossible to do with |
Absolutely agreed that it goes better in |
I feel like rerun |
Describe the problem
It's more like a genuine question about a new feature in sveltekit. I just noticed this in the docs:
After login when I set
$session.user = response.user;
like in the realworld demo (https://github.com/sveltejs/realworld/blob/master/src/routes/login/index.svelte) I see that the login page'sload
function runs again.I use access and refresh tokens in my app and I implemented a silent refresh method, something like the one mentioned here: https://hasura.io/blog/best-practices-of-using-jwt-with-graphql/ .
When I do a silent refresh I reset the session, but this causes a rerun of the load function now.
The problem with this is I have a lot of components that use data returned by the load function (like an editor that gets it's content first from the load function, or a form that has it's model from the load function) and if it runs again the data changes on the page, so if I had unsaved changes in my editor for example and my silent refresh runs, it loads the data again from the server so my changes will be lost.
Describe the proposed solution
If someone could explain the benefits we get from this feature I'd be really glad, because right now this is the first thing that doesn't make any sense about sveltekit for me.
Alternatives considered
No response
Importance
i cannot use SvelteKit without it
Additional Information
I follow the changes of sveltekit from day to day, last year I decided to ditch angular in favour of svelte and migrated almost every app to it that we had and I'm having problems with the authentication part because of this change. I'm open to alternative solutions as well.
The text was updated successfully, but these errors were encountered: