Skip to content

Commit

Permalink
fix(server): use requestHandlerOptions.onError function (#6392)
Browse files Browse the repository at this point in the history
  • Loading branch information
targeral authored Oct 16, 2024
1 parent d6986c5 commit d5475d1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
37 changes: 21 additions & 16 deletions packages/server/core/src/plugins/render/ssrCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ import type {
RequestHandler,
RequestHandlerOptions,
} from '../../types/requestHandler';
import {
ErrorDigest,
createTransformStream,
getPathname,
onError,
} from '../../utils';
import { createTransformStream, getPathname } from '../../utils';

interface CacheStruct {
val: string;
Expand Down Expand Up @@ -46,6 +41,7 @@ async function processCache({
cacheStatus?: CacheStatus;
}) {
const response = await requestHandler(request, requestHandlerOptions);
const { onError } = requestHandlerOptions;

const decoder: TextDecoder = new TextDecoder();

Expand Down Expand Up @@ -73,12 +69,19 @@ async function processCache({
};

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

writer.close();
Expand Down Expand Up @@ -188,17 +191,19 @@ export async function getCacheResult(
requestHandler,
requestHandlerOptions,
} = options;
const { onError } = requestHandlerOptions;

const key = computedKey(request, cacheControl);

let value: string | undefined;
try {
value = await container.get(key);
} catch (_) {
onError(
ErrorDigest.ERENDER_CACHE,
`[render-cache] get cache failed, key: ${key}`,
);
if (onError) {
onError(`[render-cache] get cache failed, key: ${key}`);
} else {
console.error(`[render-cache] get cache failed, key: ${key}`);
}
value = undefined;
}
const { maxAge, staleWhileRevalidate } = cacheControl;
Expand Down
1 change: 0 additions & 1 deletion packages/server/core/src/utils/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ 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 d5475d1

Please sign in to comment.