Skip to content

Instantly share code, notes, and snippets.

@baunov
baunov / server.ts
Last active August 18, 2022 14:24
Detect crawling bots within Angular Universal. Serve SSR for bots and SPA for regular users.
import 'zone.js/dist/zone-node';
import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import { join } from 'path';
import { AppServerModule } from './src/main.server';
import { APP_BASE_HREF } from '@angular/common';
import { existsSync } from 'fs';
import * as isbot from 'isbot';
import { Inject, Injectable } from '@angular/core';
import {Meta, MetaDefinition, Title} from '@angular/platform-browser';
import {HOST_URL} from './tokens/host-url';
import { Router } from '@angular/router';
export interface PageMetadata {
title: string;
// image will be added later
description: string;
author: string;
import {InjectionToken} from '@angular/core';
export const HOST_URL = new InjectionToken<string>('HostUrl');
import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { AppModule } from './app.module';
import { AppComponent } from './app.component';
import { MetadataService } from './metadata.service';
@NgModule({
imports: [
AppModule,
import {Component, OnInit, Optional} from '@angular/core';
import { MetadataService } from '../../metadata.service';
@Component({
selector: 'app-page1',
templateUrl: './page1.component.html',
styleUrls: ['./page1.component.scss']
})
export class Page1Component implements OnInit {
import {
combineLatest,
distinctUntilChanged,
filter,
map,
Observable,
scan,
shareReplay,
switchMap,
withLatestFrom