Angular4 Universal Server Side Rendering would return more HTML rendered on request

1.5k Views Asked by At

i'm tryingo to use universal and angular4.. everything works, accessing directly via hash routes as well.. but the problem is the request always shows <body><demo-app></demo-app></body>

I expect to have some html tags and info inside <demo-app></demo-app>

Here is how I implemented that:

@NgModule({
  bootstrap: [AppComponent],
  imports: [
    BrowserModule.withServerTransition({
      appId: 'my-app-id'
    }),
    ServerModule,
    ServerTransferStateModule,
    AppModule
  ]
})

export class ServerAppModule {

  constructor(private transferState: TransferState,
  private translateService: TranslateService){
    translateService.use('en');
  }
  // Gotcha
  ngOnBootstrap = () => {
    this.transferState.inject();
  }
}


app.engine('html', ngExpressEngine({
  bootstrap: ServerAppModule
}));

app.set('view engine', 'html');
app.set('views', 'src');


app.get("*", (req, res) => {
  console.time(`GET: ${req.originalUrl}`);
  res.render('../dist/index', {
    req: req,
    res: res
  });
  console.timeEnd(`GET: ${req.originalUrl}`);
});

My versions are "@angular/common": "^4.0.0"

Tested using https://github.com/angular/universal/blob/master/modules/ng-express-engine/src/main.ts and import { ngExpressEngine } from '@nglibs/universal-express-engine'

Following this discussion i could see that is a new implementation for that

Tryed to import as it suggest, but does not work (namespace does not exists)

  • import { ngExpressEngine } from '@universal/ng-express-engine'

Tested with version rc.5 as suggested in this pull request.

Finally , following this link

https://github.com/angular/universal/tree/master/modules/ng-express-engine

Tested with this lib gives me same results:

import { ngExpressEngine } from '@nguniversal/express-engine';

At least the project is running, so i can keep coding and using it.. but looking forward to have a full server side rendering.

Thanks!

2

There are 2 best solutions below

0
mariomol On BEST ANSWER

Thanks @mark-pieszak for you attention and support!

I could make it work using the https://github.com/clbond/angular-ssr , working with angular-material and everything.. pretty good =)

1
Mark Pieszak - Trilon.io On

For Angular 4+ with platform-server, use the official @nguniversal/express-engine, that other one looks like an older version of the express-engine that someone published for some reason?

The namespace is now going to be @nguniversal though so give that one a shot and you should be fine!

You can find documentation on the newest universal express-engine (for Angular 4+ and platform-server) here:

https://github.com/angular/universal/tree/master/modules/ng-express-engine