angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

"Unexpected token import" with angularfire2 on angular-cli, AOT, SSR, and firebase-functions

Open sdebaun opened this issue 9 years ago • 40 comments

Has anyone done this? I'm stuck, would love some suggestions, or pointers if I'm missing something obvious or misunderstanding the problem.

Version info

Angular: 4.2.4

Firebase: 4.1.3

AngularFire: 4.0.0-rc1

Firebase-functions: 0.5.9

How to reproduce these conditions

Using AOT compiling, and server-side rendering, as described in this blagpost: https://medium.com/@evertonrobertoauler/angular-4-universal-app-with-angular-cli-db8b53bba07d

Repo: https://github.com/SparksNetwork/exp-angular-firebase/tree/shared-code (note I am working from the shared-code branch) (the repo structure is kind of haphazard, everything lives in /functions because thats what firebase-functions requires)

Steps to set up and reproduce

App runs successfully JIT running ng serve.

Compile client and server with ng run build.

Serve via standalone node express server with ts-node src/server.ts or via firebase-functions local emulator with firebase serve --only functions.

Debug output

FirebaseError: Error occurred while parsing your function triggers.

/Users/sdebaun/exp-angular-firebase/functions/node_modules/angularfire2/app/firebase.app.module.js:1
(function (exports, require, module, __filename, __dirname) { import { InjectionToken, } from '@angular/core';
                                                              ^^^^^^
SyntaxError: Unexpected token import
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/sdebaun/exp-angular-firebase/functions/fn/dist/dist/ngfactory/src/app/app.server.module.ngfactory.js:23:13)
i  functions: No HTTPS functions emulated. Support for other function types are coming soon.

Diagnosis

I suspect it's choking because the angularfire2 npm module is compiled with es2015 syntax which doesn't work in node (which firebase-functions is based on).

Possible fix?

I don’t know a lot about how NPM works, but I’m wondering if I:

  1. Fork angularfire2
  2. change the target and/or module settings in forked tsconfig until I find something that works
  3. in my app, npm install from my forked angularfire2

Only thing I don’t know how to do there is: how do I ensure that my forked angularfire2 gets built? There’s a bunch of stuff in the repo that has to do with the build process that is totally alien to me.

The only other thing I can think of is to try to write my own firebase wrapper for angular, ~~with blackjack and hookers~~ that compiles to commonjs. That sounds like an awful idea.

Things I’ve Tried

Using DI to swap out AngularFire2 on server

Per the discussion in this thread (https://github.com/angular/universal-starter/issues/127#issuecomment-287644140), I started writing a fake service that adhered to the angularfire2 interfaces that I could use in my app.server.module.ts. Quickly I realized that because all my services actually have to import from the real angularfire2 module, it still wouldn’t work, because the error gets thrown when the file is imported, not when it’s run.

Copying angularfire2/src into a local lib directory and building it with the project

When I try this, I get all sorts of errors about Zone not being found. I can’t figure out what needs to be included, or how, in order to get it to compile the ts.

sdebaun avatar Jul 16 '17 07:07 sdebaun

There's also no node.js entry to firebase/app which is a bug + why Websockets aren't getting pulled in.

(global as any).WebSocket = require("ws"); in your server.ts works until I get that patched upstream ;)

jamesdaniels avatar Nov 08 '17 21:11 jamesdaniels

so what should we do ? firebase is very important to me and without SSR to SEO, my angular 2 application is just a toys :(

hiepxanh avatar Nov 09 '17 00:11 hiepxanh

@hiepxanh I know its not elegant, nor does it cover all scenarios, but it gets me by until the official supports lands in this library: https://github.com/patrickmichalina/fusebox-angular-universal-starter/blob/master/src/server/server.angular-fire.service.ts

patrickmichalina avatar Nov 11 '17 01:11 patrickmichalina

We use angularfire2 and firestore in our application. Firestore does not render data as @davideast mentioned. From this conversation I understand its going to take time. @hiepxanh you can try other solutions. One which I saw recently is to use chrome's rendertron. Definitely not better than universal. But will work. Check this blog

sureshprabhu2218 avatar Nov 11 '17 07:11 sureshprabhu2218

Finally Server Side Rendering with Firebase :blush:

angularfirelite-logo

🎉 Introducing AngularFire Lite 🎉

AnglularFire Lite is a very very simple library to use Angular with Firebase it supports server-side rendering out of the box for both firebase realtime database and firestore. It renders the SSR app and then switches to the non-SSR to receive real-time updates from firebase with no flickering at all when the angular app bootstraps. Visit AngularFire Lite repo to get started today:

Get Started

Demo App

Please note that this is a personal initiative I started and it's still in very early beta so your help by contributing and supporting the library will be the only way to improve it.

hamedbaatour avatar Nov 28 '17 20:11 hamedbaatour

@hamedbaatour This is really cool! I wish you would have contributed to this project however. We plan on adding support for the rest of the Firebase features soon.

I do have one question, is it actually lighter than AngularFire? AngularFire itself is really small, the DB API is 3kb. The Firestore API is 2.3kb. The Auth API is 950 bytes.

davideast avatar Nov 28 '17 20:11 davideast

wow, you deserve this 🥇 I love that, finally, I can't wait to use this, thanks you for your initiative. That help me a lot and make my dream come true. I still didn't using any different database to only work with firebase

hiepxanh avatar Nov 29 '17 01:11 hiepxanh

Looking forward to SSR support

khalid-halo avatar Dec 13 '17 19:12 khalid-halo

well, without support for socket method from angular. We (angularfire, or anything use socket) cannot work with universal. That really bad news,

  1. there are some investment in socket.io with ssr from angular universal but look like very slow. They are trying to doing something with ZoneMacroTaskWrapper but i don't understand.

  2. Sir @jamesdaniels is trying to doing something different, Support Angular Universal he work with service worker and xmlhttprequest to make our dream come true. I hope that work. 👍 everything I can help, you just mention, sir.

hiepsieuviet avatar Dec 14 '17 01:12 hiepsieuviet

still looking forward to make my web go production 👍 , event I have dedicated server, when calling to firebase, my data still not render with universal. I can't SEO

hiepxanh avatar Jan 21 '18 14:01 hiepxanh

Quick question - can this not be resolved using Cloud apis on server - https://cloud.google.com/firestore/docs/reference/rest/ . With isPlatformBrowser, isPlatformServer can't we control it ?

kpsrinivas avatar Jan 31 '18 04:01 kpsrinivas

are there already results of an AngularFire Lite cooperation? When can we expect SSR in AngularFire?

doczoidberg avatar Feb 06 '18 12:02 doczoidberg

@doczoidberg there's a pull request (#1454) for that, so probably soon :)

ceolinwill avatar Feb 06 '18 12:02 ceolinwill

"angularfire2": "^5.0.0-rc.6" seems to be working with ssr. Did anything change ?

kpsrinivas avatar Feb 08 '18 05:02 kpsrinivas

@kpsrinivas were you able to make the ssr work with rc.6?

andreaaschei avatar Apr 13 '18 07:04 andreaaschei

@andreaaschei yes.

kpsrinivas avatar Apr 13 '18 08:04 kpsrinivas

angularfire2@next has more SSR work, will be cutting RC.7 shortly. Give it a spin.

On Fri, Apr 13, 2018, 4:47 AM Srinivas Kolluri [email protected] wrote:

@andreaaschei https://github.com/andreaaschei yes.

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/angular/angularfire2/issues/1073#issuecomment-381068921, or mute the thread https://github.com/notifications/unsubscribe-auth/AACvr1oL7Y9a8gjw9Z3CRMN_yqKFRWvnks5toGY8gaJpZM4OZObk .

jamesdaniels avatar Apr 13 '18 10:04 jamesdaniels

Seems to be working with RC.7 although some modifications are needed to the official universal guide if you're using material and other libraries in the server.ts file. Thanks @jamesdaniels and the team at angular firebase for the very helpful video highly recommend checking it out.

https://www.youtube.com/watch?v=wij2-gyG12E

Still will need to test it further but great job guys!

anasbaddyr avatar Apr 19 '18 19:04 anasbaddyr

SSR in the minimal sense. It still does not have state transfer nor does it have auth.

patrickmichalina avatar Apr 19 '18 19:04 patrickmichalina

We're working on those. This is a nessisary first step and was already a large change, so we wanted to get out and in people's hands while we work on state-transfer story (will require a change to the Firebase JS SDK) and auth (a needed change just landed in the SDK).

On Thu, Apr 19, 2018, 1:20 PM Patrick Michalina [email protected] wrote:

SSR in the minimal sense. It still does not have state transfer nor does it have auth.

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/angular/angularfire2/issues/1073#issuecomment-382851479, or mute the thread https://github.com/notifications/unsubscribe-auth/AACvr3fLZcDVzPtJfciP10YUJJyoKRTbks5tqON9gaJpZM4OZObk .

jamesdaniels avatar Apr 19 '18 22:04 jamesdaniels