October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

What Is BEFORE_APP_SERIALIZED in Angular?

Angular’s BEFORE_APP_SERIALIZED token registers synchronous or asynchronous callbacks to run just before server rendering serializes platform state.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

BEFORE_APP_SERIALIZED is an injection token from Angular’s @angular/platform-server package. It lets an application register callbacks that run just before Angular’s server-rendering process serializes the current platform state to a string. Those callbacks may run synchronously or return a promise.

When does the hook run?

Angular calls the registered callbacks near the end of server rendering, immediately before renderApplication or renderModule renders the current platform state to a string. The hook is for work that needs to happen before that serialization step; it is not a general-purpose callback for every stage of application startup or rendering.

As an Amazon Associate I earn from qualifying purchases.

Angular documents the token as a stable API. For version-specific behavior or migration questions, consult the API reference for the Angular version used by your application: Angular BEFORE_APP_SERIALIZED API reference.

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

What callback forms can you register?

The token’s current type is InjectionToken<readonly (() => void | Promise<void>)[]>. A callback can finish synchronously by returning void, or represent asynchronous work by returning a Promise<void>. Angular’s source-level integration test demonstrates registering callbacks with multi providers, including a promise-based callback: Angular platform-server integration test.

Here is the provider shape, adapted to illustrate the API:

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

An asynchronous callback can return a promise:

{ provide: BEFORE_APP_SERIALIZED, useValue: () => doAsyncWork(), multi: true }

Import the token from @angular/platform-server and register providers in the server-rendering configuration appropriate to your application. Check the documentation and declarations for your installed Angular version when adapting the example; the test shows the provider pattern, not a guarantee that every application should use this hook.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When should you use it?

Use the hook only when a particular task must finish immediately before server-rendered platform state is serialized. The key decision is whether that task is synchronous or asynchronous: return directly for synchronous work, or return its promise when completion must be represented asynchronously.

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

The API definition establishes the timing and callback contract, but does not by itself recommend a specific production use case or spell out detailed error-handling guarantees. Base those decisions on the needs of your application and the documentation for its Angular version.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.