-
-
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
Rethink or clarify when load
runs
#2301
Comments
I have a design for Would a design where |
We just got another complaint about the existing behavior in #2455 I figured out where this is happening and under what conditions while looking at that issue:
|
<script context="module">
export async function load({ page, session }) {
return {
invalidate: { page, session },
status: 200,
props: { id: page.params.id },
};
}
</script> in another thread, I think, we ended with this proposed API for setting what to invalidate... this gives really a lot of abilities to devs. |
This is an interesting idea. The contract could be "the objects in invalidate need to implement some certain API, which page/session etc already implement, and then that stuff is just watched and load is retriggered whenever one of the objects update". |
I just like to suggest a way to prevent load from running whatsoever, I think these suggested methods can also be used to implement such functionality My use for this is: I have a global store that is initialized with data returned from the load function. Currently the store is resetted once the user navigates back to the page but I'm planning to implement a logic to just ignore the 'init' if the store has data In either case I think that it's not that far of an idea to "prevent when load re-runs while on the same page" to "prevent load from running while navigating back to the page" |
I believe this is where Next.js Data Fetching shines. It is explicit to users and the functionality is distinctly separate across the APIs. I realise Has the idea of splitting |
Chiming in from the 'new devs' perspective Splitting Load seems to make a lot of sense to me. Typescript was a huge factor in cutting my learning curve across the board, and hobbling Load into a monolith lacks the clarity of a well defined set of interfaces. Just looking at the docs, it's the 2nd largest section - outdone only by Configs! Even the Routing and Modules section are shorter. I fully support splitting That is to say, since I really hate the term 'stuff' for this though - something like 'mids' or 'fixtures' sounds more descriptive to me.
(let me know if I'm misunderstanding anything, it's rather la-- early, on this side of the pond) |
Non activé car cela pose un problème bizarre avec la première animation. Sans doute lié à sveltejs/kit#2301
I am very new to Kit so I might be in over my head, but what if This feels more idiomatic to me because it mirrors how reactivity is handled in the component script, just without the syntax sugar. With the current approach (where |
load must run on server, if You want prerendering/SSR. The issue is not about if it runs on client or server, but about when it reruns, aka if it reruns when $session change. It makes sense to let user define when to rerun, and there is already suggestions about it, in other thread. |
I was suggesting that it still runs on both the server and the client, but only once on the client. Reactivity would then be provided by explicitly subscribing. I don't think this would affect SSR at all. |
Also, something weird to me: For instance, suppose i have 2 query params: This export function load({ page }) {
console.log('load', page.query.get('b'));
return {};
} I find this pretty annoying since it is counter intuitive, especially given that regular |
I think that how I am not a very experienced developer that I just transitioned from plain HTML js CSS, then react, nextjs, and now sveltkit. nextjs provide simple functions and middleware implementations so you can be very clear of where that code is run (on client-side, on request, or during build time). While sveltekit have access to a lot of environment variable, I found that harder to maintain as while nextjs you can split concern into different functions or parts of code, in sveltekit you are doing conditionals in a big load function. I think it would be easier if we split server-side and client-side code into two functions just like nextjs, and use conditional in the server-side to distinguish build time and SSR (or maybe split again). Such that the load function in the client is really running before component rendering and only run once before creation or upon parameter changes (I am also actually not 100% sure what this suppose to mean, or it is a client-side hook somehow?) I also wonder by Another issue I met (though I think this might only be caused by my lack of knowledge and should solve it easily maybe when I move to that point in my project?) is about caching. From my understanding traditionally headers are the only way to specify caching and normally you specify the proxy level(CDN) and client level (public and private caching), with optional maxage properties. But what modern cloud computing complicates things is that caching can be done in a whole lot more ways, and not only the "traditional ways" of caching. In an abstract way of thinking, normally we may cache info upon SSR endpoints and in the browser. When mixed with authentication, we might want to cache also protected content, or even protected user content on the CDN that requires authentication once people retrieve those content (just read some docs so may not get the full picture, but seems these all complications comes with Vercel middleware or even more freedom with amazon Cloudfront@functions and lambda@edge and other similar functions in other cloud platforms? like Alibaba Cloud. edge routines etc). Is that possible for sveltkit to also implement caching functions and then the adapters could choose how they would implement it? (I know it could also be done by those platform/adapters providing SDKs or APIs but it would be so advanced for svelte/kit maybe if it is a pluggable module maybe?) Lastly, which I think might be a bit off-topic but rather more personal is that I would like to ask how you guys refactor code? I used components and tailwind to abstract out designs and a lot of binds to allow functions to deep dive into the fundamental components, but I still find a page with 375 lines is a bit too long to read and maintain. I searched online (not much content seemed to focus on refactoring frontend component frameworks), and I thought of splitting a page into different sections then including them back. Yet I am not sure if that's the right way to do it as that would create a new folder may be for each page, then also requires a store for state variables that are shared across the page(or maybe not just by binding starting from the page -> section -> fundamental components) That's what I want to share and sorry that's so long lol. Thank you guys if you finished reading it! |
There seem to be a couple of threads all talking about the same issue, but this seems to be the most active / best fleshed out -- I left this comment on the other one as well, but wanted to make sure it was documented in both places: Just a quick note that the approach here that seems to be most accepted, edit: The "return an invalidate object from the |
With the upcoming changes to What is probably most viable is to use the new |
Describe the bug
The docs say:
But I don't think that is when
load
runs? I thinkload
is reactive to its input parameters and runs everytime they changeThis is confusing to people. E.g. they don't expect it to run when
$session
changes #2252.Also, sometimes
load
in your layout will run whenever the path changes and sometimes it will load only when the page first loads, which is super hard to discover. This also leads to weird workarounds like accessing$page.path
to causeload
to run eventhough you don't need to do anything with it (#1165 (comment))This all feels a little too magical to me. It's not very clear that it will happen (vs if I did something like
$: load = function(page) {}
. Maybe this is just how Svelte works, but it's not obvious to me. The Svelte docs don't have any stores as function parameters, and I never did something like$page.subscribe(load)
.We also have
page
accessible from two places: theload
parameters and a store. Is there a reason we have two ways of doing the same thing? Doesload
only re-run when we accesspage
via one of those locations or via both?We've also gotten a request to not have
load
run on every URL change (#2673)At the very least we need to update the docs for
load
, but I wonder if we can't make things more explicit.Part of the difficulty here is that components aren't recreated for each page view and so sometimes you get a new page component created and sometimes you don't. Given that, we sort of have to rerun
load
when$page
changes. This is somewhat related to the transition bugs that we have. The reason we don't create new components for each page view is so that when viewing multiple pages rendered by the same svelte file you can have transitions - though I'm not sure that we couldn't accomplish that other waysThis also feels slightly related to the before / after hooks for routing (#552) because some people try to put code that will run on every page navigation on
load
in__layout.svelte
, but you might also be able to accomplish some of those use cases with routing hooks.Reproduction
n/a
Logs
No response
System Info
Severity
serious, but I can work around it
Additional Information
No response
The text was updated successfully, but these errors were encountered: