This project was generated using Angular CLI and updated to Angular 22, with server-side rendering enabled and rollbar.js integrated for client-side and server-side errors.
Angular 22 requires Node.js ^22.22.3 || ^24.15.0 || >=26.0.0.
src/app/rollbar.config.tscreates the client-side Rollbar instance.src/app/rollbar.service.tsloads that file with a dynamicimport(), so Rollbar is kept out of the initial bundle and downloaded as a separate chunk.src/app/app.config.tsstarts loading it as soon as the app starts, so telemetry is collected before the first error.src/app/rollbar.errorhandler.tsreplaces Angular'sErrorHandler. It logs each error to the console and sends it to Rollbar once Rollbar has loaded, so errors that happen while the chunk is still downloading are not lost.provideBrowserGlobalErrorListeners()forwards uncaught errors and unhandled promise rejections to thatErrorHandler. This is whycaptureUncaughtandcaptureUnhandledRejectionsare turned off inrollbar.config.ts: leaving them on would report those errors twice.src/server.tscreates a separate server-side Rollbar instance for the Express server. It passes areportError()function to the Angular app in the request context, so errors that Angular catches during server-side rendering are reported with that instance, along with the request, by the sameRollbarErrorHandler.src/app/app.routes.server.tsrenders every route on request (RenderMode.Server) for this reason: a prerendered route is rendered once at build time, when there is no request and no server Rollbar instance, so errors there are only logged to the console.
The app runs without zone.js, which is the default since Angular 21.
To use this example, add your Rollbar tokens. You can find the tokens in the Rollbar UI for your project. Go to Project Settings, then Project Access Tokens, and note the post_client_item token (client-side token) and post_server_item token (server-side token, if using).
Add the client-side token in src/app/rollbar.config.ts by replacing ROLLBAR_POST_CLIENT_ITEM_TOKEN with your post_client_item token.
Add the server-side token in src/server.ts by replacing ROLLBAR_POST_SERVER_ITEM_TOKEN with your post_server_item token.
To start a local development server, run:
ng serveOnce the server is running, open your browser and navigate to http://localhost:4200/. The application will automatically reload whenever you modify any of the source files.
Now, you can test the Rollbar integration by clicking the buttons "Throw an Error" and "Log a Warning". You should see the error and the warning in the Rollbar UI within a few seconds.
To run the server-side rendering server, first build:
npm run buildthen run:
npm run serve:ssr:angularOnce the server is running, open your browser and navigate to http://localhost:4000/
Now, you can test the Rollbar integration by navigating to the url http://localhost:4000/api/server-error. You should see "Server error logged to Rollbar" in your browser and the error should appear in the Rollbar UI within a few seconds.
The server only renders requests whose Host header is listed in security.allowedHosts in angular.json, which protects against server-side request forgery. This example allows localhost; add your own hostnames before deploying it.
To run the unit tests with Vitest, run:
npm testNeed help? Rollbar Support can be reached via email at support@rollbar.com