You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Using with Angular 17 SSR -> document is not defined on initializeAppCheck #3488
⠇ 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
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/3488constplatformId=inject(PLATFORM_ID);if(isPlatformServer(platformId)){return;}letprovider: CustomProvider|ReCaptchaV3Provider;// Initialise AppCheck natively if running on iOS or Androidif(Capacitor.isNativePlatform()){// Set the web debug token to false if running on nativeglobalThis.FIREBASE_APPCHECK_DEBUG_TOKEN=false;provider=newCustomProvider({getToken: ()=>FirebaseAppCheck.getToken({forceRefresh: true})asPromise<AppCheckToken>});}else{// Or use ReCAPTCHAv3 if running in webprovider=newReCaptchaV3Provider(environment.firebase.recaptchaSiteKey);}// Initialise AngularFire app check using providerconstappCheck=initializeAppCheck(getApp(),{provider: provider});returnappCheck;}),
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
Activity
jamesdaniels commentedon Jan 6, 2024
It's older Angular but here's an example on how to use AppCheck with SSR
angularfire/samples/advanced/src/app/app.module.ts
Line 46 in 8157744
jjgriff93 commentedon Jan 6, 2024
Thanks James, I've managed to work around it for now by using
isPlatformServer()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
jjgriff93 commentedon Jan 6, 2024
Raised in #3489
fix(app-check)!: skip App Check during server rendering by default (a…