Skip to content

Commit

Permalink
inject right performance api depending on platform
Browse files Browse the repository at this point in the history
  • Loading branch information
buggy1985 committed Jun 28, 2021
1 parent 735a9c3 commit 39aa084
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
10 changes: 9 additions & 1 deletion server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { APP_BASE_HREF } from '@angular/common';
import { existsSync } from 'fs';

import { performance } from 'perf_hooks';
import { PERFORMANCE_API } from 'src/app/app.module';

console.log('performance.now() is working in server.ts => ', performance.now());

Expand Down Expand Up @@ -36,7 +37,14 @@ export function app(): express.Express {

// All regular routes use the Universal engine
server.get('*', (req, res) => {
res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
res.render(indexHtml, {
req,
providers: [
{ provide: APP_BASE_HREF, useValue: req.baseUrl },
{ provide: PERFORMANCE_API, useValue: performance },

]
});
});

return server;
Expand Down
17 changes: 17 additions & 0 deletions src/app/app.browser.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { InjectionToken, NgModule } from '@angular/core';

import { AppModule } from './app.module';
import { AppComponent } from './app.component';
import { BrowserModule } from '@angular/platform-browser';
import { PERFORMANCE_API } from './app.module';

@NgModule({
imports: [
AppModule
],
providers: [
{ provide: PERFORMANCE_API, useValue: window.performance },
],
bootstrap: [AppComponent],
})
export class AppBrowserModule {}
13 changes: 8 additions & 5 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Component, OnInit } from '@angular/core';

// with the following line, it doesn't even compile
// import { performance } from 'perf_hooks';
import { Component, Inject, OnInit } from '@angular/core';
import { PERFORMANCE_API } from './app.module';


@Component({
Expand All @@ -12,9 +10,14 @@ import { Component, OnInit } from '@angular/core';
export class AppComponent implements OnInit {
title = 'angular-ssr-demo';

constructor(
@Inject(PERFORMANCE_API) private performance: any
) {}


ngOnInit() {
console.log('lets see if it works in app.component.ts too');
console.log('performance.now() is working here too => ', performance.now());
console.log('performance.now() is working here too => ', this.performance.now());
}

}
6 changes: 3 additions & 3 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { NgModule } from '@angular/core';
import { InjectionToken, NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

export const PERFORMANCE_API = new InjectionToken('performance-api');

@NgModule({
declarations: [
AppComponent
Expand All @@ -12,7 +14,5 @@ import { AppComponent } from './app.component';
BrowserModule.withServerTransition({ appId: 'serverApp' }),
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';
import { AppBrowserModule } from './app/app.browser.module';
import { environment } from './environments/environment';

if (environment.production) {
enableProdMode();
}

document.addEventListener('DOMContentLoaded', () => {
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err));
platformBrowserDynamic().bootstrapModule(AppBrowserModule)
.catch(err => console.error(err));
});

0 comments on commit 39aa084

Please sign in to comment.