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

feat: CXSPA-6890 Create toggle and optimization options for propagating errors to the server #19021

Merged
merged 12 commits into from
Jul 15, 2024

Conversation

pawelfras
Copy link
Contributor

@pawelfras pawelfras commented Jul 2, 2024

PR contains toggle for propagating async errors captured during SSR to the server. Apart from that, it contains additional optimization options to configure the rendering cache - required as we're going to handle errors, which we discourage caching.

QA steps for propagateErrorsToServer toggle:

  1. Tests in storefront app:
  • call HTTP error that may be caught during server-side rendering e.g use HTTP proxy OR
  • modify pages property in default-cms-config.ts to generate 'CmsPageNotFound` error.
  • run npm run dev:ssr
  • open the application in the browser
  • verify whether the app fell back to CSR and index.html returned with status 404
  • stop the app
  • go to spartacus-features.module.ts and inside provideFeatureTogglesFactory set propagateErrorsToServer to false
  • run app and verify whether the rendered with status 200 (as it used to be before changes around ssr error handling)
  1. Tests in customer's app:
  • build and deploy Spartacus packages locally:
    npx ts-node ./tools/schematics/testing.ts in the SPA root folder
  • generate fresh ng17 application:
    npx @angular/cli@17 new my-app --standalone=false --ssr=false --style=scss --routing=false
  • go to the project's directory and install Spartacus with SSR from local packages:
    npx @angular/cli@17 add @spartacus/schematics@latest --ssr
  • use HTTP proxy or modify config for CMS endpoints to cause an error when rendering:
provideConfig(<OccConfig>{
      backend: {
        occ: {
          endpoints: {
            pages: 'cms/pages2', // FOR STATUS 404
          },
        },
      },
    }),
  • provide provideFeatureToggles({propagateErrorsToServer: true}) in app.module.ts`
  • in server.ts inside app function use handlers provided by Spartacus:
  const indexHtmlContent = readFileSync(indexHtml, 'utf-8');

  /** */

  server.use(defaultServerErrorResponseHandlers(indexHtmlContent));
  • run npm run build and npm run serve:ssr:my-app to verify whether the app fell back to CSR and index.html returned with status 404
  • close the app and remove provided toggle
  • run npm run build and npm run serve:ssr:my-app and verify whether the rendered with status 200 (as it used to be before changes around ssr error handling)

QA steps for new optimization options avoidCachingErrors and cacheStrategyResolver:

  • in server.ts, pass cache: true to the ssrOptions
  • modify components property in default-cms-config.ts to generate async error in SSR.
  • run npm run dev:ssr and run app in the browser
  • verify server logs whether request has been resolved with error and in the browser if the index.html returned with status 500:
{
  "message": "Request is resolved with the SSR rendering error (/electronics-spa/en/USD/)",
 / ... /
}
  • reload page and check server logs to verify if page has been taken from cache:
{
  "message": "Render from cache (/electronics-spa/en/USD/)",
  / ... /
}
  • in server.ts, add avoidCachingErrors: true to the ssrOptions
  • refresh the page a few times and ensure that log Render from cache (/electronics-spa/en/USD/) and rendering was triggered each time the page was reloaded
  • in server.ts, add any custom cacheStrategyResolver to ssrOptions, e.g:
cacheStrategyResolver: (options, entry) => {
    console.log('MY CUSTOM CACHE STRATEGY RESOLVER');
    return !(options.avoidCachingErrors === true && Boolean(entry.error));
 }
  • run app and verify if code from the custom cacheStrategyResolver was executed (If the above example was used, check server logs)

@pawelfras pawelfras marked this pull request as ready for review July 5, 2024 11:49
@pawelfras pawelfras requested a review from a team as a code owner July 5, 2024 11:49
core-libs/setup/ssr/optimized-engine/rendering-cache.ts Outdated Show resolved Hide resolved
* Caching strategy resolver that determines the behavior of caching in RenderingCache.
* By default, the caching strategy is based on the presence of an error.
*/
cacheStrategyResolver?: (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cacheStrategyResolver: (config, entry) => boolean

Maybe it's only me that see this name as very ambigious. On the first sight I don't know what is a "cacheStrategy", that it wants to resolve.

What about alternatives? Examples:

shouldCachePage: (config, entry) => boolean
cacheCondition: (config, entry) => boolean

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind changing this property name. What do you think about:

shouldCacheResponse: (config, entry) => boolean
shouldCacheRendering: (config, entry) => boolean
shouldCacheRenderingResult: (config, entry) => boolean

Copy link
Contributor

@Platonn Platonn Jul 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldCacheRenderingResult is most clear to me.

+to be future proof (for adding new properties/arguments), I'd wrap args in just a single object ({ config, entry }) => ...

@pawelfras pawelfras merged commit 51a859d into epic/ssr-error-handling Jul 15, 2024
11 checks passed
@pawelfras pawelfras deleted the feat/CXSPA-6890 branch July 15, 2024 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants