From fff6422b5d3fb84f94e26e3bb92fb6054c84241d Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Thu, 4 Jul 2024 16:53:57 +0800 Subject: [PATCH 1/2] fix: cacheProvider support return false --- .changeset/sour-cows-visit.md | 7 +++++++ packages/server/core/src/plugins/render/ssrCache.ts | 4 +++- packages/toolkit/types/server/utils.d.ts | 2 +- tests/integration/ssr/fixtures/base/server/cache.ts | 12 +++++++++--- tests/integration/ssr/tests/base.test.ts | 4 ++++ 5 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 .changeset/sour-cows-visit.md diff --git a/.changeset/sour-cows-visit.md b/.changeset/sour-cows-visit.md new file mode 100644 index 000000000000..79ecba5228dd --- /dev/null +++ b/.changeset/sour-cows-visit.md @@ -0,0 +1,7 @@ +--- +'@modern-js/types': patch +'@modern-js/server-core': patch +--- + +fix: cacheProvider support return false +fix: cacheProvider 支持返回 false diff --git a/packages/server/core/src/plugins/render/ssrCache.ts b/packages/server/core/src/plugins/render/ssrCache.ts index 13e53989f3f2..677f41e1ae6a 100644 --- a/packages/server/core/src/plugins/render/ssrCache.ts +++ b/packages/server/core/src/plugins/render/ssrCache.ts @@ -125,10 +125,12 @@ function computedKey(req: Request, cacheControl: CacheControl): string { } } +type MaybeAsync = Promise | T; + export function matchCacheControl( cacheOption?: CacheOption, req?: IncomingMessage, -): CacheControl | Promise | undefined { +): MaybeAsync { if (!cacheOption || !req) { return undefined; } else if (isCacheControl(cacheOption)) { diff --git a/packages/toolkit/types/server/utils.d.ts b/packages/toolkit/types/server/utils.d.ts index 0e0275122cf4..1a49c6f4a368 100644 --- a/packages/toolkit/types/server/utils.d.ts +++ b/packages/toolkit/types/server/utils.d.ts @@ -103,7 +103,7 @@ export type CacheControl = { export type CacheOptionProvider = ( req: IncomingMessage, -) => Promise | CacheControl; +) => Promise | CacheControl | false; export type CacheOption = | false diff --git a/tests/integration/ssr/fixtures/base/server/cache.ts b/tests/integration/ssr/fixtures/base/server/cache.ts index c22fac8c5587..8422af2e00a2 100644 --- a/tests/integration/ssr/fixtures/base/server/cache.ts +++ b/tests/integration/ssr/fixtures/base/server/cache.ts @@ -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, + }; + } }; diff --git a/tests/integration/ssr/tests/base.test.ts b/tests/integration/ssr/tests/base.test.ts index 84cc2a169468..4acf2de6d540 100644 --- a/tests/integration/ssr/tests/base.test.ts +++ b/tests/integration/ssr/tests/base.test.ts @@ -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 () => { From c5886457d833559418fe3eeb3321b423543f8fcf Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Thu, 4 Jul 2024 17:07:31 +0800 Subject: [PATCH 2/2] docs: ssr cache --- .../docs/en/guides/advanced-features/ssr/cache.mdx | 9 +++++++-- .../docs/zh/guides/advanced-features/ssr/cache.mdx | 8 ++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/document/main-doc/docs/en/guides/advanced-features/ssr/cache.mdx b/packages/document/main-doc/docs/en/guides/advanced-features/ssr/cache.mdx index 578328b84c70..69de237fca83 100644 --- a/packages/document/main-doc/docs/en/guides/advanced-features/ssr/cache.mdx +++ b/packages/document/main-doc/docs/en/guides/advanced-features/ssr/cache.mdx @@ -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; +) => Promise | 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'; @@ -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 diff --git a/packages/document/main-doc/docs/zh/guides/advanced-features/ssr/cache.mdx b/packages/document/main-doc/docs/zh/guides/advanced-features/ssr/cache.mdx index 0513796b4fcb..c17990fb5d1b 100644 --- a/packages/document/main-doc/docs/zh/guides/advanced-features/ssr/cache.mdx +++ b/packages/document/main-doc/docs/zh/guides/advanced-features/ssr/cache.mdx @@ -55,10 +55,10 @@ export interface CacheControl { ```ts export type CacheOptionProvider = ( req: IncomingMessage, -) => Promise | CacheControl; +) => Promise | CacheControl | false; ``` -有时开发者需要通过 req 来自定义缓存 key,可以配置为函数的形式进行处理, 例如以下代码: +有时开发者需要通过 req 来自定义缓存 key,或者特定 url 时缓存不生效,可以配置为函数的形式进行处理, 例如以下代码: ```ts title="server/cache.ts" @@ -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