#useTask waiting for routeLoader

48 messages · Page 1 of 1 (latest)

small hare
#

Hi!
We have some logic which I moved from routeLoader to useTask. We did this to avoid re-running it on each navigation from the user.

The issue that I have is that useTask will always be executed after all the routeLoaders (for initial page load).

Since both routeLoaders and useTask are database reads, this is pushing my response time by quite a bit.

Is there a way to solve this? Basically is there a way to execute some logic at the same time as routeLoaders but only on page load and not navigation?

Thank You in advance

drifting anvil
#

Mount your stuff in the layout

small hare
#

Can you please explain what this means? Would mean a world to me

drifting anvil
#

If you have a single layout component, mount your stuff in there.

#

So your stuff will be executed only on the page load

#

Also you can hook the window or the document.

small hare
#

@drifting anvil thanks for the quick answer, but I don't think it is actually what I need.
While multiple routeLoaders are asynchronous when reading from the database, everything that I would write inside the layout is waiting for routeLoaders to finish

#

Also, if I hook on widnow or document, the logic would be done on the client, which isn't somethign I want

drifting anvil
#

Well, if it's SSR what you want, so mount in the main layout.

small hare
#

What does it mean to "mount"

#

Where would I write this code

drifting anvil
#

Mount it's mostly an old school concept from react

#

In code, in react you would use useEffect or componentDidMount

#

In qwik your emulate something like that using useTask$

small hare
#

Okey, I am using useTask, but it is executed afte rall the routeLoaders finish, that's the whole issue

#

I want to basically send all the requests for the database at the same time

#

But some I want to be executed always (routeLoader) while others only on initial

drifting anvil
#

You don't really have to put your fetch inside a routerloader

small hare
#

So I have 2 fetch requests. One I want always, the second one only on initial

drifting anvil
#

If it's useful I'm currently fetching inside my useTask$

small hare
#

But on inital I want A and B to be done in parrallel

drifting anvil
#

Well, it's qwik, if it's not done in parallel so it's weird

small hare
#

it wouldn't make sense to do them in parallel (routeLoader and useTask). But, we need a way to say "Do this routeLoader only on initial"

#

I will make a feature request for this

drifting anvil
#

Note that you can create a route loader in the main layout

small hare
#

But it will execute on every navigation

drifting anvil
#

anyways, I don't think that feature makes any sense imo.

#

maybe I missunderstood, but do you want a feature which gets executed once in server initialization?

small hare
#

Yes but at the same time as routeLaoders. NOT after they finish 😄

#

This can save me 200-300ms on request

drifting anvil
#

well, as I told you, you should load all your stuff up in the useTask$ of your main layout, then have all the states populated.

#

not sure what are you doing wrong, but in my case, I have a user mount in my layout and it gets called once in the entire application mount, then I can freely navigate without getting my layout remounted.

small hare
#

For this to work, I would have to stop using routeLoaders everywhere in the app

drifting anvil
small hare
#

Nothing is getting blown up 😄 I just want to do some requests in parallel. It doesn't matter, there is a feature request for this already. Hopefully they will implement it someday 🙂

#

@drifting anvil thanks for trying to help ❤️

keen badge
#

@small hare so you can also put a useTask in the root layout and have it do a promise.all

#

Or even better do it in useResource.

small hare
#

Hi @keen badge thanks for joining.
I am doing it but the issue is that all the routeLoaders happen first, and after they resolve, useTask will begin with its logic.

Lets say that on some page I have routeLoaders that read from the database and it takes them about 300ms to finish.
Only after those will useTask trigger in the main layout. If it also takes 300ms to read from the database, my server response time is around 600ms instead of 300 (as it would be if bothr request could've started at the same time)

keen badge
#

Right, you wouldn't be using route loaders in this scenario

small hare
#

Yeah, I could move all the logic from all the routeLoaders into useTasks/useResource. But kinda defeats the purpose

keen badge
#

BTW I use SQLite, most of my queries are a couple ms

small hare
#

There are many reasons not to run routeLoader on every page navigation and/or routeAction

small hare
keen badge