Skip to content

Commit

Permalink
Merge branch 'main' into feat/op-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin authored Oct 16, 2024
2 parents 8994fd0 + 518b783 commit 5b404e2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/famous-balloons-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/server-core': patch
---

feat(server): add catch logic for ssrCache container operation
feat(server): 为 ssr 缓存容器操作添加错误捕获逻辑
29 changes: 26 additions & 3 deletions packages/server/core/src/plugins/render/ssrCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import type {
RequestHandler,
RequestHandlerOptions,
} from '../../types/requestHandler';
import { createTransformStream, getPathname } from '../../utils';
import {
ErrorDigest,
createTransformStream,
getPathname,
onError,
} from '../../utils';

interface CacheStruct {
val: string;
Expand Down Expand Up @@ -66,7 +71,16 @@ async function processCache({
val: html,
cursor: current,
};
container.set(key, JSON.stringify(cache), { ttl });

container.set(key, JSON.stringify(cache), { ttl }).catch(() => {
onError(
ErrorDigest.ERENDER_CACHE,
`[render-cache] set cache failed, key: ${key}, value: ${JSON.stringify(
cache,
)}`,
);
});

writer.close();
return;
}
Expand Down Expand Up @@ -177,7 +191,16 @@ export async function getCacheResult(

const key = computedKey(request, cacheControl);

const value = await container.get(key);
let value: string | undefined;
try {
value = await container.get(key);
} catch (_) {
onError(
ErrorDigest.ERENDER_CACHE,
`[render-cache] get cache failed, key: ${key}`,
);
value = undefined;
}
const { maxAge, staleWhileRevalidate } = cacheControl;
const ttl = maxAge + staleWhileRevalidate;

Expand Down
1 change: 1 addition & 0 deletions packages/server/core/src/utils/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export enum ErrorDigest {
ENOTF = 'Page could not be found',
EINTER = 'Internal server error',
ERENDER = 'SSR render failed',
ERENDER_CACHE = 'SSR render cache failed',
// INIT: 'Server init error',
// WARMUP: 'SSR warmup failed',
// EMICROINJ: 'Get micro-frontend info failed',
Expand Down

0 comments on commit 5b404e2

Please sign in to comment.