Skip to content

Using with Angular 17 SSR -> document is not defined on initializeAppCheck #3488

Closed

Description

@jjgriff93

Version info

Angular:
17.0.8

Firebase:
10.7.1

AngularFire:
17.0.0

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

How to reproduce these conditions

Steps to set up and reproduce

  1. New Angular 17 project
  2. ng add @angular/fire@17
  3. ng add @angular/ssr
  4. Set up App Check as per https://github.com/angular/angularfire/blob/master/docs/app-check.md
  5. ng build

Debug output

⠇ Building...
ReferenceError: document is not defined
    at makeDiv (/app/.angular/prerender-root/node_modules/@firebase/app-check/dist/esm/index.esm2017.js:1119:26)
    at initializeV3 (/app/.angular/prerender-root/node_modules/@firebase/app-check/dist/esm/index.esm2017.js:1064:19)
    at _ReCaptchaV3Provider.initialize (/app/.angular/prerender-root/node_modules/@firebase/app-check/dist/esm/index.esm2017.js:1255:9)
    at _activate (/app/.angular/prerender-root/node_modules/@firebase/app-check/dist/esm/index.esm2017.js:1543:20)
    at initializeAppCheck (/app/.angular/prerender-root/node_modules/@firebase/app-check/dist/esm/index.esm2017.js:1498:5)
    at <anonymous> (app/.angular/prerender-root/c56c3be0-e454-4b23-bf7d-d028ecf276ec/node_modules/@angular/fire/fesm2022/angular-fire.mjs:216:44)
    at <anonymous> (/app/.angular/prerender-root/c56c3be0-e454-4b23-bf7d-d028ecf276ec/node_modules/@angular/fire/fesm2022/angular-fire.mjs:148:57)
    at _ZoneDelegate.invoke (/app/.angular/prerender-root/node_modules/zone.js/fesm2015/zone.js:368:26)
    at _Zone.run (/app/.angular/prerender-root/node_modules/zone.js/fesm2015/zone.js:129:43)
    at _NgZone.runOutsideAngular (/app/.angular/prerender-root/node_modules/@angular/core/fesm2022/core.mjs:1461
An unhandled exception occurred: document is not defined

Expected behavior

AngularFire should check if rendering on a browser before calling App Check code and RecaptchaV3 (which relies on document being available)

Actual behavior

No check is done, so when providing App Check, this above error occurs as soon as pre-rendering on the server happens and document is accessed but isn't available due to not running in a browser

Activity

  1. jamesdaniels commented on Jan 6, 2024

    @jamesdaniels
    Contributor

    It's older Angular but here's an example on how to use AppCheck with SSR

    const admin = injector.get<app.App|null>(FIREBASE_ADMIN, null);

  2. jjgriff93 commented on Jan 6, 2024

    @jjgriff93
    Author

    Thanks James, I've managed to work around it for now by using isPlatformServer()

    provideAppCheck(() => {
          // Don't initialise AppCheck if running in server
          // Workaround for https://github.com/angular/angularfire/issues/3488
          const platformId = inject(PLATFORM_ID);
          if (isPlatformServer(platformId)) {
            return;
          }
    
          let provider: CustomProvider | ReCaptchaV3Provider;
    
          // Initialise AppCheck natively if running on iOS or Android
          if (Capacitor.isNativePlatform()) {
            // Set the web debug token to false if running on native
            globalThis.FIREBASE_APPCHECK_DEBUG_TOKEN = false;
            provider = new CustomProvider({
              getToken: () =>
                FirebaseAppCheck.getToken({
                  forceRefresh: true
                }) as Promise<AppCheckToken>
            });
          } else {
            // Or use ReCAPTCHAv3 if running in web
            provider = new ReCaptchaV3Provider(
              environment.firebase.recaptchaSiteKey
            );
          }
    
          // Initialise AngularFire app check using provider
          const appCheck = initializeAppCheck(getApp(), {
            provider: provider
          });
          return appCheck;
        }),

    Although now hitting a similar issue with Performance which the above doesn't seem to work for, so will raise that as a separate ticket

  3. jjgriff93 commented on Jan 6, 2024

    @jjgriff93
    Author

    Raised in #3489

  4. added
    comp: ssrServer-side rendering, hydration, @angular/ssr interop.
    type: bugDefect: expected behavior doesn't happen.
    on Sep 25, 2026
  5. added a commit that references this issue on Sep 27, 2026
    84a8a51
  6. added this to the 21.0.0-rc.2 milestone on Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp: app-checkApp Check (src/app-check).comp: ssrServer-side rendering, hydration, @angular/ssr interop.type: bugDefect: expected behavior doesn't happen.version: current (v17+)Targets the current modular API (v17+).

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions