Files
kmtnflower/server.ts
T
Hermes Agent e95a158d57
CI Pipeline / backend-test (push) Successful in 9m4s
CI Pipeline / frontend-test (push) Successful in 24s
CI Pipeline / frontend-build (push) Successful in 32s
CI Pipeline / backend-lint (push) Successful in 14s
CI Pipeline / backend-test (pull_request) Successful in 9m17s
CI Pipeline / frontend-test (pull_request) Successful in 24s
CI Pipeline / frontend-build (pull_request) Successful in 30s
CI Pipeline / backend-lint (pull_request) Successful in 10s
CI Pipeline / quality-gate (push) Successful in 2s
CI Pipeline / quality-gate (pull_request) Successful in 2s
fix(ssr): apply theme CSS vars during SSR and enable client hydration
- ThemeService: remove isPlatformBrowser guard so CSS custom properties
  are applied during SSR/prerender — server-rendered HTML now carries the
  correct color scheme and gradients (fixes missing colors on test site).

- main.ts: add provideClientHydration() so Angular preserves the server-
  rendered DOM on the client instead of destroying and rebuilding it
  (fixes jarring full-page reload between static and dynamic versions).

- Tests: update theme.service.spec.ts to match new behaviour, expand
  coverage with gradient tests and empty-value handling (9 tests).
  Fix structured-data.component.spec.ts import paths. All 54 tests pass.
2026-08-06 03:24:57 +00:00

79 lines
2.4 KiB
TypeScript

import {
AngularNodeAppEngine,
createNodeRequestHandler,
isMainModule,
writeResponseToNodeResponse,
} from '@angular/ssr/node';
import * as http from 'node:http';
/**
* Public routes prerendered at build time (SSG — Static Site Generation).
* These five routes are compiled to static HTML during `ng build` and
* served directly from disk at runtime for optimal performance and SEO.
* The list here mirrors `prerender-routes.txt` and the Prerender entries
* in `src/app/app.server.routes.ts`.
*/
export const PRERENDERED_ROUTES: readonly string[] = [
'/',
'/about',
'/schedule',
'/events',
'/contact',
];
const ngApp = new AngularNodeAppEngine();
/**
* Normalise a request pathname and check against prerendered routes.
* Handles both bare paths (`/about`) and trailing-slash variants (`/about/`).
*/
export function isPrerenderedRoute(url: string): boolean {
const pathname = new URL(url, 'http://localhost').pathname;
const normalised = pathname.endsWith('/') && pathname !== '/'
? pathname.slice(0, -1)
: pathname;
return PRERENDERED_ROUTES.includes(normalised);
}
/**
* Handle incoming requests using the Angular SSR engine.
*
* Prerendered routes (/ , /about, /schedule, /events, /contact) are served
* as pre-generated static HTML from the build output. All other routes
* fall back to server-side rendering (SSR) or the fallback 404 handler.
*/
export default createNodeRequestHandler(async (req, res, next) => {
try {
const prerendered = isPrerenderedRoute(req.url || '/');
if (prerendered) {
// Prerendered routes are served as static HTML — AngularNodeAppEngine
// reads the build-time manifest and returns the pre-generated response.
console.log(`[SSR] Prerendered route: ${req.url}`);
}
const response = await ngApp.handle(req);
if (response) {
await writeResponseToNodeResponse(response, res);
} else {
next();
}
} catch (error) {
next(error);
}
});
if (isMainModule(import.meta.url)) {
const httpServer = http.createServer();
httpServer.on('request', async (req: http.IncomingMessage, res: http.ServerResponse) => {
const handler = (await import('./server.js')).default;
handler(req, res, () => {
res.statusCode = 404;
res.end('Not Found');
});
});
httpServer.listen(4200, () => {
console.log(`Node SSR server listening on http://localhost:4200`);
});
}