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: cacheProvider support return false #5905

Merged
merged 2 commits into from
Jul 4, 2024
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
7 changes: 7 additions & 0 deletions .changeset/sour-cows-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/types': patch
'@modern-js/server-core': patch
---

fix: cacheProvider support return false
fix: cacheProvider 支持返回 false
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ In this, customKey is used for custom cache key. By default, Modern.js will use
```ts
export type CacheOptionProvider = (
req: IncomingMessage,
) => Promise<CacheControl> | CacheControl;
) => Promise<CacheControl | false> | CacheControl | false;
```

Sometimes developers need to customise the cache key through req, which can be handled using the function form, as shown in the following code:
Sometimes developers need to customise the cache key through req or when caching does not work for specific URLs, which can be handled using the function form, as shown in the following code:

```ts title="server/cache.ts"
import type { CacheOption, CacheOptionProvider } from '@modern-js/runtime/server';
Expand All @@ -66,6 +66,11 @@ const provider: CacheOptionProvider = (req) => {

const key = computedKey(url, headers, ...);

if(url.includes('no-cache=1')) {
return false;
}


return {
maxAge: 500, // ms
staleWhileRevalidate: 1000, // ms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ export interface CacheControl {
```ts
export type CacheOptionProvider = (
req: IncomingMessage,
) => Promise<CacheControl> | CacheControl;
) => Promise<CacheControl | false> | CacheControl | false;
```

有时开发者需要通过 req 来自定义缓存 key,可以配置为函数的形式进行处理, 例如以下代码:
有时开发者需要通过 req 来自定义缓存 key,或者特定 url 时缓存不生效,可以配置为函数的形式进行处理, 例如以下代码:

```ts title="server/cache.ts"

Expand All @@ -69,6 +69,10 @@ const provider: CacheOptionProvider = (req) => {

const key = computedKey(url, headers, ...);

if(url.includes('no-cache=1')) {
return false;
}

return {
maxAge: 500, // ms
staleWhileRevalidate: 1000, // ms
Expand Down
4 changes: 3 additions & 1 deletion packages/server/core/src/plugins/render/ssrCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,12 @@ function computedKey(req: Request, cacheControl: CacheControl): string {
}
}

type MaybeAsync<T> = Promise<T> | T;

export function matchCacheControl(
cacheOption?: CacheOption,
req?: IncomingMessage,
): CacheControl | Promise<CacheControl> | undefined {
): MaybeAsync<CacheControl | undefined | false> {
if (!cacheOption || !req) {
return undefined;
} else if (isCacheControl(cacheOption)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/types/server/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export type CacheControl = {

export type CacheOptionProvider = (
req: IncomingMessage,
) => Promise<CacheControl> | CacheControl;
) => Promise<CacheControl | false> | CacheControl | false;

export type CacheOption =
| false
Expand Down
12 changes: 9 additions & 3 deletions tests/integration/ssr/fixtures/base/server/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ class MyContainer implements Container {

export const customContainer: Container = new MyContainer();

export const cacheOption: CacheOption = {
maxAge: 50000,
staleWhileRevalidate: 10000,
export const cacheOption: CacheOption = req => {
if (req.url?.includes('no-cache=1')) {
return false;
} else {
return {
maxAge: 50000,
staleWhileRevalidate: 10000,
};
}
};
4 changes: 4 additions & 0 deletions tests/integration/ssr/tests/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ describe('Traditional SSR', () => {
});
const content1 = await page.content();
expect(content1).toMatch(result);

await page.goto(`http://localhost:${appPort}/?no-cache=1`);
const content2 = await page.content();
expect(content2).not.toMatch(result);
});

test('x-render-cache http header', async () => {
Expand Down
Loading