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
feat: add preload function #2026
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 85f160c:
|
I think we can try building this into the default |
@shuding Thank you! That makes sense, I'll try it 👍 |
I've renamed the API from |
_internal/utils/preload.ts
Outdated
const fetcher = | ||
fetcher_ == null | ||
? null | ||
: // fetcher might be a sync function, so this should not be an async function |
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.
If this becomes an async function, the following test is failed.
swr/test/use-swr-error.test.tsx
Line 433 in 9263ca7
it('should reset isValidating when an error occured synchronously', async () => { |
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.
Makes sense!
I've added a test for resetting an error when preloading has been failed. |
_internal/utils/preload.ts
Outdated
import { Middleware, Key, BareFetcher } from '../types' | ||
import { serialize } from './serialize' | ||
|
||
const REQUEST = new Map<string, Promise<any>>() |
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.
should this be bound with cache in global state?
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.
@huozhi Thank you! That makes sense to me.
But does the preload function have to accept cache
as the argument to use the global state? (It might work if we always use a global cache to store the value)
// we need the cache argument to get REQUEST from SWRGlobalState
preload(key, fetcher, cache)
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.
I think it's OK to make it always global and apply to any cache layers for now (just like fallback
) considering the major use cases of preload
. If necessary we can add an optional cache
parameter in the future, that wouldn't be breaking.
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.
I've used SWRGlobalState
to store preloading requests.
85f160c
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.
Thanks for this API. I wanted to confirm, does it use custom cache if provider
config is set?
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.
Looks good on my side
Looks amazing, thank you! |
Thank you! |
Great..!! It looks amazing, best wishes! |
This is an experimental implementation to preload fetcher functions. The following is an example.
The preload function is necessary to avoid waterfall problems with the suspense mode.
Currently, this is implemented as middleware, but the API might be changed before merging.
The limitation of the current implementation is that we have to useuseSWR
fromswr/preload
.This is added as a built-in middleware
The following has the waterfall problem.
To avoid that, we should preload fetcher functions.
preload
is the function for it.