Skip to content

Commit

Permalink
fix: ensure all possible SsrOptimizationOptions have explicitly defin…
Browse files Browse the repository at this point in the history
…ed default values (#19372)

Thanks to this, all the options (with their explicit default values) will printed in the logs on the start of SSR, which should improve developers' confidence while troubleshooting.

closes https://jira.tools.sap/browse/CXSPA-7482
  • Loading branch information
Platonn authored Oct 10, 2024
1 parent 901ddd4 commit 6eb3258
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 38 deletions.
66 changes: 38 additions & 28 deletions core-libs/setup/ssr/optimized-engine/optimized-ssr-engine.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ describe('OptimizedSsrEngine', () => {
context: {
timestamp: '2023-01-01T00:00:00.000Z',
options: {
cache: false,
cacheSize: 3000,
ttl: undefined,
concurrency: 10,
timeout: 50,
forcedSsrTimeout: 60000,
Expand All @@ -198,6 +200,9 @@ describe('OptimizedSsrEngine', () => {
logger: 'DefaultExpressServerLogger',
shouldCacheRenderingResult: '({ options, entry }) => !(options.ssrFeatureToggles?.avoidCachingErrors === true &&\\n' +
' Boolean(entry.err))',
renderKeyResolver: 'function getRequestUrl(req) {\\n' +
' return (0, express_request_origin_1.getRequestOrigin)(req) + req.originalUrl;\\n' +
'}',
ssrFeatureToggles: { avoidCachingErrors: false }
}
}
Expand Down Expand Up @@ -1461,34 +1466,39 @@ describe('OptimizedSsrEngine', () => {
logger: new MockExpressServerLogger() as ExpressServerLogger,
});
expect(consoleLogSpy.mock.lastCall).toMatchInlineSnapshot(`
[
"[spartacus] SSR optimization engine initialized",
{
"options": {
"cacheSize": 3000,
"concurrency": 10,
"forcedSsrTimeout": 60000,
"logger": "MockExpressServerLogger",
"maxRenderTime": 300000,
"renderingStrategyResolver": "(request) => {
if (hasExcludedUrl(request, defaultAlwaysCsrOptions.excludedUrls)) {
return ssr_optimization_options_1.RenderingStrategy.ALWAYS_CSR;
}
return shouldFallbackToCsr(request, options)
? ssr_optimization_options_1.RenderingStrategy.ALWAYS_CSR
: ssr_optimization_options_1.RenderingStrategy.DEFAULT;
}",
"reuseCurrentRendering": true,
"shouldCacheRenderingResult": "({ options, entry }) => !(options.ssrFeatureToggles?.avoidCachingErrors === true &&
Boolean(entry.err))",
"ssrFeatureToggles": {
"avoidCachingErrors": false,
},
"timeout": 3000,
},
},
]
`);
[
"[spartacus] SSR optimization engine initialized",
{
"options": {
"cache": false,
"cacheSize": 3000,
"concurrency": 10,
"forcedSsrTimeout": 60000,
"logger": "MockExpressServerLogger",
"maxRenderTime": 300000,
"renderKeyResolver": "function getRequestUrl(req) {
return (0, express_request_origin_1.getRequestOrigin)(req) + req.originalUrl;
}",
"renderingStrategyResolver": "(request) => {
if (hasExcludedUrl(request, defaultAlwaysCsrOptions.excludedUrls)) {
return ssr_optimization_options_1.RenderingStrategy.ALWAYS_CSR;
}
return shouldFallbackToCsr(request, options)
? ssr_optimization_options_1.RenderingStrategy.ALWAYS_CSR
: ssr_optimization_options_1.RenderingStrategy.DEFAULT;
}",
"reuseCurrentRendering": true,
"shouldCacheRenderingResult": "({ options, entry }) => !(options.ssrFeatureToggles?.avoidCachingErrors === true &&
Boolean(entry.err))",
"ssrFeatureToggles": {
"avoidCachingErrors": false,
},
"timeout": 3000,
"ttl": undefined,
},
},
]
`);
});
});
});
7 changes: 1 addition & 6 deletions core-libs/setup/ssr/optimized-engine/optimized-ssr-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import { Request, Response } from 'express';
import * as fs from 'fs';
import { NgExpressEngineInstance } from '../engine-decorator/ng-express-engine-decorator';
import { getRequestUrl } from '../express-utils/express-request-url';
import {
EXPRESS_SERVER_LOGGER,
ExpressServerLogger,
Expand All @@ -21,13 +20,9 @@ import {
RenderingStrategy,
SsrOptimizationOptions,
defaultSsrOptimizationOptions,
getDefaultRenderKey,
} from './ssr-optimization-options';

/**
* Returns the full url for the given SSR Request.
*/
export const getDefaultRenderKey = getRequestUrl;

export type SsrCallbackFn = (
/**
* Error that might've occurred while rendering.
Expand Down
34 changes: 30 additions & 4 deletions core-libs/setup/ssr/optimized-engine/ssr-optimization-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { Request } from 'express';
import { getRequestUrl } from '../express-utils/express-request-url';
import { DefaultExpressServerLogger, ExpressServerLogger } from '../logger';
import { RenderingEntry } from './rendering-cache.model';
import { defaultRenderingStrategyResolver } from './rendering-strategy-resolver';
Expand Down Expand Up @@ -48,7 +49,7 @@ export interface SsrOptimizationOptions {
* Time in milliseconds after prerendered page is becoming stale and should
* be rendered again.
*/
ttl?: number;
ttl?: number | undefined;

/**
* Allows overriding default key generator for custom differentiating
Expand Down Expand Up @@ -188,10 +189,34 @@ type DeepRequired<T> = {
[P in keyof T]-?: DeepRequired<T[P]>;
};

export const defaultSsrOptimizationOptions: SsrOptimizationOptions &
// To not forget adding default values, when adding new feature toggles in the type in the future
DeepRequired<Pick<SsrOptimizationOptions, 'ssrFeatureToggles'>> = {
/**
* Returns the full url for the given SSR Request.
*/
export const getDefaultRenderKey = getRequestUrl;

/**
* The type of `defaultSsrOptimizationOptions` ensures that all properties are set to a default value.
* Thanks to this, all options are well-defined and can be printed to logs on the SSR server start.
*/
type DefaultSsrOptimizationOptions = Omit<
Required<SsrOptimizationOptions>,
| 'debug' // debug is deprecated and not used anymore
| 'ttl' // ttl is required but its default value is `undefined`
> & {
ttl: number | undefined; // needed, otherwise we could not set the value `ttl: undefined` value (due to the Required<...>)
} & DeepRequired<
// all nested properties of `ssrFeatureToggles` are required too
Pick<
SsrOptimizationOptions,
//
'ssrFeatureToggles'
>
>;

export const defaultSsrOptimizationOptions: DefaultSsrOptimizationOptions = {
cache: false,
cacheSize: 3000,
ttl: undefined,
concurrency: 10,
timeout: 3_000,
forcedSsrTimeout: 60_000,
Expand All @@ -206,6 +231,7 @@ export const defaultSsrOptimizationOptions: SsrOptimizationOptions &
options.ssrFeatureToggles?.avoidCachingErrors === true &&
Boolean(entry.err)
),
renderKeyResolver: getDefaultRenderKey,
ssrFeatureToggles: {
avoidCachingErrors: false,
},
Expand Down

0 comments on commit 6eb3258

Please sign in to comment.