Skip to content

Commit e8e91d2

Browse files
Sam2303Matthew-Mallimo
authored andcommitted
feat: force initial fetch (#98)
1 parent ef4bcb5 commit e8e91d2

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -837,14 +837,15 @@ const { isLoading, data, error, run } = useFetchye(key, { defer: Boolean, mapOpt
837837
838838
**Options**
839839
840-
| name | type | required | description |
841-
|--------------------|-------------------------------------------------------|----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|
842-
| `mapOptionsToKey` | `(options: Options) => transformedOptions` | `false` | A function that maps options to the key that will become part of the cache key. See below for a list of mapOptionsToKey helpers. |
843-
| `mapKeyToCacheKey` | `(key: String, options: Options) => cacheKey: String` | `false` | A function that maps the key for use as the cacheKey allowing direct control of the cacheKey |
844-
| `defer` | `Boolean` | `false` | Prevents execution of `useFetchye` on each render in favor of using the returned `run` function. *Defaults to `false`* |
845-
| `initialData` | `Object` | `false` | Seeds the initial data on first render of `useFetchye` to accomodate server side rendering *Defaults to `undefined`* |
846-
| `headers` | `Object` or `() => Object` | `false` | `Object`: as per the ES6 Compatible `fetch` option. `() => Object`: A function to construct a ES6 Compatible `headers` object prior to any api call |
847-
| `...restOptions` | `ES6FetchOptions` | `true` | Contains any ES6 Compatible `fetch` option. (See [Fetch Options](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Supplying_request_options)) |
840+
| name | type | required | description |
841+
|---------------------|-------------------------------------------------------|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
842+
| `mapOptionsToKey` | `(options: Options) => transformedOptions` | `false` | A function that maps options to the key that will become part of the cache key. See below for a list of mapOptionsToKey helpers. |
843+
| `mapKeyToCacheKey` | `(key: String, options: Options) => cacheKey: String` | `false` | A function that maps the key for use as the cacheKey allowing direct control of the cacheKey |
844+
| `defer` | `Boolean` | `false` | Prevents execution of `useFetchye` on each render in favor of using the returned `run` function. *Defaults to `false`* |
845+
| `forceInitialFetch` | `Boolean` | `false` | Allows the consumer to ignore the cached value and instead always fetch from the server for the initial fetch and update the cached value with what returns. *Defaults to `false`* |
846+
| `initialData` | `Object` | `false` | Seeds the initial data on first render of `useFetchye` to accomodate server side rendering *Defaults to `undefined`* |
847+
| `headers` | `Object` or `() => Object` | `false` | `Object`: as per the ES6 Compatible `fetch` option. `() => Object`: A function to construct a ES6 Compatible `headers` object prior to any api call |
848+
| `...restOptions` | `ES6FetchOptions` | `true` | Contains any ES6 Compatible `fetch` option. (See [Fetch Options](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Supplying_request_options)) |
848849
849850
**Returns**
850851

packages/fetchye/__tests__/useFetchye.spec.jsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,27 @@ describe('useFetchye', () => {
354354
}
355355
`);
356356
});
357+
it('should ignore cache', async () => {
358+
let fetchyeRes;
359+
global.fetch = jest.fn(async () => ({ ...defaultPayload }));
360+
render(
361+
<AFetchyeProvider cache={cache}>
362+
{React.createElement(() => {
363+
const { isLoading } = useFetchye('http://example.com/one');
364+
if (isLoading === true) {
365+
return null;
366+
}
367+
return React.createElement(() => {
368+
fetchyeRes = useFetchye('http://example.com/one', { forceInitialFetch: true });
369+
return null;
370+
});
371+
})}
372+
</AFetchyeProvider>
373+
);
374+
await waitFor(() => fetchyeRes.isLoading === false);
375+
376+
expect(global.fetch.mock.calls).toHaveLength(2);
377+
});
357378
});
358379
});
359380

packages/fetchye/src/useFetchye.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ const useFetchye = (
5050
return;
5151
}
5252
const { loading, data, error } = selectorState.current;
53-
if (!loading && !data && !error) {
53+
// If first render and options.forceInitialFetch is true we want to fetch from server
54+
// on first render.
55+
if (
56+
(!loading && !data && !error)
57+
|| (numOfRenders.current === 1 && options.forceInitialFetch === true)
58+
) {
5459
runAsync({
5560
dispatch, computedKey, fetcher: selectedFetcher, fetchClient, options,
5661
});

0 commit comments

Comments
 (0)