angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Unable to pre-render static html if using firestore

Open musso opened this issue 6 years ago • 7 comments

Version info

Angular: 9.0.7

**Firebase:**7.14.5

**AngularFire:**6.0.0-rc.2

Other (e.g. Ionic/Cordova, Node, browser, operating system):

How to reproduce these conditions

Create a firebase project with In firebase > database > firestore Create a collection test and document id page1 with any dummy content

ng new demo --routing --style=less --minimal --skipGit=false && cd demo
ng add @angular/fire@next
npm i -S firebase @firebase/app
ng add @nguniversal/express-engine   

In environment.prod.ts & environment.ts

export const environment = {
  production: true,
  firebase: {
    apiKey: "XXXXXXXXXXXX",
    authDomain: "XXXXXXXXXXXX,
    databaseURL: "XXXXXXXXXXXX",
    projectId: "XXXXXXXXXXXX",
    storageBucket: "XXXXXXXXXXXX",
    messagingSenderId: "XXXXXXXXXXXX",
    appId: "XXXXXXXXXXXX"
  }
};

In app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

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

import { HttpClientModule } from '@angular/common/http';
import { AngularFireModule } from '@angular/fire';
import { AngularFirestoreModule } from '@angular/fire/firestore';
import { environment } from '../environments/environment';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule.withServerTransition({ appId: 'serverApp' }),
    HttpClientModule,
    AngularFireModule.initializeApp(environment.firebase),
    AngularFirestoreModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

In app.component.ts

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { AngularFirestore } from '@angular/fire/firestore';
import { map } from 'rxjs/operators'

@Component({
  selector: 'app-root',
  template: `
    {{ result | json }}
  `,
  styles: []
})
export class AppComponent implements OnInit{

  result

  constructor(
    private afs: AngularFirestore,
    private http: HttpClient
  ) { }

  ngOnInit(): void {
    this.getHttp() // option 1, this works

    // this.getFirestore() // option 2, this wont work
  }

  async getHttp() {
    this.result = await this.http.get("https://jsonplaceholder.typicode.com/posts/1").toPromise()
  }

  async getFirestore() {
    this.result = await this.afs.collection("test").doc("page1").get().pipe(
      map(action => ({id: action.id, ...action.data()}))
    ).toPromise()
  }

}

In angular.json

...
"server": {
...
"options": {
...
"externalDependencies": ["@firebase/firestore"]
}
}
...

Debug output

Using option 1, this.getHttp() and run npm run prerender Generated dist/demo/browser/index.html shows rendered data from the http call

<!DOCTYPE html><html lang="en"><head>
  <meta charset="utf-8">
  <title>Demo</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="styles.37a7c028028debe7e785.css"></head>
<body>
  <app-root ng-version="9.0.7"> {
  "userId": 1,
  "id": 1,
  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
} </app-root>
<script src="runtime-es2015.0811dcefd377500b5b1a.js" type="module"></script><script src="runtime-es5.0811dcefd377500b5b1a.js" nomodule="" defer=""></script><script src="polyfills-es5.03ba6fca1870326fbcc1.js" nomodule="" defer=""></script><script src="polyfills-es2015.870cff9c91eaf134b88a.js" type="module"></script><script src="vendor-es2015.3a620b5fb478a1b0f383.js" type="module"></script><script src="vendor-es5.3a620b5fb478a1b0f383.js" nomodule="" defer=""></script><script src="main-es2015.693e812b221d995acfb6.js" type="module"></script><script src="main-es5.693e812b221d995acfb6.js" nomodule="" defer=""></script>

</body></html><!-- This page was prerendered with Angular Universal -->

But if using option2, this.getFirestore() and run npm run prerender Generated dist/demo/browser/index.html shows non-rendered html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Demo</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="styles.37a7c028028debe7e785.css"></head>
<body>
  <app-root></app-root>
<script src="runtime-es2015.0811dcefd377500b5b1a.js" type="module"></script><script src="runtime-es5.0811dcefd377500b5b1a.js" nomodule defer></script><script src="polyfills-es5.03ba6fca1870326fbcc1.js" nomodule defer></script><script src="polyfills-es2015.870cff9c91eaf134b88a.js" type="module"></script><script src="vendor-es2015.3a620b5fb478a1b0f383.js" type="module"></script><script src="vendor-es5.3a620b5fb478a1b0f383.js" nomodule defer></script><script src="main-es2015.e904a69de25d8ab8281f.js" type="module"></script><script src="main-es5.e904a69de25d8ab8281f.js" nomodule defer></script></body>
</html>

Expected behavior

Generated dist/demo/browser/index.html should show rendered static HTML

Actual behavior

Generated dist/demo/browser/index.html shows non-rendered HTML

Others

Angular CLI: 9.0.7
Node: 13.5.0
OS: darwin x64

Angular: 9.0.7
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... platform-server, router
Ivy Workspace: Yes

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.900.7
@angular-devkit/build-angular     0.900.7
@angular-devkit/build-optimizer   0.900.7
@angular-devkit/build-webpack     0.900.7
@angular-devkit/core              9.0.7
@angular-devkit/schematics        9.0.7
@angular/fire                     6.0.0-rc.2
@ngtools/webpack                  9.0.7
@nguniversal/builders             9.1.1
@nguniversal/common               9.0.2
@nguniversal/express-engine       9.0.2
@schematics/angular               9.0.7
@schematics/update                0.900.7
rxjs                              6.5.5
typescript                        3.7.5
webpack                           4.41.2
    

musso avatar May 24 '20 17:05 musso

Also created issue on https://github.com/angular/universal/issues/1698

musso avatar May 24 '20 17:05 musso

According to https://github.com/angular/universal/issues/1698, it is the problem of angular fire. May I know if there is possible fix?

musso avatar Jun 14 '20 04:06 musso

https://github.com/angular/angularfire/issues/2420

musso avatar Jun 14 '20 14:06 musso

Hello angularFire team. I literally spend a ton of money in your app for my clients. At least answer us. It’s a shame that core features are broken. It’s a shame you don’t manage the PR. At least give us back the projets lead and we can manage it like a TRUE open source project.

BianchiSeb avatar Jul 05 '20 17:07 BianchiSeb

Zone.js issues should be addressed in recent patches, if you find APIs that are still broken please open issues.

jamesdaniels avatar Nov 18 '20 19:11 jamesdaniels

@jamesdaniels it is still the same under "@angular/common": "~11.0.9" "@angular/fire": "^6.1.4" "zone.js": "~0.10.2"

musso avatar Jan 27 '21 15:01 musso

@BianchiSeb Quick fix will be using Firestore REST API ...

Hello angularFire team. I literally spend a ton of money in your app for my clients. At least answer us. It’s a shame that core features are broken. It’s a shame you don’t manage the PR. At least give us back the projets lead and we can manage it like a TRUE open source project.

musso avatar Jan 28 '21 03:01 musso