Skip to content

Commit

Permalink
fix: cannot force send request in restore cache mode (#627)
Browse files Browse the repository at this point in the history
  • Loading branch information
MeetinaXD authored Jan 13, 2025
1 parent c0a77b9 commit 92e83a1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-suits-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'alova': patch
---

fix: cannot force send request in restore cache mode
4 changes: 3 additions & 1 deletion internal/mockServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DefaultBodyType, delay, http, HttpResponse, passthrough, StrictRequest
import { setupServer } from 'msw/node';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { randomId } from './testUtils';

// -------------------
// 服务模拟
Expand Down Expand Up @@ -94,7 +95,8 @@ const mockServer = setupServer(
requestHeaders: Object.fromEntries(request.headers.entries())
}
})
)
),
http.get(`${baseURL}/unit-test-random`, () => HttpResponse.json({ id: randomId() }))
);

export default mockServer;
2 changes: 2 additions & 0 deletions internal/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@ export const expectType = <T>(value: T) => {};
export const expectTrue = <T extends true>() => {};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const expectAssignableBy = <T, T2 extends T = T>(value: T2) => {};

export const randomId = () => Math.random().toString(36).slice(2);
2 changes: 1 addition & 1 deletion packages/alova/src/functions/sendRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default function sendRequest<AG extends AlovaGenerics>(methodInstance: Me
: getWithCacheAdapter(id, methodKey, l1Cache));

// If it is storage restore mode and there is no data in the cache, the persistent data needs to be restored to the cache, and the cached expiration time must be used.
if (cacheMode === STORAGE_RESTORE && !cachedResponse) {
if (cacheMode === STORAGE_RESTORE && !cachedResponse && !forceRequest) {
const rawL2CacheData = await getRawWithCacheAdapter(id, methodKey, l2Cache, tag);
if (rawL2CacheData) {
const [l2Response, l2ExpireMilliseconds] = rawL2CacheData;
Expand Down
20 changes: 20 additions & 0 deletions packages/alova/test/browser/behavior/l2Cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,24 @@ describe('l2cache cache data', () => {
await Get2;
expect(Get2.fromCache).toBeFalsy();
});

test('should ignore l1Cache and l2Cache when forceRequest is true', async () => {
const alova = getAlovaInstance({
responseExpect: r => r.json()
});
const Get = () =>
alova.Get('/unit-test-random', {
cacheFor: {
expire: 100 * 1000,
mode: 'restore'
}
});

const result1 = await Get();
const result2 = await Get().send(false);
const result3 = await Get().send(true);

expect(result1).toStrictEqual(result2);
expect(result1).not.toStrictEqual(result3);
});
});

0 comments on commit 92e83a1

Please sign in to comment.