question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AppCheck Build Error During Prerender for Angular SSR

See original GitHub issue

Version info

Angular: 13.0.3

Firebase: 9.6.4

AngularFire: 7.2.0

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

How to reproduce these conditions

Steps to set up and reproduce

After setting up AppCheck as such:


import { initializeAppCheck, provideAppCheck, ReCaptchaV3Provider } from '@angular/fire/app-check';

    provideAppCheck(() => initializeAppCheck(getApp(), { provider: new ReCaptchaV3Provider(environment.recaptchaSiteKey), isTokenAutoRefreshEnabled: true })),

I am able to serve my application with both yarn server and yarn dev:ssr and I’m able to load the site with AppCheck seemingly working. However, when I try to run yarn prerender, I get the following error:

Unhandled Promise rejection: document is not defined ; Zone: <root> ; Task: Promise.then ; Value: ReferenceError: document is not defined

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
posvacommented, Nov 30, 2022

There is actually an example at https://github.com/angular/angularfire/blob/master/samples/advanced/src/app/app.module.ts#L47-L57

You need to use the admin sdk on server

1reaction
ribalnasrcommented, Feb 21, 2022

Here’s my workaround for now…

I created a service:

@Injectable({
    providedIn: 'root'
})
export class RecaptchaBrowser {

    constructor(
        @Inject(PLATFORM_ID) private platformId: string
    ) { }

    provider(siteKey: string) {
        return isPlatformBrowser(this.platformId)
            ? new ReCaptchaV3Provider(siteKey)
            : new CustomProvider({
                getToken: () => new Promise(() => { })
            })
    }
}

and then I import the AppCheck module as follows:

    provideAppCheck((injector) => initializeAppCheck(getApp(), {
      provider: injector.get(RecaptchaBrowser).provider(environment.recaptchaSiteKey),
      isTokenAutoRefreshEnabled: true
    })),

This will skip the ReCaptchaV3Provider for server-side rendering and import it only in the browser.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I implement firebase app-check in my Angular 12 ...
With this: appCheck.activate('reCAPTCHA secret key');. Hint: click on the app row instead of context menu icon (three dots). It will slide down ...
Read more >
Server-side rendering (SSR) with Angular Universal
This guide describes Angular Universal, a technology that renders Angular applications on the server. A normal Angular application executes in the browser, ...
Read more >
angular/universal - Gitter
Hi guys any fix for Reference Error document not defined when run npm run dev:ssr ? already added Document to Constructors in every...
Read more >
Next.js Pre-rendering and Data Fetching Methods - Pagepro
Learn about SSG, SSR, CSR, and ISR. The most popular pre-rendering and data fetching methods to build fast and SEO-friendly pages in ......
Read more >
Integrate Next.js | Firebase Hosting - Google
Before you begin; Initialize Firebase; Serve static content; Pre-render dynamic ... Serve fully dynamic content (SSR); Configure Hosting behavior with ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found