From 92e83a1e85a2a81fdaca945b18ea9ad4dcfe12e8 Mon Sep 17 00:00:00 2001 From: MeetinaXD <40497962+MeetinaXD@users.noreply.github.com> Date: Mon, 13 Jan 2025 14:34:42 +0800 Subject: [PATCH] fix: cannot force send request in restore cache mode (#627) --- .changeset/swift-suits-wait.md | 5 +++++ internal/mockServer.ts | 4 +++- internal/testUtils.ts | 2 ++ packages/alova/src/functions/sendRequest.ts | 2 +- .../test/browser/behavior/l2Cache.spec.ts | 20 +++++++++++++++++++ 5 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 .changeset/swift-suits-wait.md diff --git a/.changeset/swift-suits-wait.md b/.changeset/swift-suits-wait.md new file mode 100644 index 00000000..1e7f6227 --- /dev/null +++ b/.changeset/swift-suits-wait.md @@ -0,0 +1,5 @@ +--- +'alova': patch +--- + +fix: cannot force send request in restore cache mode diff --git a/internal/mockServer.ts b/internal/mockServer.ts index 929bce12..8a200332 100644 --- a/internal/mockServer.ts +++ b/internal/mockServer.ts @@ -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'; // ------------------- // 服务模拟 @@ -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; diff --git a/internal/testUtils.ts b/internal/testUtils.ts index af5b92cb..38705fe1 100644 --- a/internal/testUtils.ts +++ b/internal/testUtils.ts @@ -56,3 +56,5 @@ export const expectType = (value: T) => {}; export const expectTrue = () => {}; // eslint-disable-next-line @typescript-eslint/no-unused-vars export const expectAssignableBy = (value: T2) => {}; + +export const randomId = () => Math.random().toString(36).slice(2); diff --git a/packages/alova/src/functions/sendRequest.ts b/packages/alova/src/functions/sendRequest.ts index 53227154..317e7907 100644 --- a/packages/alova/src/functions/sendRequest.ts +++ b/packages/alova/src/functions/sendRequest.ts @@ -74,7 +74,7 @@ export default function sendRequest(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; diff --git a/packages/alova/test/browser/behavior/l2Cache.spec.ts b/packages/alova/test/browser/behavior/l2Cache.spec.ts index 692dc814..155fe041 100644 --- a/packages/alova/test/browser/behavior/l2Cache.spec.ts +++ b/packages/alova/test/browser/behavior/l2Cache.spec.ts @@ -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); + }); });