Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

BEFORE_APP_SERIALIZED is an injection token from Angular’s @angular/platform-server package. It lets you register callbacks that run just before Angular serializes server-rendered platform state to a string. A callback may finish immediately or return a promise, so it can represent synchronous or asynchronous work that must complete before serialization.

What the token does

Angular documents BEFORE_APP_SERIALIZED as a stable API for functions executed by renderApplication or renderModule immediately before the current platform state is rendered to a string. Its declared type is InjectionToken<readonly (() => void | Promise<void>)[]>. In practical terms, the token is a place to provide one or more pre-serialization callbacks.

The timing is the important part: this hook is for work that needs to happen at the end of server rendering, before serialization. It is not a general-purpose hook for every stage of an Angular app’s lifecycle.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How to provide a callback

Angular’s integration example uses a multi provider, allowing callback functions to be registered with the token. A synchronous callback can return nothing; an asynchronous callback can return a promise.

{ provide: BEFORE_APP_SERIALIZED, useValue: () => { /* synchronous work */ }, multi: true }
{ provide: BEFORE_APP_SERIALIZED, useValue: async () => { /* asynchronous work */ }, multi: true }

This illustrates the provider shape, not a recommendation to use the hook for any particular task. Adapt the provider syntax to the server-rendering setup and Angular version in your application.

When to use it

Use this hook only when a task must complete immediately before the server-rendered platform state is serialized. Choose a synchronous callback when the work is immediate, or return a promise when the work is asynchronous and needs to be represented as part of the callback.

If the work belongs at another point in application startup or rendering, this token may not be the right lifecycle mechanism. The API reference establishes the callback timing and return types, but does not prescribe production use cases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check behavior for your Angular version

The official API reference documents the token, and Angular’s source integration test demonstrates multi-provider registration, including a promise-based callback. For version-specific behavior or migration questions, consult the documentation and package declarations for the Angular version you have installed; repository examples on the mutable main branch may not match every release.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.