-
-
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
Avoid running load
on the server unnecessarily
#6056
Conversation
🦋 Changeset detectedLatest commit: 79b2a6a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Obviously there are other things that could invalidate a |
If there are no |
I hadn't considered that but yes, it should definitely work that way. (We already skip requesting server data if we know a given route doesn't use it) |
Should we err on the side of caution/correctness and always rerun in certain cases? You might scratch your head why something doesn't update when you run into the |
I'm more likely I think to be annoyed if it is re-run when I don't want it to than I am to be annoyed that it isn't re-run when I want it to. The latter leads to obvious bugs, while the former leads to inefficiencies that might not even be obvious during local development because network requests are so fast. It's also a lot clearer how to tell the In my app, I'm currently planning on using Just like in Svelte's reactivity where we give ways to access variables without Svelte seeing it, there should also be an escape hatch in SvelteKit for "I'm accessing this value in my |
These are good points. I also just realized that the |
I am glad to see this is already on the radar. I'd like to mention our use case to help shape future changes. We're building a multi-tenant Node app. The app loads tenant's information (skins, for example) based on the hostname in the request. We used to do this with a
|
It feels like the number of unique cases of "who reloads what when except if" is not fully graspable by the average mind, just like with reactivity except reactivity is mostly hidden to users but load functions are not. The solution should be explainable in a very straightforward way (almost as a precept) to avoid mob incomprehension. The "intelligent reload" is definitely a killer feature of sveltekit but it is possible I'm biased since all my cases have always fitted with its magic. The three levels of management "simple/advanced/iknowwhatimdoing" approach feels great for tools that solve infinite cases problems, being :
On a semi-serious note it would be a nice case for some documentation-driven-development! |
…, but need to switch branches so im gonna commit it now with an undisciplined commit message
I'm creating a PWA and running into this issue. How can I prevent SvelteKit from running |
Shouldn't the PWA take care of that transparently? The service worker intercepts the request and serves something from cache instead. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did some small tweaks, overall looks good 👍
Hey, have run into a small issue after this PR
In the Before this PR, layout.server.ts was triggered on every navigation & I had the latest data shown on each page, but that is not the case now. I was wondering if there is an explicit way to call load in I've tried the following but they dont work // select-profile/+page.svelte
invalidate();
goto('/auth/login/select-profile'); The above does refresh the |
When is |
Hey, just realized that //+layout.server.ts
import { get } from '$lib/api';
import type { OnboardingInfo } from '$lib/types/onboarding.type';
import { redirectIfNotAlreadyThere } from '$lib/utils';
import { redirect } from '@sveltejs/kit';
import type { LayoutServerLoad } from './$types';
export const load: LayoutServerLoad = async ({ locals, url }) => {
if (locals.bearerToken) {
const response: OnboardingInfo = await get(`users/onboardingInfo`, locals.bearerToken);
if (response.error) return { bearerToken: locals.bearerToken };
else if (response.authStatus === 'COMPLETE') throw redirect(302, '/app');
else if (response.authStatus === 'LOGIN')
redirectIfNotAlreadyThere('/auth/login', url.pathname);
else if (response.authStatus === 'CREATE_PASSWORD')
redirectIfNotAlreadyThere('/auth/register/password', url.pathname);
else if (response.authStatus === 'ENTER_INFO')
redirectIfNotAlreadyThere('/auth/register/info', url.pathname);
else if (response.authStatus === 'SELECT_PROFILE')
redirectIfNotAlreadyThere('/auth/login/select-profile', url.pathname);
return {
bearerToken: locals.bearerToken,
user: response,
};
} else {
redirectIfNotAlreadyThere('/auth/login', url.pathname);
}
};
export function redirectIfNotAlreadyThere(to: string, pathname: string) {
if (to !== pathname) throw redirect(302, to);
} Thing is that I use my Is this way of routing in a parent layout file recommended in general?
Yes it is called when the user clicks the login button Login functionasync function submit() {
isLoading = true;
const response: { authStatus: AuthStatus } | { error: string; message: string } =
await post(`auth/login`, {
email,
password,
userProfileId: data.user?.userProfile.at(0)?.id,
});
await invalidate();
// TODO handle network errors
if ('error' in response) errors = [response.message];
isLoading = false;
if ('authStatus' in response) {
if (response.authStatus === 'COMPLETE') {
setBearerForApi(data.bearerToken);
instagramConfig.fetch();
storeConfig.sync(false);
goto('/app/dashboard');
} else if (response.authStatus === 'ENTER_INFO') {
goto('/auth/register/info');
} else if (response.authStatus === 'SELECT_PROFILE') {
goto('/auth/login/select-profile');
}
}
} |
It looks like +layout.server.ts is still being called every time if the page inside the layout has a catch-all route. I have: Every time I client-side navigate to /financials/ or a subpage like /financials/balance-sheet/ or /financials/balance-sheet/quarterly/, it calls the load function in the +layout.server.ts. I tried to create a reproduction in StackBlitz but it's just throwing an error "node.component is not a function". |
How does the |
maybe related: #6349 |
The +page.server.ts is supposed to run every time when the route changes. For example, going from /stocks/aapl/financials/ to /stocks/aapl/financials/balance-sheet/. But I would prefer to re-use the response from the parent +layout.server.ts. The +page.server.ts does have an await parent() inside because I need to access info from the parent layout, which has information about the stock. I can see now that another page that is not a catch-all route but has an await parent() also calls the load function in the +layout.server.ts file every time. So I guess this does not happen in catch-all routes specifically, rather just any page that has an await parent(). Is the await parent() supposed to make the load function run again? In my case it is redundant because I am navigating from a page that already caused the layout load function to run and the data is the same. But if this is expected behavior then I can try to find a way to work around it. Here is the code in the catch-all route's +page.server.ts:
|
#5960. Just a failing test for now (have to run, will implement later if time allows).Basic strategy:
load_server_data
, what gets used (url
,params.foo
,parent
)data
__data.json
, specify (probably in a custom header) which nodes have been invalidated (i.e. if a node usesurl
, it will always be invalid if the URL has changed; if it usesparams.foo
, it is only invalid ifparams.foo
has changed; if it usesparent
then it will be invalid if any parent is invalid)-1
for everything that remains valid. If an invalid node unexpectedly callsawait parent
, run everything upstream as well-1
values from memoryPlease don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. All changesets should bepatch
until SvelteKit 1.0