Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/app/shared/live-region/live-region.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('liveRegionService', () => {
service = new LiveRegionService(
new UUIDService(),
accessibilitySettingsService,
'browser',
);
});

Expand All @@ -34,6 +35,25 @@ describe('liveRegionService', () => {
});
});

describe('addMessage on the server', () => {
beforeEach(() => {
service = new LiveRegionService(
new UUIDService(),
accessibilitySettingsService,
'server',
);
});

it('should not store the message nor schedule its removal, so SSR is not held up', fakeAsync(() => {
// fakeAsync fails the spec if a timer is still pending at the end
const uuid = service.addMessage('Message One');

expect(uuid).toBeTruthy();
expect(service.getMessages()).toEqual([]);
expect(accessibilitySettingsService.getAsNumber).not.toHaveBeenCalled();
}));
});

describe('clearMessages', () => {
it('should clear the messages', () => {
expect(service.getMessages().length).toEqual(0);
Expand Down
12 changes: 11 additions & 1 deletion src/app/shared/live-region/live-region.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Injectable } from '@angular/core';
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
import { isPlatformServer } from '@angular/common';
import { BehaviorSubject, map, Observable, switchMap, take, timer } from 'rxjs';
import { environment } from '../../../environments/environment';
import { UUIDService } from '../../core/shared/uuid.service';
Expand All @@ -18,6 +19,7 @@ export class LiveRegionService {
constructor(
protected uuidService: UUIDService,
protected accessibilitySettingsService: AccessibilitySettingsService,
@Inject(PLATFORM_ID) protected platformId: string,
) {
}

Expand Down Expand Up @@ -67,6 +69,14 @@ export class LiveRegionService {
*/
addMessage(message: string): string {
const uuid = this.uuidService.generate();

// Skip during server-side rendering: there is no screen reader to announce to, and the removal timer below
// would keep the Angular zone unstable, so the SSR response would only be sent once the timeout
// (30 s by default) has elapsed. The browser adds the message again after the app boots client-side.
if (isPlatformServer(this.platformId)) {
return uuid;
}

this.messages.push({ message, uuid });

this.getConfiguredMessageTimeOutMs().pipe(
Expand Down
Loading