Ignoring BEFORE_APP_SERIALIZED Exception: TypeError: Converting circular structure to JSON

399 Views Asked by At

I have an Angular application that uses Angular Universal to do Server Side Rendering, and the TransferState module to pass the state of the application from the server to the client.

I get this warning log on certain pages:

Ignoring BEFORE_APP_SERIALIZED Exception:  TypeError: Converting circular structure to JSON
at Object.stringify (<anonymous>)
at TransferState.toJson (/Users/my-user/Documents/MyApp/my-app/dist/server.js:40450:21)
at /Users/my-user/Documents/MyApp/my-app/dist/server.js:1338:122
at /Users/my-user/Documents/MyApp/my-app/dist/server.js:1399:29
at ZoneDelegate.invoke (/Users/my-user/Documents/MyApp/my-app/dist/server.js:167690:26)
at Zone.run (/Users/my-user/Documents/MyApp/my-app/dist/server.js:167449:43)
at /Users/my-user/Documents/MyApp/my-app/dist/server.js:168188:34
at ZoneDelegate.invokeTask (/Users/my-user/Documents/MyApp/my-app/dist/server.js:167722:31)
at Zone.runTask (/Users/my-user/Documents/MyApp/my-app/dist/server.js:167494:47)
at drainMicroTaskQueue (/Users/my-user/Documents/MyApp/my-app/dist/server.js:167900:35)

I'm not so concerned about the warning itself (I'd like to know what exactly is causing that circular reference though) but I find it really annoying that it logs the warning every single time, regardless of the environment (ergo also in production).

How could I a) prevent the error? b) prevent the warning log?

Here's the origin of the log:

function _render<T>(
    platform: PlatformRef, moduleRefPromise: Promise<NgModuleRef<T>>): Promise<string> {
  return moduleRefPromise.then((moduleRef) => {
    const transitionId = moduleRef.injector.get(ɵTRANSITION_ID, null);
    if (!transitionId) {
      throw new Error(
          `renderModule[Factory]() requires the use of BrowserModule.withServerTransition() to ensure
the server-rendered app can be properly bootstrapped into a client app.`);
    }
    const applicationRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
    return applicationRef.isStable.pipe((first((isStable: boolean) => isStable)))
        .toPromise()
        .then(() => {
          const platformState = platform.injector.get(PlatformState);

          const asyncPromises: Promise<any>[] = [];

          // Run any BEFORE_APP_SERIALIZED callbacks just before rendering to string.
          const callbacks = moduleRef.injector.get(BEFORE_APP_SERIALIZED, null);
          if (callbacks) {
            for (const callback of callbacks) {
              try {
                const callbackResult = callback();
                if (ɵisPromise(callbackResult)) {
                  asyncPromises.push(callbackResult);
                }

              } catch (e) {
                // Ignore exceptions.
                console.warn('Ignoring BEFORE_APP_SERIALIZED Exception: ', e);
              }
            }
          }

          const complete = () => {
            const output = platformState.renderToString();
            platform.destroy();
            return output;
          };

          if (asyncPromises.length === 0) {
            return complete();
          }

          return Promise
              .all(asyncPromises.map(asyncPromise => {
                return asyncPromise.catch(
                    e => { console.warn('Ignoring BEFORE_APP_SERIALIZED Exception: ', e); });
              }))
              .then(complete);
        });
  });
}
0

There are 0 best solutions below