Skip to content
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

fix: cannot force send request in restore cache mode #627

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
Loading