Files
kmtnflower/src/app/structured-data/structured-data.component.ts
T
robot a783626f34
CI Pipeline / backend-test (push) Successful in 10m24s
CI Pipeline / frontend-test (push) Failing after 19s
CI Pipeline / frontend-build (push) Failing after 23s
CI Pipeline / backend-lint (push) Successful in 12s
CI Pipeline / backend-test (pull_request) Successful in 10m45s
CI Pipeline / frontend-test (pull_request) Failing after 20s
CI Pipeline / frontend-build (pull_request) Failing after 12s
CI Pipeline / backend-lint (pull_request) Successful in 13s
CI Pipeline / quality-gate (push) Successful in 3s
CI Pipeline / quality-gate (pull_request) Successful in 3s
feat: add SSR and prerendering support
- Install @angular/ssr and @angular/platform-server (v21.2.x)
- Create server entry point (server.ts) and server bootstrap (main.server.ts)
- Add server routes config (app.server.routes.ts) with 5 prerendered routes:
  /, /about, /schedule, /events, /contact
- Update angular.json with server/ssr/prerender build configuration
- Add prerender-routes.txt for build-time static route generation
- Fix SSR compatibility:
  - App component: guard DOM access with isPlatformBrowser
  - StructuredDataComponent: skip head manipulation on server
  - ThemeService: skip CSS variable application on server
  - ShowService/EventService: return empty arrays on API failure
    (graceful fallback during prerender with no backend)
- Prerendered output verified: 5 static HTML routes in dist/
2026-08-05 20:22:43 +00:00

63 lines
2.1 KiB
TypeScript

import { Component, effect, inject, PLATFORM_ID, OnDestroy } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { StructuredDataService } from '../services/structured-data.service';
/**
* Renders JSON-LD structured data as `<script type="application/ld+json">` tags
* in the document <head>.
*
* Reads from StructuredDataService.structuredData() and keeps the head tags
* in sync on every route change or config update.
*
* Tags are identified by the data attribute `data="structured-data"` so they
* can be cleaned up on destroy without affecting other <head> scripts.
*/
@Component({
selector: 'app-structured-data',
standalone: true,
template: '', // renders nothing in the body — writes to <head>
})
export class StructuredDataComponent implements OnDestroy {
private service = inject(StructuredDataService);
private platformId = inject(PLATFORM_ID);
constructor() {
// Only manipulate DOM in the browser; skip on the server.
if (!isPlatformBrowser(this.platformId)) return;
effect(() => {
const entries = this.service.structuredData();
this.renderEntries(entries);
});
}
ngOnDestroy(): void {
if (!isPlatformBrowser(this.platformId)) return;
this.clearTags();
}
// ── Private ────────────────────────────────────────────────────
/** Write JSON-LD script tags to <head>, replacing any previous ones. */
private renderEntries(entries: unknown[]): void {
this.clearTags();
const head = document.head;
for (const entry of entries) {
const script = document.createElement('script');
script.type = 'application/ld+json';
script.setAttribute('data', 'structured-data');
script.textContent = JSON.stringify(entry);
head.appendChild(script);
}
}
/** Remove all previously injected structured-data script tags. */
private clearTags(): void {
const tags = document.querySelectorAll('script[data="structured-data"]');
for (const tag of tags) {
tag.remove();
}
}
}