Loading...
Loading...
ALWAYS use when working with Angular SSR, server-side rendering, hydration, prerendering, or Angular Universal in Angular applications.
npx skill4agent add oguzhan18/angular-ecosystem-skills angular-ssrprovideClientHydrationngSkipHydrationng add @angular/ssr
# Or create new project with SSR
ng new my-app --ssrimport { provideClientHydration } from '@angular/platform-browser';
export const appConfig: ApplicationConfig = {
providers: [
provideClientHydration()
]
};import { provideClientHydration, withIncrementalHydration } from '@angular/platform-browser';
export const appConfig: ApplicationConfig = {
providers: [
provideClientHydration(withIncrementalHydration())
]
};@Component({
template: `
@defer (on viewport) {
<heavy-component />
} @placeholder {
<div>Loading...</div>
}
`
})
export class MainComponent {}import { PLATFORM_ID, Inject } from '@angular/core';
constructor(@Inject(PLATFORM_ID) private platformId: Object) {
if (isPlatformBrowser(this.platformId)) {
// Browser-only code
}
}import { TransferState, makeStateKey } from '@angular/platform-browser';
@Injectable()
export class DataService {
constructor(private http: HttpClient, private transferState: TransferState) {}
getData() {
const DATA_KEY = makeStateKey('DATA');
if (this.transferState.hasKey(DATA_KEY)) {
return of(this.transferState.get(DATA_KEY, []));
}
return this.http.get('/api/data').pipe(
tap(data => this.transferState.set(DATA_KEY, data))
);
}
}@Component({
selector: 'app-third-party',
host: {
'ngSkipHydration': 'true'
}
})
export class ThirdPartyComponent {}import { RenderMode, ServerRoute } from '@angular/ssr';
export const serverRoutes: ServerRoute[] = [
{
path: 'dashboard',
renderMode: RenderMode.Client
},
{
path: 'blog/:slug',
renderMode: RenderMode.Server
},
{
path: 'about',
renderMode: RenderMode.Prerender
}
];import { isPlatformServer } from '@angular/common';
constructor(@Inject(PLATFORM_ID) platformId: Object) {
if (isPlatformServer(platformId)) {
// Server-side only
}
}