Skip to content

Commit 31c358e

Browse files
Add expiresAt to pending state if prefetch (#115)
Co-authored-by: Liam Ma <[email protected]>
1 parent c0f735c commit 31c358e

File tree

2 files changed

+36
-0
lines changed
  • src

2 files changed

+36
-0
lines changed

src/__tests__/unit/controllers/resource-store/test.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,36 @@ describe('resource store', () => {
903903
expect(spy).toBeCalledTimes(0);
904904
});
905905
});
906+
907+
it('should set expiresAt to pending state if prefetch', async () => {
908+
(getDefaultStateSlice as any).mockImplementation(() => ({
909+
...BASE_DEFAULT_STATE_SLICE,
910+
}));
911+
(getExpiresAt as any).mockImplementation(() => expiresAt);
912+
913+
const spy = jest.spyOn(storeState, 'setState');
914+
915+
await actions.getResourceFromRemote(
916+
mockResource,
917+
mockRouterStoreContext,
918+
{ ...mockOptions, prefetch: true }
919+
);
920+
921+
expect(spy).toHaveBeenNthCalledWith(1, {
922+
context: {},
923+
data: {
924+
[type]: {
925+
[key]: {
926+
...BASE_DEFAULT_STATE_SLICE,
927+
loading: true,
928+
accessedAt: undefined,
929+
expiresAt,
930+
promise: expect.any(Promise),
931+
},
932+
},
933+
},
934+
});
935+
});
906936
});
907937

908938
describe('cleanExpiredResources', () => {

src/controllers/resource-store/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ export const actions: Actions = {
127127
context
128128
),
129129
accessedAt: getAccessedAt(),
130+
// prevent resource from being cleaned prematurely and traigger more network requests.
131+
...(options.prefetch
132+
? {
133+
expiresAt: getExpiresAt(PREFETCH_MAX_AGE),
134+
}
135+
: {}),
130136
};
131137

132138
dispatch(setResourceState(type, key, pending));

0 commit comments

Comments
 (0)