Skip to content

spike: [WIP] Modernize Spartacus SSR#21135

Draft
pawelfras wants to merge 11 commits intorelease/221121.7.xfrom
spike/modernize-ssr
Draft

spike: [WIP] Modernize Spartacus SSR#21135
pawelfras wants to merge 11 commits intorelease/221121.7.xfrom
spike/modernize-ssr

Conversation

@pawelfras
Copy link
Contributor

@pawelfras pawelfras commented Feb 9, 2026

Please refer to PR_DESCRIPTION.md

Phase 2 implementation for modern SSR error handling:

- Add CxRequestContext interface with error() and logger properties
- Use cx namespace in REQUEST_CONTEXT to avoid customer property collisions
- Update CxAngularNodeAppEngine to pass cx context with error handler and logger
- Update PropagatingToServerErrorHandler to use REQUEST_CONTEXT.cx.error
  with fallback to legacy PROPAGATE_ERROR_TO_SERVER token
- Update ExpressLoggerService to use REQUEST_CONTEXT.cx.logger
  with fallback to legacy EXPRESS_SERVER_LOGGER token
- Fix instanceof issue for CmsPageNotFoundOutboundHttpError by using
  error.name property check instead of instanceof
- Add explicit name property to OutboundHttpError classes
- Update server.ts to create logger, pass to engine, enable error handlers
- Add @deprecated notices to legacy tokens
- Export new types from public_api

Error flow:
1. Angular app error -> CxErrorHandler -> PropagatingToServerErrorHandler
2. REQUEST_CONTEXT.cx.error() captures error
3. CxAngularNodeAppEngine.handle() throws after render completes
4. Express error middleware sends CSR fallback with 404/500 status
Add ModernEngineAdapter to bridge Promise-based AngularNodeAppEngine with
callback-based OptimizedSsrEngine, enabling all SSR optimization features
(timeout, caching, concurrency control, render reuse) in the modern engine.

Changes:
- Create ModernEngineAdapter that adapts modern engine to legacy callback API
- Extend CxAngularNodeAppEngine to support optimization mode via options
- Update server.ts to enable SSR optimization with env-configurable settings
- Export ModernEngineAdapter from public API
- Disable PWA addToHomeScreen in storefrontapp config
Implement resilient handling of SERVER_REQUEST_ORIGIN resolution to prevent
build failures during Angular 19+ route extraction when the backend is not
accessible.

Changes:
- Add fallback origin (localhost:4200) for build-time route extraction
- Support SERVER_REQUEST_ORIGIN environment variable as resolution option
- Update resolution order: explicit option > injector token > env var > fallback
- Add timeout and error handling in SiteContextConfigInitializer for SSR
- Handle LOAD_BASE_SITES_FAIL action to prevent getAll() from hanging
- Replace fatal errors with warnings during SSR/build in config initializers
- Update documentation to clarify resolution order and build-time behavior

This allows Angular CLI builds to complete without explicit origin configuration
while maintaining full functionality during actual SSR and prerendering.
…modern SSR

The modern AngularNodeAppEngine provides Angular's REQUEST token with a Web
standard Request object (not IncomingMessage or Express Request). This broke
TestConfigServerModule which was trying to read cookies using patterns that
only work for Express Request.

Changes:
- TestConfigServerModule: Update getCookieHeader() to handle three request types:
  - Express Request (has get() method)
  - Web standard Request (has Headers instance with get() method)
  - Raw IncomingMessage (has plain headers object)
- TestConfigServerModule: Add provideConfigFactory() and provideFeatureTogglesFactory()
  to actually inject test config into Spartacus config system (was only providing
  TEST_CONFIG token but not integrating it)
- ExpressLoggerService: Support both legacy Spartacus REQUEST token and Angular's
  REQUEST token for request context
- serverLoggerServiceFactory: Check both Angular's and Spartacus's REQUEST tokens
  to determine if running in Express context
- ssr-testing.spec.ts: Update REQUEST_PATH to include full site context path

Technical details:
Angular's AngularNodeAppEngine.handle() converts Node.js request to Web Request
internally via createWebRequestFromNodeRequest(). The Web Request has a Headers
instance where cookies must be read via headers.get('cookie') instead of
headers['cookie'].

All 7 SSR tests now pass.
@pawelfras pawelfras requested a review from a team as a code owner February 9, 2026 22:32
The storefrontapp now exclusively uses the modern server.ts with
CxAngularNodeAppEngine. The legacy callback-based server-legacy.ts
(using NgExpressEngineDecorator and ngExpressEngine) is no longer needed.

Note: The legacy SSR engine APIs remain available in @spartacus/setup/ssr
for backward compatibility with existing customer applications.
Add @deprecated annotations to legacy SSR APIs that are replaced by
the modern CxAngularNodeAppEngine:

- REQUEST and RESPONSE tokens (use Angular's REQUEST/RESPONSE_INIT)
- NgExpressEngineInstance and NgExpressEngine types
- decorateExpressEngine function
- NgSetupOptions type and RenderOptions interface
- ngExpressEngine function

All deprecation notices point users to CxAngularNodeAppEngine as the
modern alternative. The legacy APIs remain functional for backward
compatibility with existing customer applications.
Improve code quality across SSR modules:

- PropagatingToServerErrorHandler: Simplify error handler resolution
  and use nullish coalescing for cleaner propagation logic
- ExpressLoggerService: Extract request/logger resolution into private
  methods, make fields private, add spacing between methods
- serverLoggerServiceFactory: Add JSDoc documentation, simplify variable
  naming and request detection logic
- TestConfigServerModule: Add JSDoc comments to utility functions,
  simplify request resolution, fix unused catch parameter

No functional changes - all 7 SSR E2E tests pass.
…tion

Remove direct mode from CxAngularNodeAppEngine since all SSR renderings
should be optimized in production. The engine now:

- Requires documentFilePath (for CSR fallback)
- Always uses OptimizedSsrEngine internally
- Applies defaultSsrOptimizationOptions if optimization not provided

This simplifies the API and ensures consistent production-grade behavior.
@pawelfras pawelfras changed the title spike: Modernize Spartacus SSR spike: [WIP] Modernize Spartacus SSR Feb 9, 2026
@github-actions github-actions bot marked this pull request as draft February 10, 2026 11:49
@github-actions
Copy link
Contributor

🚨 PR Title Validation Failed 🚨

Your pull request title does not follow the required format. Please update it to match the expected pattern:

Expected format:
<type>: <subject>

Allowed Types

  • docs: Changes to documentation only
  • feat: New feature work
  • fix: Bug fixes
  • perf: Code improvements for performance
  • refactor: Code changes that are not bug fixes or features
  • style: Code style changes (e.g., whitespace, formatting)
  • test: Adding or updating tests
  • chore: Build, CI, scripts, configs, etc.

Example of a valid PR title

feat: Add user authentication
fix: Resolve checkout bug
docs: Update API documentation

Merge is blocked until the PR title is corrected.

@github-actions
Copy link
Contributor

🚨 PR Title Validation Failed 🚨

Your pull request title does not follow the required format. Please update it to match the expected pattern:

Expected format:
<type>: <subject>

Allowed Types

  • docs: Changes to documentation only
  • feat: New feature work
  • fix: Bug fixes
  • perf: Code improvements for performance
  • refactor: Code changes that are not bug fixes or features
  • style: Code style changes (e.g., whitespace, formatting)
  • test: Adding or updating tests
  • chore: Build, CI, scripts, configs, etc.

Example of a valid PR title

feat: Add user authentication
fix: Resolve checkout bug
docs: Update API documentation

Merge is blocked until the PR title is corrected.

1 similar comment
@github-actions
Copy link
Contributor

🚨 PR Title Validation Failed 🚨

Your pull request title does not follow the required format. Please update it to match the expected pattern:

Expected format:
<type>: <subject>

Allowed Types

  • docs: Changes to documentation only
  • feat: New feature work
  • fix: Bug fixes
  • perf: Code improvements for performance
  • refactor: Code changes that are not bug fixes or features
  • style: Code style changes (e.g., whitespace, formatting)
  • test: Adding or updating tests
  • chore: Build, CI, scripts, configs, etc.

Example of a valid PR title

feat: Add user authentication
fix: Resolve checkout bug
docs: Update API documentation

Merge is blocked until the PR title is corrected.

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.

1 participant